我的思路是 先 split ,然后判断类型。如果连续三次相等就返回 True 。
下面是题目
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.
Input: A string with words.
Output: The answer as a boolean.
Example:
checkio("Hello World hello") == True
checkio("He is 123 man") == False
checkio("1 2 3 4") == False
checkio("bla bla bla bla") == True
checkio("Hi") == False
How it is used: This teaches you how to work with strings and introduces some useful functions.
Precondition: The input contains words and/or numbers. There are no mixed words (letters and digits combined).
0 < len(words) < 100
http://www.checkio.org/mission/three-words/
这个是地址
大家可以说下思路,不用具体写代码。谢谢了!
1
Slienc7 2016-02-27 18:11:29 +08:00 1
regex:
([a-zA-Z]{1,99}\s[a-zA-Z]{1,99}\s[a-zA-Z]{1,99}) |
3
lijsh 2016-02-28 02:13:18 +08:00 1
题目没说只能用内置函数啊,我也是用正则的;
高票答案里除了正则以外就是定义个 count 变量,结合`isalpha()`方法。 |
5
Graxce OP def checkio(words):
succ = 0 for word in words.split(): succ = (succ + 1)*word.isalpha() if succ == 3: return True else: return False 这个答案很赞,我也想到用 split 切开然后逐个判断。可是脑袋就是转不过来。 |