Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@sugman_a

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

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

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" );

``````
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
0
Help us understand the problem. What are the problem?