[Python] Checkio Solution 解答 – Three Words

架網編程

Problem:

Let's teach the Robots to distinguish words and numbers.

You are given a string with words and numbers separated by whitespaces (one space). The words contains only letters. You should check if the string contains three words in succession. For example, the string "start 5 one two three 7 end" contains three words in succession.

 

Level:

Elementary

 

Input:

A string with words.

 

Output:

The answer as a boolean.

 

Precondition:

The input contains words and/or numbers. There are no mixed words (letters and digits combined).
0 < len(words) < 100

 

Source: https://py.checkio.org


My Solution:

 

def checkio(words): 
  import string 
  def test(x): 
    for j in x: 
      if j in string.digits: 
        return False 
        break 
      else: 
        return True 
  if " " in words: 
    wordlist = words.split(" ") 
    if len(wordlist) < 3: 
      return False 
    else: 
      for i in range(len(wordlist)-2): 
        if test(wordlist[i]) and test(wordlist[i+1]) and test(wordlist[i+2]): 
          return True 
          break 
        i += 1 
      else: 
        return False 
  else: 
    return False

Comment:

For boolean output, that means we need to use if statement. To check every word in a list or every character in a word, we need to use for loop.

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

Copyright © Vantopics