読者です 読者をやめる 読者になる 読者になる

牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

Pythonで干支を求めてみた。

Python

Rubyレシピブックに載ってたものをPythonで書いてみた。

code

*1

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

celestial_stem = ["庚","辛","壬","癸","甲","乙","丙","丁","戊","己"]
earthly_branch = ["申","酉","戌","亥","子","丑","寅","卯","辰","巳","午","未"]

def stem_and_branch(year):
    """
    >>> print stem_and_branch(1868)
    戊辰
    >>> print stem_and_branch(1966)
    丙午
    """
    return celestial_stem[year % 10] + earthly_branch[year % 12]

def _test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    _test()

    #python hoge.py -v

test (shell)

$python hoge.py -v
Trying:
    print stem_and_branch(1868)
Expecting:
    戊辰
ok
Trying:
    print stem_and_branch(1966)
Expecting:
    丙午
ok
2 items had no tests:
    __main__
    __main__._test
1 items passed all tests:
   2 tests in __main__.stem_and_branch
2 tests in 3 items.
2 passed and 0 failed.
Test passed.

doctest良いよね(・∀・)b


*1:訂正20120111: add doctest