Search code examples
pythonletter

Three Sets of Doubles


I'm writing a program where I am given a file called words.txt. It has every single word in it, and it starts out as "aa aah aahed aahing aahs . . ." Using the file, I'm supposed to run through it and find all the words that have three sets of double letters. (Double letters are where there are two of the same letter consecutively, such as in boot, kill, or abyss.) Three sets of double letters means words like committee, with 1 set (mm), 2 sets, (tt), and a third set, (ee). The program then has to display all the words found, and a total count of the number of words found. I am not allowed to import modules or other tools, I am only supposed to use basic Python. Any help is appreciated.


found_words = []
total = 0

for line in f:
    line = line.strip()
    letter_list = []
    
    for letter in line:
        letter_list.append(letter)

for letter in line:
    if letter_list[0] == letter_list[1]:
        found_words.append(line)
    else:
        del letter_list[0]
        
            

print(letter_list)

print(found_words) 
        


f.close()

Solution

  • Here you go

    f = open("words.txt")
    found_words = []
    words= f.read().split()
    for word in words:
      consecutive =0
      lastLetter=""
      for letter in word: 
        if letter == lastLetter:
          consecutive+=1
          //If you want it that if three similar letters should count as 2 repeated, dont uncomment the next line. If you dont want 3 consecutive letters to count as 2 repeated, uncomment the next line
          //lastLetter=""
        lastLetter =letter
      if consecutive ==3:
       found_words.append(word)
    print("WORDS:",found_words)
    print("LENGTH:",len(found_words))