How do i properly adjust the code so that it only searches for half of the text
ID: 3914835 • Letter: H
Question
How do i properly adjust the code so that it only searches for half of the text and it publishes the results? I adjusted the while loop in the search() method, but none of the results are getting printed. How can i get the results to print after it searches for half of the pattern?
The idea is that it searches for half of the pattern that is entered [for instance if there is 4 letters entered, it would search the last 2] and if the last two match, it would publish the results.
For instance: once i generate the code with a random generator for 100 characters of A,C,T,G and the pattern i enter is ACCT, i want all results that are generated with ending CT to be published as results. Such as AGCT, GGCT, CCCT, as the last 2 letters have matched. Same would be if i entered 10 letter pattern and it published back all results where the last 5 matched.
I would need the code to read 50% of the code from right to left to see if it was matched and if so return the whole pattern results.
Below is code.
public class Program
{
string s;
string pattern;
const double percent = .5;
public Program(string name)
{
}
public void InputPattern()
{
Console.WriteLine("Enter the pattern you want to search: ");
pattern = (Console.ReadLine()).ToUpper(); //read user input
}
public void RandomGenerator()
{
Random geneSequence = new Random(); // The random number sequence
for (int i = 0; i < 10000; i++)
{
int x = geneSequence.Next(1, 4);
switch (x)
{
case 1:
s = s + 'C';
break;
case 2:
s = s + 'T';
break;
case 3:
s = s + 'A';
break;
case 4:
s = s + 'G';
break;
}
}
Console.WriteLine(s); //display results of string
}
public void Search() // s is string sequence, pattern is what is inputted from user
{
var watch = System.Diagnostics.Stopwatch.StartNew();
List retVal = new List();
// boyer moore algorithm is executed here
int n;
n = s.Length;
int m;
m = pattern.Length;
double p = m * percent;
int[] badChar = new int[256];
BadCharHeuristic(pattern, m, ref badChar);
int i = 0;
while (i <= (n - m))
{
int j = m - 1;
while (j >= p && pattern[j] == s[i + j])
--j;
if (j < 0)
{
retVal.Add(i);
i += (i + m < n) ? m - badChar[s[i + m]] : 1;
}
else
{
i += Math.Max(1, j - badChar[s[i + j]]);
}
}
Explanation / Answer
please try above code.. you were taking a new variable which was size * percent, just update the length variable m and use it...
============================================
Another very simple way of doing it would be to only consider the half of the string in the pattern. You can do
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.