謎言語使いの徒然

適当に気になった技術や言語を流すブログ。

プログラム脳を鍛える数学パズル on python3

追記: とりま gist に置いてみた (2017/06/27

お題はこれで、目標は1日1個以上。

…ダレ無いといいけど…。

def is_int_kai(num):
    v = str(num)
    rev = v[::-1]
    return v == rev


def is_oct_kai(num):
    v = oct(num)[2:]
    rev = v[::-1]
    return v == rev


def is_bin_kai(num):
    v = bin(num)[2:]
    rev = v[::-1]
    return v == rev

current = 10

while True:
    current += 1
    if is_int_kai(current) and is_oct_kai(current) and is_bin_kai(current):
        print('answer:' + str(current))
        break
from functools import reduce

positions = [
    [1], [2], [3],
    [2, 1], [3, 2], [3, 1],
    [3, 2, 1]
]


def split(str, pos, stack):
    if len(pos) == 0:
        stack.append(str)
        return stack
    else:
        i = pos[0]
        stack.append(str[i:])
        return split(str[:i], pos[1:], stack)


def multiple(l):
    return reduce(lambda a, b: a * b, l)


def is_kaibun(left, right):
    return left == right[::-1]

for cur in range(1000, 9999):
    strCur = str(cur)
    for pos in positions:
        splits = split(strCur, pos, [])
        result = multiple(map(lambda v: int(v), splitted))
        if is_kaibun(strCur, str(result)):
            print(str(splits) + " = " + str(result))
            print("success: " + strCur)

gist にでも置くかね?