Can someone explain how this works?

Thread Solved

Join Date: Sep 2009
Posts: 12
Reputation: A_Dubbs is an unknown quantity at this point 
Solved Threads: 0
A_Dubbs A_Dubbs is offline Offline
Newbie Poster

Can someone explain how this works?

 
0
  #1
33 Days Ago
Hello everyone, new programmer here. In my class we have an oppurtunity to make up credit on a previous homework by modifying a program if we got any of it wrong. The problem for the progam was:

Problem #4. Write a program to solve the following problem. Find a word in English that contains 3 consecutive pairs of identical letters. For example, committee does not quite work because there is an "i" between the "mm" and "ttee". Mississippi also does not work because of the extra i's between the pairs of "ss", "ss", and "pp". You should use the text file words.txt which is on the COS 125 website.

The answer is here:

  1. def main():
  2. infile = open("words.txt",'r')
  3. for w in infile:
  4. if td(w): print w
  5.  
  6. infile.close()
  7. print "Done!"
  8.  
  9. def td(w):
  10. for i in range(len(w)-5):
  11. if w[i] == w[i+1] and \
  12. (w[i+2] == w[i+3]) and \
  13. (w[i+4] == w[i+5]):
  14. return True
  15. return False
  16.  
  17. main()

I now need to modify this to get more credit, but I don't really understand what is going on in the td(w) function. How does the length of the word - 5 work? I get the w[i] == stuff, its just the range part that is confusing me.

Here are some notes he gave us:

*A string like s = ...xxyyzz... looks like the following
*s[i] = s[i+1] = x
*s[i+2] = s[i+3] = y
*s[i+4] = s[i+5] = z
*You must have i+5 <= len(s) - 1
*WHY?
*Thus i <= len(s) - 6
*i is in range(len(s) - 5)
*What if len(s) < 6?

I feel like I should be able to get this from those notes, but i'm confused. Any clarification is much appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 263
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
1
  #2
33 Days Ago
In the if statement you are using w[i+5].

This means that you need to make sure the maximum i plus 5 does not go out of bounds for the string. Let me demonstrate:
  1. >>> w = 'aabbccdd'
  2. >>> print len(w), len(w) - 5
  3. 8 3
  4. >>> range(3)
  5. [0, 1, 2]
  6. >>> print w[0], w[1], w[2], w[3]
  7. a a b b
  8. >>> print w[2+2], w[2+3], w[2+4], w[2+5]
  9. c c d d
  10. >>> print w[3+5]
  11. Traceback (most recent call last):
  12. File "<input>", line 1, in <module>
  13. IndexError: string index out of range
  14. >>>
You see that the index of the string can only go up to 7 (the length - 1; and you need to subtract 1 because the first index is 0).

If we try to look at index 8 we get an IndexError. So your code just is making sure we're not breaking the indexing.

HTH
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster
 
0
  #3
33 Days Ago
Since you are testing 6 letters total, you want to use "len(w)-5" so you won't run out of letters. To set up an example using "aabbcc" as the simplest example, you only want the for loop to test the first letter. It will test a->a, b->b, and c->c. If you go beyond the first letter in this example, then you will run out of letters before you are done testing, so obviously it will not satisfy the conditions. So, the length of "aabbcc" is 6 and we want to test the first element, or 0 offset, so we want to stop at 6-6 or zero. If there were seven letters, "xaabbcc", we would test length=7 minus 6 = 1, so would test the first 2 letters, (or the zero and 1 offset), against the next 6 and not run out of letters. The for loop goes up to, but does not include the upper number, so it is len(w)-5.
  1. def td(w):
  2. for num in range(0, len(w)-5):
  3. print "first test =", w[num], w[num+1]
  4. print "2nd test =", w[num+2], w[num+3]
  5. print "3rd test =", w[num+4], w[num+5]
  6. if w[num] == w[num+1] and \
  7. (w[num+2] == w[num+3]) and \
  8. (w[num+4] == w[num+5]):
  9. return True
  10. return False
Last edited by woooee; 33 Days Ago at 4:53 pm.
Linux counter #99383
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC