Created
December 31, 2012 20:05
-
-
Save avocado3/4422318 to your computer and use it in GitHub Desktop.
PythonでJAN/EANコードのチェックディジット(モジュラス10ウェイト3)の計算
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
def recurse(numbers, sum_val, weight): | |
if numbers == []: | |
return sum_val | |
sum_val += numbers[-1] * weight | |
return recurse(numbers[:-1], sum_val, int(3/weight)) | |
def cd_mod10_w31(numbers): | |
sum_val = recurse(numbers, 0, 3) | |
cd = 10 - (sum_val % 10) | |
return cd if cd != 10 else 0 | |
s = '123456789012' | |
print(s + str(cd_mod10_w31([int(i) for i in s]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2013年の書き初め。巳年なのでPython書いてみますた。
何故こんなコードを書いたかは考えてはいけない。
一昨年はずっとPythonやってたはずなのに、去年1年Rubyに浮気してもうすっかりとりこになってしまい、Pythonのことは忘れてしまった。
あまり関係ないけど、一応Python3系を意識して書いてる(つもり)
はじめは整数のリストの後ろから2つずつ取り出して3,1を掛ける方法を考えてたけど、すっきり書けなさそうだったので、ちょっと考えて、再帰でやる方法を思いついた。
再帰を使ったコード書いたの、実は初めてかもしれない…
ところで、あまり意識せずに思いついたままに書いたけど、末尾再帰ってこういうので合っているんだろうか?