Google's Python Class
いい物見つけたのでメモ。
Python 勉強のため、Python Cookbook を見て実際書いてみたりしているが、いまいち身につかないというか、やはり、自分の出来る範囲のことでやろうとしてしまう。受身な行動だと思いつつも、やはり課題を受けてやっていきたいと思ってしまう。
Google code ではAPIの情報や、ニュース、インストラクションなどが公開されているが、Google の社員が作ったチュートリアル、練習問題などがCreative Commons Attribution 2.5にて公開されている。
また、昔行ったと考えられる勉強会の様子もビデオ形式で見ることができ、それも非常にありがたい。
練習問題は以下の4つある。
Python Excercises
- Basic Exercises
- Baby Names Exercise
- Copy Special Exercise
- Log Puzzle Exercise
Basic Exercises の問題は、基本的な操作を確認するといった問題ばかりであるが、それ以降の練習問題はGoogle Code Jamのような、章立ての構成になっている。
とりあえず、Basic Exercisesの一部をやってみたので、晒してみたいと思う。
#!/usr/bin/env python -tt # Copyright 2010 Google Inc. # Licensed under the Apache License, Version 2.0 # http://www.apache.org/licenses/LICENSE-2.0 # Google's Python Class # http://code.google.com/edu/languages/google-python-class/ # A. donuts # Given an int count of a number of donuts, return a string # of the form 'Number of donuts: <count>', where <count> is the number # passed in. However, if the count is 10 or more, then use the word 'many' # instead of the actual count. # So donuts(5) returns 'Number of donuts: 5' # and donuts(23) returns 'Number of donuts: many' def donuts(count): # +++your code here+++ st = "Number of donuts: " word = str(count) if count < 10 else "many" return st + word # B. both_ends # Given a string s, return a string made of the first 2 # and the last 2 chars of the original string, # so 'spring' yields 'spng'. However, if the string length # is less than 2, return instead the empty string. def both_ends(s): # +++your code here+++ if len(s) >= 2: return s[:2] + s[-2:] else: return '' # C. fix_start # Given a string s, return a string # where all occurences of its first char have # been changed to '*', except do not change # the first char itself. # e.g. 'babble' yields 'ba**le' # Assume that the string is length 1 or more. # Hint: s.replace(stra, strb) returns a version of string s # where all instances of stra have been replaced by strb. def fix_start(s): # +++your code here+++ first_char = s[0] return first_char + s[1:].replace(first_char, '*') # D. MixUp # Given strings a and b, return a single string with a and b separated # by a space '<a> <b>', except swap the first 2 chars of each string. # e.g. # 'mix', pod' -> 'pox mid' # 'dog', 'dinner' -> 'dig donner' # Assume a and b are length 2 or more. def mix_up(a, b): # +++your code here+++ return b[:2] + a[2:] + ' ' + a[:2] + b[2:]