2014年2月17日月曜日

Learn Python the Hard Way 24-26

Exercise 24: More Practice

print "Let's practice everything."
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""

print "-----------------"
print poem
print "-----------------"


five = 10 - 2 + 3 - 6
print "This should be five: %s" % five

def secret_formula(started):
  jelly_beans = started * 500
  jars = jelly_beans / 1000
  crates = jars / 100
  return jelly_beans, jars, crates


start_point = 10000
beans, jars, crates = secret_formula(start_point)

print "With a starting point of: %d" % start_point
print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)

start_point = start_point / 10

print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)



Exercise 25: Even More Practice

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)
  print 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)

def print_first_and_last_sorted(sentence):
  """Sorts the words then prints the first and last one."""
  words = sort_sentence(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.']
>>> sorted_words = ex25.sort_sentence(sentence)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> ex25.print_first_and_last(sentence)
All
wait.
>>> ex25.print_first_and_last_sorted(sentence)
All

who

※Notice that since we printed the first and last one, those words are now missing.

help(ex25)を実行すると以下が表示される:

NAME
    ex25

FILE
    /Users/fumiy/mystuff/ex25.py

FUNCTIONS
    break_words(stuff)
        This function will break up words for us.
    
    print_first_and_last(sentence)
        Prints the first and last words of the sentence.
    
    print_first_and_last_sorted(sentence)
        Sorts the words then prints the first and last one.
    
    print_first_word(words)
        Prints the first word after popping it off.
    
    print_last_word(words)
        Prints the last word after popping it off.
    
    sort_sentence(sentence)
        Takes in a full sentence and returns the sorted words.
    
    sort_words(words)
        Sorts the words.

help(ex25.break_words)を実行すると以下が表示される:

break_words(stuff)
    This function will break up words for us.

これらは """ で囲まれた documentation comments から取得されている。

最初に from ex25 import * を実行することで、いちいちex25. を書かなくてもよくなる。

>>> from ex25 import *
>>> sentence = "All good things come to those who wait."
>>> words = break_words(sentence)
>>> words
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> sorted_words = sort_words(words)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> print_first_word(words)
All
>>> words
['good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> print_last_word(words)
wait.
>>> words
['good', 'things', 'come', 'to', 'those', 'who']
>>> print_first_word(sorted_words)
All
>>> print_last_word(sorted_words)
who
>>> sorted_words
['come', 'good', 'things', 'those', 'to', 'wait.']
>>> sorted_words = sort_sentence(sentence)
>>> sorted_words
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
>>> print_first_and_last(sentence)
All
wait.
>>> print_first_and_last_sorted(sentence)
All
who

Exercise 26: Congratulations, Take a Test!

さて、前半の最後は他人の間違いだらけのコードをキレイに直すエクササイズ。
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)
    print 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)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)


print "Let's practice everything."
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""


print "--------------"
print poem
print "--------------"

five = 10 - 2 + 3 - 6
print "This should be five: %s" % five

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates


start_point = 10000
jelly_beans, jars, crates = secret_formula(start_point)

print "With a starting point of: %d" % start_point
print "We'd have %d beans, %d jars, and %d crates." % (jelly_beans, jars, crates)

start_point = start_point / 10

print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)


sentence = "All good things come to those who weight."

words = break_words(sentence)

sorted_words = sort_words(words)

print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)

sorted_words = sort_sentence(sentence)

print sorted_words

print_first_and_last(sentence)
print_first_and_last_sorted(sentence)


過去ログ:

SF Python Hack Night / Learn Python the Hard Way 1-6
Learn Python the Hard Way 続き 7-10
Learn Python the Hard Way 続き 11-15
PyLadies South Bay and Learn Python the Hard Way 続き 16-18
Learn Python the Hard Way 19-23

Disclaimer このブログは山崎富美の個人的なものです。ここで述べられていることは私の個人的な意見に基づくものであり、私の雇用者には一切の関係はありません。