小红背单词时如果她已经记住了 i 个单词且背了一个没有记住的新单词 i +1次则她就会记住这个新单词。例如当她按顺序背[ you thank thank ]时她第一次背单词 you 时她就能记住 you 。而由于她已经记住了一个单词所以需要背两次 thank 才能记住 thank 。用python编写代码求记住单词的数量
def count_words(words): count = 0 remembered = set()
for word in words:
if word not in remembered:
remembered.add(word)
count += 1
return count
words = ["you", "thank", "thank"] print(count_words(words)) # 输出:2
原文地址: https://www.cveoy.top/t/topic/iInm 著作权归作者所有。请勿转载和采集!