TL;DL
Paizaのレベルアップ問題集をやった時の回答集。
言語は「Python3」を選択。(模範回答例がほとんどないよ...)
マップの判定・縦横
STEP1:盤面の情報取得 (paizaランク C 相当)
回答
input_line = input()
row,col,cordi = input_line.split()
row = int(row)
col = int(col)
cordi = int(cordi)
input_bord_list = []
for r in range(0,row):
w_line = input()
input_bord_list.append(w_line)
input_cordinate_list =[]
for c in range(0,cordi):
w_line = input()
input_cordinate_list.append(w_line)
i = 0
while i < cordi:
p_x,p_y = input_cordinate_list[i].split()
p_x = int(p_x)
p_y = int(p_y)
target_row = input_bord_list[p_x]
target_data = target_row[p_y]
print(target_data)
i += 1
模範解答
H, W, N = map(int, input().split())
S = [list(input()) for _ in range(H)]
for _ in range(N):
y, x = map(int, input().split())
print(S[y][x])
- 標準入力をsplit()で分割したものにmap()でint型に変換し、各変数にアンパックしている。
- map()の第一引数に指定する際の関数には「()」は不要。
- 盤面情報のリストはリスト内包表記で実装している。forループの変数は後続で使用しないので「_」を使用している。
- 自作回答では、座標情報も最後まで取得してから出力を行っているが、模範解答では座標が入力されるたびに該当するデータを出力するようにしている。
STEP: 2 盤面の情報変更
h,w,n = map(int, input().split())
row_list = [list(input()) for _ in range(h)]
for _ in range(n):
x,y = map(int, input().split())
row_list[x][y] = '#'
for i in range(h):
print(''.join(row_list[i]))
- join()をいつもListオブジェクトのメソッドのように勘違いするが、Stringオブジェクトのメソッドなので間違えないようにする。
<結合部文字列>.join(list)
STEP: 3 マップの判定・横
h,w = map(int,input().split())
rows = [list(input()) for _ in range(h)]
for y_idx, row in enumerate(rows):
for x_idx, col in enumerate(row):
# 左端
if x_idx == 0 and row[1] == '#':
print(y_idx, x_idx)
continue
# 右端
if x_idx == len(row)-1 and row[-2] == '#':
print(y_idx, x_idx)
continue
# 両側が「#」
if 1 <= x_idx < len(row):
if row[x_idx-1] == '#':
if row[x_idx+1] == '#':
print(y_idx,x_idx)
- forループでインデックスも同時に取得したい場合は、enumerate([list]))を使用する。戻り値はindex, valueの順で格納される。
STEP: 4 マップの判定・縦
h,w = map(int, input().split())
rows = [list(input()) for _ in range(h)]
for y in range(h):
for x in range(w):
# 最上段
if y == 0 and rows[1][x] == '#':
print(y,x)
continue
# 最下段
if y == len(rows) - 1 and rows[y-1][x] == '#':
print(y,x)
continue
# 上下
if 0 < y < h:
if rows[y-1][x] == '#' and rows[y+1][x] == '#':
print(y,x)
FINAL問題:マップの判定・縦横
h,w = map(int,input().split())
rows = [list(input()) for _ in range(h)]
for y_idx, row in enumerate(rows):
for x_idx, col in enumerate(row):
# 最上段
if y_idx == 0:
# 左端
if x_idx == 0:
if rows[y_idx][x_idx+1] == '#' and rows[y_idx+1][x_idx] == '#':
print(y_idx,x_idx)
# 右端
elif x_idx == w-1:
if rows[y_idx][-2] == '#' and rows[y_idx+1][x_idx] == '#':
print(y_idx,x_idx)
# 両端以外
else:
if rows[y_idx][x_idx-1] == '#' and rows[y_idx][x_idx+1] == '#' and rows[y_idx+1][x_idx] == '#':
print(y_idx,x_idx)
# 最下段
if y_idx == h - 1:
# 左端
if x_idx == 0:
if rows[y_idx][1] == '#' and rows[y_idx-1][x_idx] == '#':
print(y_idx,x_idx)
# 右端
elif x_idx == w - 1:
if x_idx == w-1:
if rows[y_idx][-2] == '#' and rows[y_idx-1][x_idx] == '#':
print(y_idx,x_idx)
# 両端以外
else:
if rows[y_idx][x_idx-1] == '#' and rows[y_idx][x_idx+1] == '#' and rows[y_idx-1][x_idx] == '#':
print(y_idx,x_idx)
# 上下左右
if 1 <= y_idx < h-1:
if 0 <= x_idx < w:
# 左端
if x_idx == 0:
if rows[y_idx-1][x_idx] == '#' and rows[y_idx+1][x_idx] == '#' and rows[y_idx][x_idx+1] == '#':
print(y_idx,x_idx)
# 右端
elif x_idx == w-1:
if rows[y_idx-1][x_idx] == '#' and rows[y_idx+1][x_idx] == '#' and rows[y_idx][x_idx-1] == '#':
print(y_idx,x_idx)
# 両端以外
else:
if rows[y_idx-1][x_idx] == '#' and rows[y_idx+1][x_idx] == '#' and \
rows[y_idx][x_idx-1] == '#' and rows[y_idx][x_idx+1] == '#':
print(y_idx,x_idx)
- かなり冗長的なコードになっている自覚はあるけど、使えるライブラリも構文も知識内にないのでまぁ追々。
累積和の計算
STEP: 33 累積和の計算
cnt = int(input())
numbers = list(map(int,list(input().split())))
total = 0
for number in numbers:
total += number
print(total)
- map()の結果をList型として受け取りたい場合はlist()で変換する。
Next ...