How to write offline real-time Solving E05 problems with Python

Click here for questions and answers from others http://qiita.com/Nabetani/items/c516875b13a4d282affe

I decided to run backwards from behind.

#!/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()

Execution result


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

How to write offline real-time Solving E05 problems with Python
Offline real-time how to write Python implementation example of E14
Offline real-time how to write E11 ruby and python implementation example
Offline real-time how to write Python implementation example of E15 problem
How to write offline real time Solve E04 problems in Python
Answer to "Offline Real-time How to Write E13 Problem"
How to write offline real time Solve F01 problems with Python
The 16th offline real-time how to write problem was solved with Python
How to write offline real time I tried to solve E11 with python
The 16th offline real-time how to write reference problem to solve with Python
The 19th offline real-time how to write reference problem to solve with Python
The 15th offline real-time how to write problem was solved with python
How to write offline real time I tried to solve E12 with python
The 15th offline real-time I tried to solve the problem of how to write with python
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
Answer to "Offline real-time how to write F02 problem"
Answer to "Offline Real-time How to Write F01 Problem"
The 17th offline real-time how to write reference problem implemented in Python
How to write a Python class
Python: How to use async with
[Python] Write to csv file with Python
How to get started with Python
How to use FTP with Python
How to write offline real time I tried to solve the problem of F02 with Python
How to write Python document comments (Docstrings)
Precautions when solving DP problems with Python
Answer to "Offline Real-Time Writing E12 Problem"
How to do portmanteau test with python
How to display python Japanese with lolipop
How to enter Japanese with Python curses
[Python] How to deal with module errors
How to write Ruby to_s in Python
How to install python3 with docker centos
[Introduction to Python] How to write a character string with the format function
How to upload with Heroku, Flask, Python, Git (4)
How to read a CSV file with Python 2/3
How to enjoy programming with Minecraft (Ruby, Python)
[REAPER] How to play with Reascript in Python
How to do multi-core parallel processing with python
Strategy on how to monetize with Python Java
[Python] How to draw multiple graphs with Matplotlib
[Python] How to read excel file with pandas
How to crop an image with Python + OpenCV
[Write to map with plotly] Dynamic visualization with plotly [python]
How to specify attributes with Mock of python
How to measure execution time with Python Part 1
How to use tkinter with python in pyenv
Write CSV data to AWS-S3 with AWS-Lambda + Python
[Python] How to handle Japanese characters with openCV
I want to write to a file with Python
[Python] How to compare datetime with timezone added
How to measure execution time with Python Part 2
Introduction to Python Mathematical Optimization Solving junior high school math problems with pulp
How to install Python
How to install python
How to convert / restore a string with [] in python
How to add help to HDA (with Python script bonus)
[Python] How to draw a line graph with Matplotlib
How to write string concatenation in multiple lines in Python
How to scrape image data from flickr with python