| | |
Can someone explain how this works?
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
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:
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.
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:
Python Syntax (Toggle Plain Text)
def main(): infile = open("words.txt",'r') for w in infile: if td(w): print w infile.close() print "Done!" def td(w): for i in range(len(w)-5): if w[i] == w[i+1] and \ (w[i+2] == w[i+3]) and \ (w[i+4] == w[i+5]): return True return False 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.
1
#2 Oct 24th, 2009
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:
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
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:
python Syntax (Toggle Plain Text)
>>> w = 'aabbccdd' >>> print len(w), len(w) - 5 8 3 >>> range(3) [0, 1, 2] >>> print w[0], w[1], w[2], w[3] a a b b >>> print w[2+2], w[2+3], w[2+4], w[2+5] c c d d >>> print w[3+5] Traceback (most recent call last): File "<input>", line 1, in <module> IndexError: string index out of range >>>
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
•
•
Join Date: Dec 2006
Posts: 1,008
Reputation:
Solved Threads: 285
0
#3 Oct 24th, 2009
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.
Python Syntax (Toggle Plain Text)
def td(w): for num in range(0, len(w)-5): print "first test =", w[num], w[num+1] print "2nd test =", w[num+2], w[num+3] print "3rd test =", w[num+4], w[num+5] if w[num] == w[num+1] and \ (w[num+2] == w[num+3]) and \ (w[num+4] == w[num+5]): return True return False
Last edited by woooee; Oct 24th, 2009 at 4:53 pm.
Linux counter #99383
![]() |
Similar Threads
- Senator Can't Explain Net Neutrality (AUDIO LINK) (Upcoming News Stories)
- Dummy asks: how can i? (PHP)
- Why won't this compile (C++)
- X server (*nix Software)
- errors in my file but not sure whats wrong file attatched (Visual Basic 4 / 5 / 6)
- Googles Patent is out (Search Engine Optimization)
- Will somebody please explain value parameters to me!!! (C)
- Factorial program (Java)
- don't understand (C++)
- my IE has been hijacked by res://mbnuq.dll/index.html#96676 (Viruses, Spyware and other Nasties)
Other Threads in the Python Forum
- Previous Thread: Return selected variables
- Next Thread: Python data validation. Idioms and ideas.
| Thread Tools | Search this Thread |
alarm ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator gnu graphics gui halp heads homework http ideas import input itunes java leftmouse line linux list lists loop maze module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh statistics string strings sudokusolver sum terminal text thread threading time tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo vigenere web webservice wikipedia write wxpython xlib






