Check 里的“Three words”题目的解题思路是怎么样的?

2016-02-27 17:06:48 +08:00
 Graxce

我的思路是 先 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/

这个是地址
大家可以说下思路,不用具体写代码。谢谢了!

2577 次点击
所在节点    Python
5 条回复
Slienc7
2016-02-27 18:11:29 +08:00
regex:
([a-zA-Z]{1,99}\s[a-zA-Z]{1,99}\s[a-zA-Z]{1,99})
Graxce
2016-02-27 19:45:19 +08:00
@xgowex 唔,忘了说了。只能用内置函数。
lijsh
2016-02-28 02:13:18 +08:00
题目没说只能用内置函数啊,我也是用正则的;
高票答案里除了正则以外就是定义个 count 变量,结合`isalpha()`方法。
Graxce
2016-02-28 08:34:51 +08:00
@lijsh 可是我为什么导包会报错呢!!真是醉了。
Graxce
2016-02-28 09:36:55 +08:00
def checkio(words):
succ = 0
for word in words.split():
succ = (succ + 1)*word.isalpha()
if succ == 3: return True
else: return False


这个答案很赞,我也想到用 split 切开然后逐个判断。可是脑袋就是转不过来。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/259511

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX