Last active
April 1, 2019 06:46
-
-
Save seven0525/18f1c2f9e3d6af8d09b78e969b22a2ae to your computer and use it in GitHub Desktop.
「自作Python100本ノック」15日目(やっと最終日:96本〜100本目) ref: https://qiita.com/ahpjop/items/2c0ba1decdb60a6e643b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 自分のやつ | |
def checkio(n): | |
list_n =[1,2,3,4,5,6,7,8,9] | |
if n < 10: | |
return n | |
factors = [] | |
for d in range(9,1, -1): | |
if n >= 10 and n % d == 0 and d != 1: | |
factors.append(d) | |
n = n / d | |
else: | |
continue | |
if n in list_n: | |
factors.append(n) | |
else: | |
return 0 | |
if len(factors) == 1: | |
return 0 | |
else: | |
factors = ['{:.0f}'.format(n) for n in factors] | |
factors.sort() | |
s = ''.join([str(n) for n in factors]) | |
print(s) | |
return int(float(s)) | |
checkio(20) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def double_substring(line): | |
s = [] | |
for i in range(len(line)-1) : | |
for j in range(i+1,len(line)+1) : | |
if line[i:j] in line[j:] : | |
s.append(len(line[i:j])) | |
if len(s)>0 : | |
return max(s) | |
else : | |
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def perfect_number(n): | |
y_num = [] | |
for i in range(2, n): | |
if n % i == 0: | |
y_num.append(i) | |
m = n // i | |
y_num.append(m) | |
y_num.append(1) | |
y_num = set(y_num) | |
sum_y = sum(y_num) | |
if sum_y == n: | |
print("完全数です") | |
else: | |
print("完全数ではありません") | |
perfect_number(6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hamming_distance(n, m): | |
return format(n ^ m, 'b').count('1') | |
hamming_distance(17, 117) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 正しい方 | |
def checkio(number): | |
ret = [] | |
for i in range(9, 1, -1): | |
while not number % i: | |
number /= i | |
ret.append(i) | |
if number == 1: | |
return int(''.join(map(str, sorted(ret)))) | |
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def convert_10(str_number, radix): | |
try: | |
return int(str_number, radix) | |
except ValueError: | |
return -1 | |
convert_10(3C5,16) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment