[PYTHON] Answer to "Offline real-time writing F04 problem"

Answer to "Offline Real-Time Writing F04 Problem-Division of Regular Octagons" http://nabetani.sakura.ne.jp/hena/ordf04octsp/

This time it will be held at the venue It was all 40 minutes to Success. After that, rework

Pointed out below

  1. Sorry for the first table
  2. Whether the result of "/2" is floating point or integer depends on the version of Python (running at 2.7 in the field) ↓ I tried about 2.
Python 2.7.13 (default, Apr 12 2017, 21:12:30) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.41)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
0
>>> 
Python 3.6.0 |Anaconda 4.3.1 (x86_64)| (default, Dec 23 2016, 13:19:00) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
0.5
>>>

By the way, if it is as follows, it will work as intended regardless of the Python version. Called floor division or integer division

>>> 1//2
0

The following on-site answers

main.py


polygon_table = {}
polygon_table[0] = ''
polygon_table[1] = '3'
polygon_table[2] = '4'
polygon_table[3] = '5'
polygon_table[4] = '5'
polygon_table[5] = '7'
polygon_table[6] = '8'
polygon_table[7] = '9'

def get_startline(data):
    start = 128
    while True:
        if data >= start:
            return start
        else:
            start /= 2

def detect_poly(data):
    start = get_startline(data)
    triangle_count = []
    count = 0

    while start != 0:
        if data >= start:
            data -= start
            triangle_count.append(count)
            count = 0
        else:
            start /= 2
            count += 1

    polys = []
    for t in triangle_count:
        polys.append(polygon_table[t])

    remaining = 8 - sum(triangle_count)
    if remaining > 0:
        polys.append(polygon_table[remaining])

    polys.sort()
    return ''.join(polys)

## test logic
class Result:
    def __init__(self):
        self.success = 0
        self.fail = 0

RESULT = Result()
RESULT.success = RESULT.fail = 0

def test(data, expect):
#    print("actual:" + detect_poly(int(data)) + " expected:" + expect)
    if detect_poly(int(data)) == expect:
        RESULT.success += 1
    else:
        RESULT.fail += 1

## ---- tests ----

test("165", "3445" )
test("80", "48" )
test("255", "33333333" )
test("68", "55" )
test("200", "355" )
test("82", "455" )
test("164", "455" )
test("73", "455" )
test("146", "455" )
test("37", "455" )
test("74", "455" )
test("148", "455" )
test("41", "455" )
test("38", "355" )
test("76", "355" )
test("152", "355" )
test("49", "355" )
test("98", "355" )
test("196", "355" )
test("137", "355" )
test("19", "355" )
test("20", "48" )
test("9", "57" )
test("209", "3345" )
test("121", "33345" )
test("239", "3333334" )
test("26", "347" )
test("111", "333344" )
test("95", "333344" )
test("85", "4444" )
test("24", "39" )
test("97", "347" )
test("234", "33444" )
test("59", "33345" )
test("187", "333344" )
test("34", "55" )
test("249", "333335" )
test("43", "3445" )
test("143", "33335" )
test("28", "338" )
test("79", "33345" )
test("173", "33444" )
test("55", "33345" )
test("77", "3445" )
test("35", "355" )
test("153", "3355" )
test("30", "3337" )
test("228", "3355" )
test("177", "3345" )
test("162", "445" )
test("184", "3345" )

## --------------

print("Success: {0.success}, Fail: {0.fail}".format(RESULT))

Recommended Posts

Answer to "Offline real-time writing F04 problem"
Answer to "Offline real-time writing F05 problem"
Answer to "Offline Real-Time Writing E12 Problem"
Answer to "Offline real-time how to write F02 problem"
Answer to "Offline Real-time How to Write F01 Problem"
Answer to "Offline Real-time How to Write E13 Problem"
The 18th offline real-time writing problem in Python
The 19th offline real-time writing problem in Python
The 14th offline real-time writing reference problem with Python
13th Offline Real-time How to Solve Writing Problems in Python
The twelfth offline real-time writing reference problem. Implementation by python
The 15th offline real-time how to write reference problem in Python
The 17th Offline Real-time How to Solve Writing Problems in Python
The 10th offline real-time writing reference problem. Implementation example by Python.
Offline real-time how to write Python implementation example of E15 problem
The 11th offline real-time writing reference problem. Implementation example by 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 Splash memory overuse problem (draft)
The 16th offline real-time how to write problem was solved with Python
The 17th offline real-time how to write reference problem implemented in 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
The 15th offline real-time I tried to solve the problem of how to write with python
20th Offline Real-time How to Write Problems in Python
[Python] Try to read the cool answer to the FizzBuzz problem
How to write offline real time I tried to solve the problem of F02 with Python
Offline real-time how to write Python implementation example of E14
How to write offline real-time Solving E05 problems with Python
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python