Last active
May 24, 2018 02:58
-
-
Save seven0525/0ca9dbba8b2c3afa7f640fd0d14e0a03 to your computer and use it in GitHub Desktop.
「自作Python100本ノック」11日目(71本〜80本目) ref: https://qiita.com/ahpjop/items/199d3a204f47588b40b8
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 good_number(n): | |
n = str(n) | |
if n[0] == n[1] == n[2] or n[1] == n[2] == n[3]: | |
print("Yes, it is a good number") | |
else: | |
print("No, it isn't good nomber") | |
good_number(1116) |
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 lucas(n): | |
n = int(n) | |
if n == 0: | |
return 2 | |
elif n == 1: | |
return 1 | |
else: | |
return lucas(n - 1) + lucas(n - 2) | |
lucas(4) |
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 put_formula(n): | |
a,b,c,d = list(str(n)) | |
sign = "+-" | |
aws_list = [] | |
for i in range(2): # 1つ目の記号 | |
for j in range(2): # 2つ目の記号 | |
for k in range(2): # 3つ目の記号 | |
if eval(a+sign[i]+b+sign[j]+c+sign[k]+d) == 7: | |
aws = (str(a+sign[i]+b+sign[j]+c+sign[k]+d)+"=7") | |
aws_list.append(aws) | |
print(aws) | |
if len(aws_list) == 0: | |
print("impossible") | |
put_formula(1161) |
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
import requests | |
from bs4 import BeautifulSoup | |
r = requests.get('https://hikkoshi.suumo.jp/sankaku/') | |
soup = BeautifulSoup(r.text, 'html.parser') | |
titleTags = soup.select('a') | |
names = [] | |
for titleTag in titleTags: | |
name = titleTag.text.strip() | |
names.append(name) | |
#distinct_names = list(set(names)) | |
names_uniq = [] | |
for d_name in names: | |
if d_name not in names_uniq: | |
names_uniq.append(d_name) | |
print(names_uniq) |
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 first_word(text: str) -> str: | |
if text.find(",")>= 0: | |
text2= text.replace(',', ' ') | |
if text.find(".")>=0: | |
text2=text.replace('.', ' ') | |
texts = text2.split() | |
return texts[0] | |
first_word("..... greetings, friends") |
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 second_index(text, symbol): | |
count = 0 | |
for i in range(len(text)): | |
if text[i] == symbol: | |
count += 1 | |
if count == 2: | |
return i | |
return None | |
second_index("I am a good student but you are not a good student", "g") |
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
#株名と株価の辞書 | |
stock_dict = { | |
'CAC': 10.0, | |
'ATX': 390.2, | |
'WIG': 1.2 | |
} | |
#1番高いストックを表示する関数 | |
def best_stock(data): | |
max = 0 | |
code = "" | |
for stock in data: | |
print(stock) | |
if data[stock] > max: | |
max = data[stock] | |
code = stock | |
return code | |
best_stock(stock_dict) |
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 popular_words(text: str, words: list) -> dict: | |
text = text.lower().split() | |
count_list = [] | |
for word in words: | |
count = text.count(word) | |
count_list.append(count) | |
aws = dict(zip(words, count_list)) | |
return aws |
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 check_osxs(result): | |
judge = "D" | |
for i in range(3): | |
if result[i][0] == result[i][1] == result[i][2] != ".": | |
judge = result[i][0] | |
elif result[0][i] == result[1][i] == result[2][i] != ".": | |
judge = result[0][i] | |
if result[0][0] == result[1][1] == result[2][2] != ".": | |
judge = result[0][0] | |
elif result[0][2] == result[1][1] == result[2][0] != ".": | |
judge = result[0][2] | |
return judge | |
check_osxs([ | |
"X.O", | |
"XX.", | |
"XOO"]) |
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 safe_pawns(pawns): | |
pwans = list(pawns) | |
cols = {"a":0,"b":1,"c":2,"d":3,"e":4,"f":5,"g":6,"h":7} | |
s_pwans = [] | |
for i in pawns: | |
target = [] | |
for j in pwans: | |
if int(i[1])+1 == int(j[1]): | |
target.append(j) | |
for k in target: | |
if abs(cols.get(k[0]) - cols.get(i[0])) == 1: | |
s_pwans.append(k) | |
if s_pwans.count(k) > 1: | |
s_pwans.pop() | |
return len(s_pwans) | |
aws = {"b4","c4","d4","e4","f4","g4","e3"} | |
safe_pawns(aws) |
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
checkio([ | |
"X.O", | |
"XX.", | |
"XOO"]) == "X" | |
checkio([ | |
"OO.", | |
"XOX", | |
"XOX"]) == "O" | |
checkio([ | |
"OOX", | |
"XXO", | |
"OXX"]) == "D" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment