2014年7月23日水曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 4(Working with Text)、4.7(Exercises) 8.を解いてみる。

4.7(Exercises) 8.

コード(BBEdit)

sample8.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

def repeat(s, n):
    """ (str, int) -> str
    Return s repeated n times, if n is negative, return the empty string.
    >>> repeat('yes', 4)
    'yesyesyesyes'
    >>> repeat('no', 0)
    ''
    >>> repeat('no', -2)
    ''
    >>> repeat('yesnomaybe', 3)
    'yesnomaybeyesnomaybeyesnomaybe'
    """
    return s * n

if __name__ == '__main__':
    print(repr(repeat('yes', 4)))
    print(repr(repeat('no', 0)))
    print(repr(repeat('no', -2)))
    print(repr(repeat('yesnomaybe', 3)))

入出力結果(Terminal, IPython)

$ ./sample8.py 
'yesyesyesyes'
''
''
'yesnomaybeyesnomaybeyesnomaybe'
$

0 コメント:

コメントを投稿