三组双打
Three Sets of Doubles
我正在编写一个程序,我得到一个名为 words.txt 的文件。它包含了每一个单词,它开始于
“啊
啊
阿赫德
啊啊
啊啊
.
.
”
使用该文件,我应该 运行 通过它并找到所有具有三组双字母的单词。 (双字母是连续出现两个相同字母的地方,比如boot, kill, abyss。)三组双字母表示像committee这样的词,1组(mm),2组,(tt),和第三组,(ee)。然后程序必须显示找到的所有单词,以及找到的单词总数。我不允许导入模块或其他工具,我只能使用基本的 Python。感谢任何帮助。
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()
给你
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))
我正在编写一个程序,我得到一个名为 words.txt 的文件。它包含了每一个单词,它开始于 “啊 啊 阿赫德 啊啊 啊啊 . . ” 使用该文件,我应该 运行 通过它并找到所有具有三组双字母的单词。 (双字母是连续出现两个相同字母的地方,比如boot, kill, abyss。)三组双字母表示像committee这样的词,1组(mm),2组,(tt),和第三组,(ee)。然后程序必须显示找到的所有单词,以及找到的单词总数。我不允许导入模块或其他工具,我只能使用基本的 Python。感谢任何帮助。
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()
给你
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))