习题 25 更多更多的实践

习题 25 更多更多的实践

# ex25.py

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word

def print_last_word(words):
    """prints the last word after popping it off."""
    word = words.pop(-1)
    return word

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

结果:

>>> import ex25
>>> sentence = "All good things come to those who wait."
>>> words = ex25.break_words(sentence)
>>> words
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> sorted_words = ex25.sort_words(words)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex25.print_first_word(words)
All
>>> ex25.print_last_word(words)
'wait.'
>>> words
['good', 'things', 'come', 'to', 'those', 'who']
>>> ex25.print_first_word(sorted_words)
All
>>> ex25.print_last_word(sorted_words)
'who'
>>> sorted_words
['come', 'good', 'things', 'those', 'to', 'wait.']
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,860评论 25 709
  • 一:沙箱 1.1. 维基百科定义:在软件安全中,沙箱是一个用来分离软件运行的安全机制。它经常用来测试代码,或者来自...
    ovens阅读 1,148评论 0 0
  • 你是否经历过绝望? 现在我正经历着绝望,这是一种饱暖思淫欲的绝望,是一种精神上的绝望,与物质上的绝望相比,这更能摧...
    去他娘的昵称阅读 173评论 0 0
  • 当今社会,都主张亲子教育,又及教育西化,崇尚给孩子自由民主和足够的尊重。他画的画你得好生琢磨这到底是个啥,一不...
    任性才来写俩字阅读 930评论 4 5
  • 前不久,我在小灶群里提出了一个关于“选择”的问题,希望伙伴儿们能帮我解惑。 我问的是:我要不要放下一切,带着孩子,...
    人间最美是清欢阅读 801评论 0 7