Skip to content

Instantly share code, notes, and snippets.

@yuheiomori
Created August 29, 2014 08:56
Show Gist options
  • Save yuheiomori/940cd0b239d8d37776ea to your computer and use it in GitHub Desktop.
Save yuheiomori/940cd0b239d8d37776ea to your computer and use it in GitHub Desktop.
Decode Numbers(CodeEval) in python 3.x
# coding=utf-8
import sys
import string
# A : 1
# B : 2
# C : 3
# ...
# Z : 26
mapping = dict([(c, str(i + 1)) for i, c in enumerate(string.ascii_uppercase)])
def count_decode_way(s):
if not s or len(s) == 1:
return 1
count = 0
char_length = 1
while True:
target = s[:char_length]
if len(target) != char_length:
break
if int(target) > 26:
break
count += count_decode_way(s[char_length:])
char_length += 1
return count
def main():
with open(sys.argv[1], "r") as f:
for line in f:
line = line.rstrip()
print(count_decode_way(line))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment