日付セット「次の日 / 翌営業日 / 日付のフォーマット / 年月日の順序 Python3編」回答

翌営業日は他の問題で使うので保存。

翌営業日

# coding: utf-8
# Your code here!
'''
"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
'''
s = input().split( )
m = int(s[0])
d = int(s[1])
w = str(s[2])

if (m==2):
    md = 28
elif m==4 or m==6 or m==9 or m==11:
    md = 30
else :
    md = 31
    
    
if w=="SUN":
    n=1
elif w=="SAT":
    n=2
elif w=="FRI":
    n=3
else :
    n=1

#day
if d+n > md :
    res_d = d+n-md
    res_m = m+1
else :
    res_d = d+n
    res_m = m
#month
if res_m > 12:
    res_m = res_m - 12

    
print(str(res_m)+"月"+str(res_d)+"日")

次の日

# coding: utf-8
# Your code here!
'''
ただし、各月の日数は以下のように決まることに注意してください。
・4, 6, 9, 11月は30日
・2月は閏年ならば29日、そうでなければ28日
・それ以外の月は31日
ただし、閏年は次のような年のことをいいます。
・西暦が4で割り切れる年は閏年
・ただし、100で割り切れる年は平年
・ただし、400で割り切れる年は閏年
'''
s = input().split( )
y = int(s[0])
m = int(s[1])
d = int(s[2])
uru=0

if y%400 ==0 :
    uru = 1
elif (y%4==0) and (y%100 != 0):
    uru = 1

if (m==2)and(uru==1):
    md = 29
elif (m==2)and(uru!=1):
    md = 28
elif m==4 or m==6 or m==9 or m==11:
    md = 30
else :
    md = 31
#day
if d+1 > md :
    res_d = d+1-md
    res_m = m+1
else :
    res_d = d+1
    res_m = m
#month
if res_m > 12:
    res_m = res_m - 12
    res_y = y + 1
else :
    res_y = y
    
print(str(res_y)+" "+str(res_m)+" "+str(res_d))


阿保みたいに落っこちた「日付のフォーマット

# coding: utf-8
# Your code here!
line = input().split("/")
#print(line)

#str.isdecimal()

if ( len(line)==3 and 
         line[0].isdecimal() and 
         line[1].isdecimal() and 
         line[2].isdecimal() and
         int(line[1])<=12 and
         int(line[2])<=31 and
         int(line[1])>=1 and
         int(line[2])>=1 and
         len(line[0])==4 and 
         len(line[1])==2 and 
         len(line[2])==2 ):
    print("Yes")
else:
    print("No")

年月日の順序
デバッグできなくて何回もやり直しした。

line = input().split("/")
res = ["","",""]
#print(res)
y=0
m=0
d=0
Error = False
for i in line:
    if len(str(i))==4 and int(i)>0:
        res[line.index(i)] = "YYYY"
        y += 1
    else :
        if int(i)>31 or int(i)<=0:
            Error = True
        elif int(i)>12 and int(i)<=31:
            d +=1
            res[line.index(i)] = "DD"
        else :
            res[line.index(i)] = "MM"

#結果の出力
if y!=1 or d >=2 or Error:
    print("no answer")
elif d==1 :
    print(res[0]+"/"+res[1]+"/"+res[2])
else :
    print("many answers")

「野球の審判 Python3編」回答

# coding: utf-8
# Your code here!
'''
ストライクが 1 〜 2 つたまったとき → "strike!"
ストライクが 3 つたまったとき → "out!"
ボールが 1 〜 3 つたまったとき → "ball!"
ボールが 4 つたまったとき → "fourball!"
'''
num = int(input())
ball = 0
strike = 0

for i in range(num):
    word = str(input())
    if word =="ball":
        ball += 1
        if ball ==4:
            ball == 0
            print("fourball!")
        else :
            print("ball!")
    elif word == "strike":
        strike += 1
        if strike == 3:
            strike = 0
            print("out!")
        else : 
            print("strike!")
            
    else:
        print("Error")

「宝くじ Python3編」回答

123456[1:3] で 12 が出てくる
654321[-3:-1] で 32 が出てくる
654321[-3:] で 321 が出てくる
参考:
note.nkmk.me

# coding: utf-8
# Your code here!
hit = input()
num = int(input())
for i in range(num):
    each = input()
    if hit == each:
        print("first")
    elif (int(hit)-1 == int(each)) or (int(hit)+1 == int(each)):
        print("adjacent")
    elif hit[-4:] == each[-4:]:
        print("second")
    elif hit[-3:] == each[-3:]:
        print("third")
    else:
        print("blank")

「検索履歴 Python3編」回答

remove でリスト中の特定の要素を削除できる
for i in reversed(line)
でlineの要素を逆順に出力できる

# coding: utf-8
# Your code here!

num = int(input())
line = []
for i in range(num):
    word = input()
    if line.count(word) == 0:
        line.append(word)
    else :
        line.remove(word)
        line.append(word)
        
#print(line)

for i in reversed(line):
    print(i)

Bランクレベルアップメニュー「占い」回答

問題はこちら。
https://paiza.jp/works/mondai/prob60/fortune_telling_9?language_uid=python3&t=993db344ac4c2f9435c72067e674f99a

辞書使うみたいなのですが、二次元リストでもよくない?っていう。二次元リスト大好きマンになってしまって辞書の使い方ようわからんのですわ。あと、forを2個重ねるやつめっちゃ使いません?色んな問題で。

# coding: utf-8
# Your code here!
per_num = int(input())
per = []
for i in range(per_num):
    per.append(input().split( ))
#print(per)

res_num = int(input())
res = []
for i in range(res_num):
    res.append(input().split( ))
#print(res)

out = []
for i in range(per_num):
    for j in range(res_num):
        if per[i][1]==res[j][0]:
            print(per[i][0] + " " + res[j][1])

Cランクレベルアップメニュー「文字列」私の回答

問題はこちら。
https://paiza.jp/works/mondai/c_rank_level_up_problems/c_rank_string_boss?language_uid=python3
なーんか長い気がするのですが。記録までに。

# coding: utf-8
# Your code here!
num = int(input())

for i in range(num):
    #timeに現在時刻を、addに加える時間をリストとして記録
    s = input().split( )
    line = []
    g = s[0].split(":")
    g1 = int(g[0])
    g2 = int(g[1])
    s1 = int(s[1])
    s2 = int(s[2])
    time = [g1 , g2]
    add = [s1 , s2]
    #print(time)
    #print(add)
    
    #分の計算(uphは繰上りを表す)
    if time[1]+add[1] >= 60:
        res_min = time[1] + add[1] - 60
        uph = int(1)
    else :
        res_min = time[1] + add[1]
        uph = int(0)
        
    #時の計算
    if time[0] + add[0] + uph >= 24:
        res_hour = time[0] + add[0] + uph - 24
    else :
        res_hour = time[0] + add[0] + uph
    
    #resに計算結果を格納
    res = []
    res.append(res_hour)
    res.append(res_min)
    #print(res)
        
    out = []
    #整形して出力。(1桁の数字の先頭に0を付ける)
    for j in range(2):
        if res[j] < 10:
            out.append("0"+str(res[j]))
        else :
            out.append(str(res[j]))
            
    print(out[0] + ":" + out[1])

Pythonで(Numpyとか使わずに)行列積の計算をしてみる

Python習いたての大学生がやりたがりそうなこと、第4位「行列計算」。一応、自力で書いてみました。
行列A,Bを二次元リストとして、その行列積を計算します。行列積が定義できないときにはその旨を表示、定数×行列 の計算もできるようにしています。
きっと色々がばがばなので....改善したら適宜編集します。rangeの書き方これじゃなくていいですね
A,Bが行列じゃないときに警告文を出すようにしたいのですが、どうすりゃいいかなあ。
ご指摘等ありましたら優しく@aagoon545まで。

A=[[1 for i in range(3)] for j in range(2)]
B=[[1 for k in range(4)] for l in range(3)]

line_A = len(A)
raw_A = len(A[0])
line_B = len(B)
raw_B = len(B[0])

print("A is (" + str(line_A) + "," + str(raw_A) +") matrix." )
print("B is (" + str(line_B) + "," + str(raw_B) +") matrix." )

#そもそもAが行列でないとき
#そもそもBが行列でないとき

#Aが1*1行列の時
if line_A == 1 and raw_A == 1 :
    AB = []
    for i in range(0 , line_B ):
        line_AB_i = []
        for j in range(0 , raw_B ):
            AB_ij = 0
            AB_ij = A[0][0]*B[i][j]
            #print(AB_ij)
            line_AB_i.append(AB_ij)
        AB.append(line_AB_i)
    print(AB)
        
#演算が定義できないとき
elif raw_A != line_B:
    print("This calculation is not defined!!")

#普通に計算できるとき
else:
    AB = []
    for i in range(0 , line_A ):
        line_AB_i = []
        for j in range(0 , raw_B ):
            AB_ij = 0
            for k in range(0 , raw_A ):
                AB_ij = A[i][k]*B[k][j] + AB_ij
            #print(AB_ij)
            line_AB_i.append(AB_ij)
        AB.append(line_AB_i)
    print(AB)