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:
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 == 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 = 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
*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.