V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Graxce
V2EX  ›  Python

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

  •  
  •   Graxce · 2016-02-27 17:06:48 +08:00 · 2531 次点击
    这是一个创建于 3186 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    5 条回复    2016-02-28 09:36:55 +08:00
    Slienc7
        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})
    Graxce
        2
    Graxce  
    OP
       2016-02-27 19:45:19 +08:00
    @xgowex 唔,忘了说了。只能用内置函数。
    lijsh
        3
    lijsh  
       2016-02-28 02:13:18 +08:00   ❤️ 1
    题目没说只能用内置函数啊,我也是用正则的;
    高票答案里除了正则以外就是定义个 count 变量,结合`isalpha()`方法。
    Graxce
        4
    Graxce  
    OP
       2016-02-28 08:34:51 +08:00
    @lijsh 可是我为什么导包会报错呢!!真是醉了。
    Graxce
        5
    Graxce  
    OP
       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 切开然后逐个判断。可是脑袋就是转不过来。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5161 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 05:40 · PVG 13:40 · LAX 21:40 · JFK 00:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.