Matching substrings

I need a little help with an assignment. Following is the code I am
being asked to create, any help with this would be great.

Ask the user to enter two strings, a and b. Then return the number of
positions where they contain the same length 2 substring. So “xxcaazz”
and “xxbaaz” yields 3, since the “xx”, “aa”, and “az” substrings appear
in the same place in both strings.

string_match('xxcaazz', 'xxbaaz') → 3
string_match('abc', 'abc') → 2
string_match('abc', 'axc') → 0

···

--
Posted via http://www.ruby-forum.com/.

We generally don't do homework outright on this forum/list. Perhaps
you could write up some code, see if it works, and if not, post it
here and we can discuss how to improve it.

···

On Wed, Apr 25, 2012 at 2:48 PM, Clay Tzoucalis <lists@ruby-forum.com> wrote:

I need a little help with an assignment. Following is the code I am
being asked to create, any help with this would be great.

Ask the user to enter two strings, a and b. Then return the number of
positions where they contain the same length 2 substring. So “xxcaazz”
and “xxbaaz” yields 3, since the “xx”, “aa”, and “az” substrings appear
in the same place in both strings.

string_match('xxcaazz', 'xxbaaz') → 3
string_match('abc', 'abc') → 2
string_match('abc', 'axc') → 0

I understand that. I am looking more for where to start,in attempt to
match the sub strings. This one is just boggling me a little bit.

···

--
Posted via http://www.ruby-forum.com/.

I do understand. I have that part done, but it doesn't help me to figure
out what code will accomplish what I am thinking. But thank you.

···

--
Posted via http://www.ruby-forum.com/.

I also against solving someones homework, but I'll give you maybe a helpful
advice.
You should look at algorithms, that solve Largest Common Subsequence
problem. Here the sequences
are arrays of consecutive length 2 substrings. For instance "abcde" should
be represented with sequence
["ab", "bc", "cd", "de"]. The rest is up to you :slight_smile:

···

26 апреля 2012 г. 6:48 пользователь Clay Tzoucalis <lists@ruby-forum.com>написал:

I need a little help with an assignment. Following is the code I am
being asked to create, any help with this would be great.

Ask the user to enter two strings, a and b. Then return the number of
positions where they contain the same length 2 substring. So “xxcaazz”
and “xxbaaz” yields 3, since the “xx”, “aa”, and “az” substrings appear
in the same place in both strings.

string_match('xxcaazz', 'xxbaaz') → 3
string_match('abc', 'abc') → 2
string_match('abc', 'axc') → 0

--
Posted via http://www.ruby-forum.com/\.

--
Dmitry S. Kravtsov

Think how you would do it on paper and translate the approach into a program.

Kind regards

robert

···

On Wed, Apr 25, 2012 at 10:36 PM, Clay Tzoucalis <lists@ruby-forum.com> wrote:

I understand that. I am looking more for where to start,in attempt to
match the sub strings. This one is just boggling me a little bit.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Ok Ok wrote in post #1058374:

I do understand. I have that part done, but it doesn't help me to figure
out what code will accomplish what I am thinking.

So, post the list of steps you have come up with here.

"This is what I would do if solving this problem with pencil and paper:
step 1: ....
step 2: ....
step 3: ....
etc"

Then we can talk about how to put those steps into code. Actually
writing it down, rather than just thinking about it, *does* make a huge
difference.

···

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote in post #1058371:

···

On Wed, Apr 25, 2012 at 10:36 PM, Clay Tzoucalis <lists@ruby-forum.com> > wrote:

I understand that. I am looking more for where to start,in attempt to
match the sub strings. This one is just boggling me a little bit.

Think how you would do it on paper and translate the approach into a
program.

Kind regards

robert

I also understand this concept. I mean more should I be thinking, array?
if statements? etc? Is there an approach that I should think about when
tackling this.

--
Posted via http://www.ruby-forum.com/\.

Then we can talk about how to put those steps into code. Actually
writing it down, rather than just thinking about it, *does* make a huge
difference.

Drawing it (in this case) makes an even bigger difference.

I'm thinking something simple like (needs monospace):

       ai
       >
       v
a = "abcdefghi"

       bi
       >
       v
b = "jklmnopqr"

then walk through each step you write from Brian's suggestion until you know it works.

···

On Apr 25, 2012, at 14:38 , Brian Candler wrote:

Stop trying to decide things like if statements, arrays, etc. Take
Robert's advice. If you were given two strings "abcdefghi" and
"qwcderghz", what steps would you do mentally or on a piece of paper
to figure out how many matches there were? Break your own mental
process down into steps. Then you can worry about translating those
steps into code, if statements, or whatever you decide to use. Robert
gave you a very good approach to tackle this, but I think you perhaps
didn't fully understand his suggestion.

Aaron out.

···

On Wed, Apr 25, 2012 at 2:57 PM, Clay Tzoucalis <lists@ruby-forum.com> wrote:

Robert Klemme wrote in post #1058371:

On Wed, Apr 25, 2012 at 10:36 PM, Clay Tzoucalis <lists@ruby-forum.com> >> wrote:

I understand that. I am looking more for where to start,in attempt to
match the sub strings. This one is just boggling me a little bit.

Think how you would do it on paper and translate the approach into a
program.

Kind regards

robert

I also understand this concept. I mean more should I be thinking, array?
if statements? etc? Is there an approach that I should think about when
tackling this.