Cliquez ici pour les questions et réponses des autres http://qiita.com/Nabetani/items/c516875b13a4d282affe
J'ai décidé de courir en arrière par derrière.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from compiler.ast import flatten
a, b, c = 0, 1, 2
reversed_branches = (
# a b c
( (a, ), (a,b), (b,c) ), # course 1
( (a, ), (b,c), (a,c) ), # course 2
( (a,b), (b, ), (a,c) ), # course 3
( (a,b), (b,c), (c, ) ), # course 4
( (a,c), (b, ), (b,c) ), # course 5
( (a,c), (a,b), (c, ) ), # course 6
( (a, ), ( ), (c, ) ), # course 7
( ( ), (b, ), (c, ) ), # course 8
( (a, ), (b, ), ( ) ), # course 9
)
def back_course(course, tracks):
backed_tracks = (reversed_branches[course][track] for track in tracks)
return set(flatten(backed_tracks))
def solve(courses):
reversed_courses = (int(course)-1 for course in courses[::-1])
tracks = (a, b, c)
for course in reversed_courses:
tracks = back_course(course, tracks)
return ''.join(chr(ord('a')+track) for track in sorted(tracks)) or '-'
def test():
testdata = (
( 0, "1728398", "bc" ),
( 1, "789", "-" ),
( 2, "274", "ac" ),
( 3, "185", "abc" ),
( 4, "396", "ab" ),
( 5, "1278", "abc" ),
( 6, "7659832", "a" ),
( 7, "178", "bc" ),
( 8, "189", "ab" ),
( 9, "197", "a" ),
( 10, "278", "ac" ),
( 11, "289", "bc" ),
( 12, "297", "a" ),
( 13, "378", "ac" ),
( 14, "389", "b" ),
( 15, "397", "ab" ),
( 16, "478", "c" ),
( 17, "489", "bc" ),
( 18, "497", "ab" ),
( 19, "578", "bc" ),
( 20, "589", "b" ),
( 21, "597", "ac" ),
( 22, "678", "c" ),
( 23, "689", "ab" ),
( 24, "697", "ac" ),
( 25, "899", "b" ),
( 26, "7172", "ac" ),
( 27, "54787", "bc" ),
( 28, "83713", "bc" ),
( 29, "149978", "-" ),
( 30, "159735", "abc" ),
( 31, "1449467", "abc" ),
( 32, "9862916", "b" ),
( 33, "96112873", "ab" ),
( 34, "311536789", "-" ),
( 35, "281787212994", "abc" ),
( 36, "697535114542", "ac" ),
)
for no, data, correct in testdata:
answer = solve(data)
print '%s #%d "%s" "%s" -> "%s"' % (('NG!!','OK ')[answer == correct], no, data, correct, answer)
if __name__ == '__main__':
test()
Résultat d'exécution
OK #0 "1728398" "bc" -> "bc"
OK #1 "789" "-" -> "-"
OK #2 "274" "ac" -> "ac"
OK #3 "185" "abc" -> "abc"
OK #4 "396" "ab" -> "ab"
OK #5 "1278" "abc" -> "abc"
OK #6 "7659832" "a" -> "a"
OK #7 "178" "bc" -> "bc"
OK #8 "189" "ab" -> "ab"
OK #9 "197" "a" -> "a"
OK #10 "278" "ac" -> "ac"
OK #11 "289" "bc" -> "bc"
OK #12 "297" "a" -> "a"
OK #13 "378" "ac" -> "ac"
OK #14 "389" "b" -> "b"
OK #15 "397" "ab" -> "ab"
OK #16 "478" "c" -> "c"
OK #17 "489" "bc" -> "bc"
OK #18 "497" "ab" -> "ab"
OK #19 "578" "bc" -> "bc"
OK #20 "589" "b" -> "b"
OK #21 "597" "ac" -> "ac"
OK #22 "678" "c" -> "c"
OK #23 "689" "ab" -> "ab"
OK #24 "697" "ac" -> "ac"
OK #25 "899" "b" -> "b"
OK #26 "7172" "ac" -> "ac"
OK #27 "54787" "bc" -> "bc"
OK #28 "83713" "bc" -> "bc"
OK #29 "149978" "-" -> "-"
OK #30 "159735" "abc" -> "abc"
OK #31 "1449467" "abc" -> "abc"
OK #32 "9862916" "b" -> "b"
OK #33 "96112873" "ab" -> "ab"
OK #34 "311536789" "-" -> "-"
OK #35 "281787212994" "abc" -> "abc"
OK #36 "697535114542" "ac" -> "ac"
Recommended Posts