V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Feb 27, 2016 · 3183 views
    This topic created in 3715 days ago, the information mentioned may be changed or developed.

    我的思路是 先 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 replies    2016-02-28 09:36:55 +08:00
    Slienc7
        1
    Slienc7  
       Feb 27, 2016   ❤️ 1
    regex:
    ([a-zA-Z]{1,99}\s[a-zA-Z]{1,99}\s[a-zA-Z]{1,99})
    Graxce
        2
    Graxce  
    OP
       Feb 27, 2016
    @xgowex 唔,忘了说了。只能用内置函数。
    lijsh
        3
    lijsh  
       Feb 28, 2016   ❤️ 1
    题目没说只能用内置函数啊,我也是用正则的;
    高票答案里除了正则以外就是定义个 count 变量,结合`isalpha()`方法。
    Graxce
        4
    Graxce  
    OP
       Feb 28, 2016
    @lijsh 可是我为什么导包会报错呢!!真是醉了。
    Graxce
        5
    Graxce  
    OP
       Feb 28, 2016
    def checkio(words):
    succ = 0
    for word in words.split():
    succ = (succ + 1)*word.isalpha()
    if succ == 3: return True
    else: return False


    这个答案很赞,我也想到用 split 切开然后逐个判断。可是脑袋就是转不过来。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2452 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 568ms · UTC 04:59 · PVG 12:59 · LAX 21:59 · JFK 00:59
    ♥ Do have faith in what you're doing.