Help us understand the problem. What is going on with this article?

yhpgF06 - ツリーの中の数 をPythonで解いてみる

More than 3 years have passed since last update.

オフラインで参加したツリーの中の数をPythonで解きました。

http://nabetani.sakura.ne.jp/hena/ordf06numit/

時間内に解けずに持ち帰って解く。
家に帰ってからは10分くらいで成功にはなった。

当日は計算時間が話題になりましたが、
私の実行環境ではテストコードがすべて終わるまで10分弱程度かかります。

yhpgf06.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# https://yhpg.doorkeeper.jp/
# http://nabetani.sakura.ne.jp/hena/ordf06numit/
from functools import reduce

def solve(data):
    data = data.split(",")

    field = []
    tmp = [(int(data[0]))]  #root
    while len(tmp) > 0:
        field.append(tmp)
        tmp = createChild(tmp)
    # print(field)

    flattern = reduce(lambda x,y:x+y,field)
    # print(flattern)
    count = flattern.count(int(data[1]))
    return str(count)

def createChild(field):
    data = []
    for i in field:
        c =child1(i)
        if c > 0:
            data.append(c)
        c =child2(i)
        if c > 0:
            data.append(c)

    return data




def child1(target):
    return int(((target) / 2) - 10)

def child2(target):
    return int((target * 2) / 3)


def test(data, expect):
    result = solve(data)
    if result == expect:
        print("成功: " + result + "/" + expect )
    else:
        print("失敗: " + result + "/" + expect )

test( "123,4", "5" );
test( "1,1", "1" );
test( "2,1", "1" );
test( "3,3", "1" );
test( "19,5", "1" );
test( "69,5", "3" );
test( "88,9", "2" );
test( "1,100", "0" );
test( "100,4", "4" );
test( "101,9", "0" );
test( "456,7", "7" );
test( "567,8", "12" );
test( "756,10", "10" );
test( "789,10", "12" );
test( "896,29", "2" );
test( "7764,6", "664" );
test( "1234,56", "3" );
test( "8563,29", "35" );
test( "12345,67", "10" );
test( "72927,51", "263" );
test( "71441,145", "22" );
test( "123456,78", "397" );
test( "123456,789", "1" );
test( "592741,216", "55" );
test( "913826,584", "81" );
test( "1234567,89", "2293" );
test( "10000000,1", "19383507" );
test( "12345678,9", "3567354" );
test( "6215879,358", "2907" );
test( "12345678,90", "79419" );
test( "5745432,1032", "1287" );

sugman_a
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした