Python小代码收集

http://tieba.baidu.com/p/554284778

1. def cleanword(word): 

(用Python写出程序,使程序可以通过下面的doctest) 
    """ 
      >>> cleanword('what?') 
      'what' 
      >>> cleanword('"now!"') 
      'now' 
      >>> cleanword('?+="word!,@$()"') 
      'word' 
    """ 
2. def has_dashdash(s): 
(用Python写出程序,使程序可以通过下面的doctest) 
    """ 
      >>> has_dashdash('distance--but') 
      True 
      >>> has_dashdash('several') 
      False 
      >>> has_dashdash('critters') 
      False 
      >>> has_dashdash('spoke--fancy') 
      True 
      >>> has_dashdash('yo-yo') 
      False 
    """ 
3. def extract_words(s): 
(用Python写出程序,使程序可以通过下面的doctest) 
    """ 
      >>> extract_words('Now is the time!  "Now", is the time? Yes, now.') 
      ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time', 'yes', 'now'] 
      >>> extract_words('she tried to curtsey as she spoke--fancy') 
      ['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke', 'fancy'] 
    """ 
4. def wordcount(word, wordlist): 
(用Python写出程序,使程序可以通过下面的doctest) 
    """ 
      >>> wordcount('now', ['now', 'is', 'time', 'is', 'now', 'is', 'is']) 
      ['now', 2] 
      >>> wordcount('is', ['now', 'is', 'time', 'is', 'now', 'is', 'the', 'is']) 
      >>> wordcount('time', ['now', 'is', 'time', 'is', 'now', 'is', 'is']) 
      ['time', 1] 
      >>> wordcount('frog', ['now', 'is', 'time', 'is', 'now', 'is', 'is']) 
      ['frog', 0] 
    """ 
5. def wordset(wordlist): 
(用Python写出程序,使程序可以通过下面的doctest) 
    """ 
      >>> wordset(['now', 'is', 'time', 'is', 'now', 'is', 'is']) 
      ['is', 'now', 'time'] 
      >>> wordset(['I', 'a', 'a', 'is', 'a', 'is', 'I', 'am']) 
      ['I', 'a', 'am', 'is'] 
      >>> wordset(['or', 'a', 'am', 'is', 'are', 'be', 'but', 'am']) 
      ['a', 'am', 'are', 'be', 'but', 'is', 'or'] 
    """ 

6. def longestword(wordset): 
(用Python写出程序,使程序可以通过下面的doctest) 
    """ 
      >>> longestword(['a', 'apple', 'pear', 'grape']) 
      5 
      >>> longestword(['a', 'am', 'I', 'be']) 
      2 
      >>> longestword(['this', 'that', 'supercalifragilisticexpialidocious']) 
      34 
    """
5. def wordset(wordlist): 
(用Python写出程序,使程序可以通过下面的doctest) 
    """ 
      >>> wordset(['now', 'is', 'time', 'is', 'now', 'is', 'is']) 
      ['is', 'now', 'time'] 
      >>> wordset(['I', 'a', 'a', 'is', 'a', 'is', 'I', 'am']) 
      ['I', 'a', 'am', 'is'] 
      >>> wordset(['or', 'a', 'am', 'is', 'are', 'be', 'but', 'am']) 
      ['a', 'am', 'are', 'be', 'but', 'is', 'or'] 
    """ 
6. def longestword(wordset): 
(用Python写出程序,使程序可以通过下面的doctest) 
    """ 
      >>> longestword(['a', 'apple', 'pear', 'grape']) 
      5 
      >>> longestword(['a', 'am', 'I', 'be']) 
      2 
      >>> longestword(['this', 'that', 'supercalifragilisticexpialidocious']) 
      34 
    """
6. def longestword(wordset): 
(用Python写出程序,使程序可以通过下面的doctest) 
    """ 
      >>> longestword(['a', 'apple', 'pear', 'grape']) 
      5 
      >>> longestword(['a', 'am', 'I', 'be']) 
      2 
      >>> longestword(['this', 'that', 'supercalifragilisticexpialidocious']) 
      34 
    """

 ['is', 4] 

7. def myreplace(old, new, s): 
(用Python写出程序,使程序可以通过下面的doctest) 
 """ 
 Replace all occurances of old with new in the string s. 
 >>> myreplace(',', ';', 'this, that, and, some, other, thing') 
 'this; that; and; some; other; thing' 
 >>> myreplace(' ', '**', 'Words will now be separated by stars.') 
 'Words**will**now**be**separated**by**stars.' 
 """


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值