-3

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()
Prune
  • 76,765
  • 14
  • 60
  • 81
  • 2
    Hey @Madison Han.Stackoverflow is not a code writing service. You are supposed to try it out yourself, show what error you are getting and where you get stuck. I actually can write and give you the entire code but that's just now how stackoverflow works. Ok add it in your question inside backticks like `````` and I am just writing the answer – Siddharth Agrawal Sep 17 '20 at 17:56
  • Ok. I started writing it to one double only, but then I got an error. – Madison Han Sep 17 '20 at 17:57
  • 1
    Welcome to stack overflow! Please [edit] your question to include your full [mcve]. Code doesn't show well in comments – G. Anderson Sep 17 '20 at 17:58
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results differ from what you expected. We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. – Prune Sep 17 '20 at 18:09

1 Answers1

0

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))    
Siddharth Agrawal
  • 2,960
  • 3
  • 16
  • 37