paizaラーニングレベルアップ問題集をPython3でやってみた

paiza×Qiita記事投稿キャンペーンということで、キャンペーン対象問題25問をPython3で解いてみました。
- - -
###### [文字の一致](https://paiza.jp/works/mondai/d_rank_skillcheck_sample/diff_str)
```py
print("OK" if input() == input() else "NG")
```
- - -
###### [一番小さい値](https://paiza.jp/works/mondai/d_rank_skillcheck_sample/min_num)
```py
print(min(int(input()) for _ in range(5)))
```
- - -
###### [足し算](https://paiza.jp/works/mondai/d_rank_skillcheck_sample/addition)
```py
a, b = map(int, input().split())
print(a + b)
```
- - -
###### [Eメールアドレス](https://paiza.jp/works/mondai/d_rank_skillcheck_archive/email_address)
```py
print(input() + "@" + input())
```
- - -
###### [N倍の文字列](https://paiza.jp/works/mondai/d_rank_skillcheck_archive/square)
```py
print('*' * int(input()))
```
- - -
###### [宝くじ](https://paiza.jp/works/mondai/c_rank_skillcheck_archive/lottery)
```py
b = int(input())
for _ in range(int(input())):
	a = int(input())
	if a == b:
		print("first")
	elif abs(a - b) == 1:
		print("adjacent")
	elif not (a - b) % 10000:
		print("second")
	elif not (a - b) % 1000:
		print("third")
	else:
		print("blank")
```
を、2行で書きます。
こちらは[以前頂いたコメント](https://qiita.com/anonymously115/items/1235f75236a6f1235b3c#comments)を参考にさせていただきました。有難うございます。
```py
b=int(input())
print(*[(lambda a:abs(a-b)==1and"adjacent"or(a-b)%1000and"blank"or(a-b)%10000and"third"or a-b and"second"or"first")(int(input()))for _ in[0]*int(input())],sep='\n')
```
- - -
###### [野球の審判](https://paiza.jp/works/mondai/c_rank_skillcheck_archive/umpire)
```py
strike = 0
ball = 0
for _ in range(int(input())):
	s = input()
	if s == "strike":
		strike += 1
		if strike == 3:
			print("out!")
		else:
			print("strike!")
	elif s == "ball":
		ball += 1
		if ball == 4:
			print("fourball!")
		else:
			print("ball!")
```
- - -
###### [みかんの仕分け](https://paiza.jp/works/mondai/c_rank_skillcheck_archive/mikan)
```py
N, M = map(int, input().split())
for _ in range(M):
	print(((int(input()) + N // 2) // N or 1) * N)
```
- - -
###### [Fizz Buzz](https://paiza.jp/works/mondai/c_rank_skillcheck_sample/fizz-buzz)
```py
for i in range(1, int(input()) + 1):
	if i % 3 and i % 5:
		print(i)
	elif i % 5:
		print("Fizz")
	elif i % 3:
		print("Buzz")
	else:
		print("Fizz Buzz")
```
を、1行で書きます。
```py
print(*[((""if i%3else"Fizz ")+(""if i%5else"Buzz")).strip()or i for i in range(1,-~int(input()))],sep='\n')
```
- - -
###### [残り物の量](https://paiza.jp/works/mondai/c_rank_skillcheck_archive/leftover)
```py
m, p, q = map(int, input().split())
print(m * (100 - p) * (100 - q) / 10000)
```
- - -
###### [3Dプリンタ](https://paiza.jp/works/mondai/b_rank_skillcheck_archive/3d_printer)
```py
X, Y, Z = map(int, input().split())
s = []
for z in range(Z):
	s.append([])
	for x in range(X):
		s[z].append(input())
	bar = input()
for z in range(Z - 1, -1, -1):
	t = ""
	for y in range(Y):
		t += '#' if any(s[z][x][y] == '#' for x in range(X)) else '.'
	print(t)
```
を、3行で書きます。
```py
X, Y, Z = map(int, input().split())
s = [[input() for x in range(X)] + [input()] * 0 for z in range(Z)]
print(*["".join('#' if any(s[~z][x][y]=='#' for x in range(X)) else '.' for y in range(Y)) for z in range(Z)], sep='\n')
```
- - -
###### [神経衰弱](https://paiza.jp/works/mondai/b_rank_skillcheck_archive/concentration)
```py
H, W, N = map(int, input().split())
t = []
for i in range(H):
	t.append(input().split())
C = [0] * N
P = 0
for _ in range(int(input())):
	a, b, A, B = (int(x) - 1 for x in input().split())
	if t[a][b] == t[A][B]:
		C[P] += 2
	else:
		P = -~P % N
print(*C, sep='\n')
```
- - -
###### [みんなでしりとり](https://paiza.jp/works/mondai/b_rank_skillcheck_archive/word_chain)
```py
N, K, M = map(int, input().split())
D = {input() for _ in range(K)}
alive = {i for i in range(N)}
P = 0
bef = ""
for _ in range(M):
	s = input()
	if s in D and (not bef or bef[-1] == s[0]) and s[-1] != 'z':
		D.remove(s)
		bef = s
	else:
		alive.remove(P)
		bef = ""
	P = -~P % N
	while P not in alive:
		P = -~P % N
print(len(alive))
print(*[-~i for i in sorted(list(alive))], sep='\n')
```
- - -
###### [長テーブルのうなぎ屋](https://paiza.jp/works/mondai/b_rank_skillcheck_sample/long-table)
```py
n, m = map(int, input().split())
seats = [False] * n
for _ in range(m):
	a, b = map(int, input().split())
	if not any(seats[b + i - n] for i in range(a)):
		for i in range(a):
			seats[b + i - n] = True
print(sum(seats))
```
- - -
###### [名刺バインダー管理](https://paiza.jp/works/mondai/b_rank_skillcheck_archive/name_card)
```py
n, m = map(int, input().split())
print(2 * n * ((m - 1) // (2 * n) * 2 + 1) + 1 - m)
```
- - -
###### [本の整理](https://paiza.jp/works/mondai/real_event/book_sort)
```py
N = int(input())
A = [None] + list(map(int, input().split()))
B = {A[i] : i for i in range(1, -~N)}
C = 0
for i in range(1, -~N):
	if A[i] != i:
		B[A[i]] = B[i]
		A[B[i]] = A[i]
		C += 1
print(C)
```
- - -
###### [山折り谷折り](https://paiza.jp/works/mondai/real_event/origami)
```py
def reverse(s):
	t = ""
	for c in s[::-1]:
		t += chr(ord('0') + ord('1') - ord(c))
	return t


def origami(n):
	if n == 0:
		return ""
	s = origami(~-n)
	return s + "0" + reverse(s)


print(origami(int(input())))
```
- - -
###### [ハノイの塔](https://paiza.jp/works/mondai/real_event/hanoi)
```py
N, t = map(int, input().split())
piles = [[i for i in range(N, 0, -1)], [], []]


def hanoi(n, src, dst, tmp):
	global t
	if t == 0:
		return
	if n == 1:
		dst.append(src.pop())
		t -= 1
		if t == 0:
			return
	else:
		hanoi(~-n, src, tmp, dst)
		hanoi(1, src, dst, tmp)
		hanoi(~-n, tmp, dst, src)


hanoi(N, piles[0], piles[2], piles[1])
for pile in piles:
	if pile:
		print(*pile)
	else:
		print("-")
```
- - -
###### [じゃんけんの手の出し方](https://paiza.jp/works/mondai/a_rank_skillcheck_sample/janken)
```py
N, M = map(int, input().split())
s = input()
G, C, P = [0] * 3
for c in s:
	if c == 'G': G += 1
	elif c == 'C': C += 1
	elif c == 'P': P += 1
ans = 0
for p in range(M % 2, M // 5 + 1, 2):
	c = (M - 5 * p) // 2
	if c > N:
		continue
	g = N - c - p
	if g < 0:
		continue
	ans = max(ans, min(g, C) + min(c, P) + min(p, G))
print(ans)
```
- - -
###### [お菓子の詰め合わせ](https://paiza.jp/works/mondai/a_rank_skillcheck_archive/school_hiking)
```py
N, X = map(int, input().split())
P = sorted([int(input()) for _ in range(N)])
M = 0
S = 0
while M < N:
	if S + P[M] > X:
		break
	S += P[M]
	M += 1


def dfs(A, amount):
	if len(A) == M:
		if amount <= X:
			return amount
		else:
			return -1
	else:
		res = 0
		for i in range(-~A[-1] if A else 0, N):
			A.append(i)
			res = max(res, dfs(A, amount + P[i]))
			A.pop()
		return res


print(X - dfs([], 0))
```
- - -
###### [十億連勝](https://paiza.jp/works/mondai/real_event/continuous_winning)
```py
from collections import defaultdict

MOD = 1000000000
N, X = map(int, input().split())
state = {b: defaultdict(int) for b in [False, True]}
state[False][0] = 1
for _ in range(N):
	a = int(input())
	temp = {b: defaultdict(int) for b in [False, True]}
	for b in [False, True]:
		for w in state[b].keys():
			if w + a <= X:
				temp[b][w + a] = (temp[b][w + a] + state[b][w]) % MOD
				temp[b][0] = (temp[b][0] + a * state[b][w]) % MOD
			else:
				temp[True][0] = (temp[True][0] + state[b][w]) % MOD
				temp[b][0] = (temp[b][0] + (X - w) * state[b][w]) % MOD
	state = temp
ans = state[False][X]
for k, v in state[True].items():
	ans = (ans + v) % MOD
print(ans)
```
- - -
###### [文字列収集](https://paiza.jp/works/mondai/real_event/word_collection)
```py
from collections import defaultdict

N, M = map(int, input().split())
D = defaultdict(int)
for _ in range(N):
	s, p = input().split()
	p = int(p)
	for i in range(1, -~len(s)):
		D[s[:i]] += p
for _ in range(M):
	print(D[input()])
```
- - -
###### [mod7占い](https://paiza.jp/works/mondai/s_rank_skillcheck_sample/mod7)
```py
N = int(input())
C = [0] * 7
for _ in range(N):
	C[int(input()) % 7] += 1
ans = 0
for i in range(7):
	for j in range(i, 7):
		k = (14 - i - j) % 7
		if k < j: continue
		if i == j == k:
			ans += C[i] * (C[i] - 1) * (C[i] - 2) // 6
		elif i == j:
			ans += C[i] * (C[i] - 1) * C[k] // 2
		elif j == k:
			ans += C[i] * C[j] * (C[j] - 1) // 2
		else:
			ans += C[i] * C[j] * C[k]
print(ans)
```
- - -
###### [島探し](https://paiza.jp/works/mondai/s_rank_skillcheck_sample/search-island)
```py
from sys import setrecursionlimit
setrecursionlimit(1 << 20)
D = [(0, 1), (1, 0), (0, -1), (-1, 0)]


def dfs(A, y, x):
	A[y][x] = "0"
	for dy, dx in D:
		i = y + dy
		j = x + dx
		if 0 <= i < len(A) and 0 <= j < len(A[i]) and A[i][j] == "1":
			dfs(A, i, j)


M, N = map(int, input().split())
A = [input().split() for _ in range(N)]
C = 0
for i in range(N):
	for j in range(M):
		if A[i][j] == "1":
			dfs(A, i, j)
			C += 1
print(C)
```
- - -
###### [村人の友好関係](https://paiza.jp/works/mondai/s_rank_skillcheck_archive/group_popularity)
```py
import heapq


def root(A, v):
	if A[v] < 0:
		return v
	A[v] = root(A, A[v])
	return A[v]


def unite(A, v, u):
	rv = root(A, v)
	ru = root(A, u)
	if rv == ru:
		return False
	if A[rv] > A[ru]:
		A[rv] = A[ru]
	else:
		if A[rv] == A[ru]:
			A[rv] -= 1
		A[ru] = rv
	return True


N, M, Q = map(int, input().split())
HQ = []
heapq.heapify(HQ)
for _ in range(M):
	a, b, f = map(int, input().split())
	heapq.heappush(HQ, (-f, a, b))
A = [-1] * N
MST = []
while HQ:
	f, a, b = heapq.heappop(HQ)
	if unite(A, ~-a, ~-b):
		MST.append((-f, a, b))
S = set()
for _ in range(Q):
	op, q = input().split()
	q = int(q)
	if op == '+':
		S.add(q)
	elif op == '-':
		S.remove(q)
	for f, a, b in MST:
		if (a in S) != (b in S):
			print(f)
			break
	else:
		print(0)
```