[Python] Checkio Solution 解答 – Monkey Typing



The infinite monkey theorem states that a monkey hitting keys at random on a typewriter keyboard for an infinite length of time will almost surely type out a given text, such as the complete works of John Wallis, or more likely, a Dan Brown novel.

Let's suppose our monkeys are typing, typing and typing, and have produced a wide variety of short text segments. Let's try to check them for sensible word inclusions.

You are given some text potentially including sensible words. You should count how many words are included in the given text. A word should be whole and may be a part of other word. Text letter case does not matter. Words are given in lowercase and don't repeat. If a word appears several times in the text, it should be counted only once.

For example, text - "How aresjfhdskfhskd you?", words - ("how", "are", "you", "hello"). The result will be 3.






Two arguments. A text as a string (unicode for py2) and words as a set of strings (unicode for py2).



The number of words in the text as an integer.



0 < len(text) ≤ 256
all(3 ≤ len(w) and w.islower() and w.isalpha for w in words)


Source: https://py.checkio.org

My Solution:

def count_words(text, words): 
  text = text.lower() 
  wordcount = 0 
  for word in words: 
    if word in text: 
      wordcount += 1 
  return wordcount


The problem seems to be complicated. But when you look at it closely and try to solve it, it only takes a few lines.

發佈日期:2017年9月11日 | 作者:橘小佑

Copyright © Vantopics