Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
one basic solution, one trying to be clever solution in Clear category for Monkey Typing by lucas.stonedrake
#basic solution
def count_words(text: str, words: set) -> int:
count = 0 # initialise a counter
for w in words:
# increment counter only if the word is in the text
count += 1 if w in text.lower() else 0
return count
# trying to be clever
def count_words(text: str, words: set) -> int:
# return the length of the intersection of words and the specified set
return len(words & {w if w in text.lower() else 1 for w in words})
if __name__ == '__main__':
print("Example:")
print(count_words("How aresjfhdskfhskd you?", {"how", "are", "you", "hello"}))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert count_words("How aresjfhdskfhskd you?", {"how", "are", "you", "hello"}) == 3, "Example"
assert count_words("Bananas, give me bananas!!!", {"banana", "bananas"}) == 2, "BANANAS!"
assert count_words("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
{"sum", "hamlet", "infinity", "anything"}) == 1, "Weird text"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
Nov. 16, 2020
Comments: