0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「オフラインリアルタイムどう書くF04の問題」の解答

Posted at

「オフラインリアルタイムどう書くF04の問題 - 正八角形の分割」の解答
http://nabetani.sakura.ne.jp/hena/ordf04octsp/

今回は会場で実施
全部Successまで40分でした。
その後手直し実施

指摘は以下

  1. 最初のテーブルが残念
  2. "/2"の結果が浮動小数点か整数かがPythonのバージョンにより異なる(現場では2.7で実行)
    ↓ 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
>>>

ちなみに以下ならPythonのバージョンにかかわらず意図通りの動作となる。
floor division もしくは integer division と呼ばれる

>>> 1//2
0

以下現場での解答

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))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?