Skip to content

Instantly share code, notes, and snippets.

@yue4u
Last active October 30, 2019 20:14
Show Gist options
  • Select an option

  • Save yue4u/a6e6059e32cd99fc79ad698a811cf8c9 to your computer and use it in GitHub Desktop.

Select an option

Save yue4u/a6e6059e32cd99fc79ad698a811cf8c9 to your computer and use it in GitHub Desktop.
"""
問9.1,2,4,5,7,8の数値を組み合わせた数値("124578"など)から、
一桁のある数値をかけると
答えが99や999などの"9"のみの数字の連続で表すことのできる数値を
検出して出力するプログラムを実装せよ
"""
# 推理 (プログラムを書く必要がない)
"""
理由:
STEP1:
"9"のみの数字の連続で表す => 偶数ではない 残り: 1, 3, 5, 7, 9
1,2,4,5,7,8の数値を組み合わせた数値 => 1, 3, 5, 9は不可能
合わせて: 7のみ
STEP2:
最小124578、最大875421
*7の範囲は 872046~6127947
よって、範囲内なのは 999999 のみ
STEP3:
999999 / 7 = 142857
"""
# permutations の紹介はここ -> https://docs.python.org/3/library/itertools.html#itertools.permutations
# 日本語(オススメしない) -> https://docs.python.org/ja/3/library/itertools.html#itertools.permutations
from itertools import permutations
# 順列
[print(x) for x in [int("".join(n)) for n in permutations(list('124578'))] if any(len(i) == 1 and i[0] == '9' for i in [list(set(str(x * j))) for j in range(2, 10)])]
# あるいは
[print(x) for x in map(lambda n: int("".join(n)) if n else 0, permutations(list('124578'))) if any(len(i) == 1 and i[0] == '9' for i in map(lambda j: list(set(str(x * j))), range(2, 10)))]
# 組み合わせ順列
[print(x) for x in map(lambda n: int("".join(n)) if n else 0, [p for k in range(7) for p in permutations(list('124578'),k)]) if any(len(i) == 1 and i[0] == '9' for i in map(lambda j: list(set(str(x * j))), range(2, 10)))]
# 一行で書かない
for x in [int("".join(n)) for n in permutations(list('124578'))]:
for i in [list(set(str(x * j)))for j in range(2, 10)]:
if len(i) == 1 and i[0] == '9':
print(x)
for x in map(lambda n: int("".join(n)), permutations(list('124578'))):
for i in map(lambda j: list(set(str(x * j))), range(2, 10)):
if len(i) == 1 and i[0] == '9':
print(x)
# map と lambda を使わない
for x in permutations(list('124578')):
y = int("".join(x))
for i in range(2, 10):
j = list(set(str(y * i)))
if len(j) == 1 and j[0] == '9':
print(f'{y} * {i} = {y * i}')
"""
#関数f(n)は
nが偶数のときf(n)=2n+3+f(n-1)、
nが奇数のときf(n)=f(n-1)*f(n-2)、
nが0以下のときは3であるとする。
この際に答えが100以上になる最小のnはなにか。
算出をするプログラムを実装し求めよ
"""
f = [
lambda n: 2 * n + 3 + f[1](n - 1) if n >= 0 else 3,
lambda n: f[0](n - 1) * f[1](n - 2) if n > 0 else 3
]
n = 0
while 1:
if (f[n % 2](n) >= 100):
print(n)
break
n += 1
"""
#モールス信号をアルファベットへ変換する関数を実装し、
下記の文言を英語へ変換せよ。
なお、モールス信号はこちら
(https://ja.wikipedia.org/wiki/%E3%83%A2%E3%83%BC%E3%83%AB%E3%82%B9%E7%AC%A6%E5%8F%B7)
の欧文モールス信号に準拠し、
区切り文字は" "(半角スペース)である。
"""
m = {
'.-': 'a',
'-...': 'b',
'-.-.': 'c',
'-..': 'd',
'.': 'e',
'..-.': 'f',
'--.': 'g',
'....': 'h',
'..': 'i',
'.---': 'j',
'-.-': 'k',
'.-..': 'l',
'--': 'm',
'-.': 'n',
'---': 'o',
'.--.': 'p',
'--.-': 'q',
'.-.': 'r',
'...': 's',
'-': 't',
'..-': 'u',
'...-': 'v',
'.--': 'w',
'-..-': 'x',
'-.--': 'y',
'--..': 'z',
'-----': '0',
'.----': '1',
'..---': '2',
'...--': '3',
'....-': '4',
'.....': '5',
'-....': '6',
'--...': '7',
'---..': '8',
'----.': '9'
}
decode = lambda code: "".join([m[c] for c in code.split(' ')])
## テスト
assert decode('.--. -.-- - .... --- -.') == 'python'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment