def reading(file_name): r = read_lines(file_name) a = r[:7] a[0] = a[0][:-1] for i in range(len(a)): a[i] = list(map(int, a[i].split())) return a
if -1 in l: level = l.index(-1) brint(level) def climbing2(ladder): checker = [[0] * 10 for _ in range(10)] def brint(lst2D): for l in lst2D: print(l) if __name__ == "__main__": climbing2(to_ladder(read_lines("example_files/사다리in1.txt"))) matrix = to_ladder(read_lines("example_files/사다리in1.txt")) checker = [[0] * 10 for _ in range(10)] def dfs(row, col): checker[row][col] = 1 if row == 0: print(f"luck in {col}") else: if col - 1 >= 0 and matrix[row][col - 1] == 1 and checker[row][col - 1] == 0: dfs(row, col - 1) elif col + 1 < 10 and matrix[row][col + 1] == 1 and checker[row][col +
x_ = [1, -1, 0, 0] y_ = [0, 0, 1, -1] def harvest(x, y, d): print("x: ", x, " y: ", y, " d: ", d) nonlocal answer answer += farm[x][y] farm[x][y] = -1 nice_print(farm) if d > 0: nice_print(farm) for i in range(4): temp_x = x + x_[i] temp_y = y + y_[i] if farm[temp_x][temp_y] > 0: harvest(temp_x, temp_y, d - 1) harvest(depth, depth, depth) print(answer) return def nice_print(lst): for i in lst: for j in i: print(f"{j:3}", end="") print() if __name__ == "__main__": print(apple_farm_dfs(read_lines(".\\example_files\\apple_tree_1.txt")))
def skill_order(lst): order, length, tree = reset(lst) print(order, length, tree) answer = [] for t in tree: temp_order = order.copy() for l in t: if l in temp_order: if l != temp_order.popleft(): answer.append("No") break else: if not temp_order: answer.append("Yes") else: answer.append("No") return answer def reset(lst): order = deque(lst[0]) length = lst[1] tree = [] for i in lst[2:-1]: tree.append(deque(i)) return order, length, tree if __name__ == "__main__": print(skill_order(read_lines("example_files/전공필수in2.txt")))
print(need) print(cut_count(lines, 40198)) while cut_count(lines, n) != need: if cut_count(lines, n) < need: # 개수 부족 temp_long = n n = (temp_short + temp_long) // 2 elif cut_count(lines, n) > need: # 개수 남음 temp_short = n n = (temp_short + temp_long) // 2 print(temp_short, " ", n, " ", temp_long) while cut_count(lines, n) < need: n += 1 print(n) print(n) return n def cut_count(lst, cut_len): answer_lst = list(map(lambda x: x // cut_len, lst)) answer = 0 for ele in answer_lst: answer += ele return answer if __name__ == "__main__": cut(read_lines("example_files\\랜선자르기in1.txt")) cut(read_lines("example_files\\랜선자르기in2.txt")) cut(read_lines("example_files\\랜선자르기in3.txt")) cut(read_lines("example_files\\랜선자르기in4.txt"))
# answer = [] # print(line) # for _ in range(length): # temp_index = line.argmin() # answer.append(temp_index + 1) # line[temp_index] = max_value + 1 # # return answer # 4 8 9 6 1 3 10 7 5 2 def reversing(lst): length = int(lst[0]) line = list(map(int, lst[1].split())) maximum = length + 1 answer = [length + 1] * length print(answer) print(line) for i, e in enumerate(line): idx = i + 1 pos = e for l in answer: if l < idx: pos -= 1 elif pos == -1: pass if __name__ == "__main__": print(reversing(read_lines("example_files/역수열in1.txt")))
continue elif large_index == h: return count + 1 def order_deque(lst): lst[0] = tuple(map(int, lst[0].split())) n, m = lst[0] patient_lst = deque(map(int, lst[1].split())) del lst enum_lst = [(idx, int(xx)) for idx, xx in enumerate(patient_lst)] Q = deque(enum_lst) cnt = 0 while True: now = Q.popleft() if any(now[1] < x[1] for x in Q): Q.append(now) else: cnt += 1 if now[0] == m: break print(f"지정하신 {m} 환자의 진료 차례는 {cnt}번째 입니다.") if __name__ == "__main__": order_deque(read_lines("example_files\\응급실in1.txt")) print(order_deque(read_lines("example_files\\응급실in2.txt"))) order_deque(read_lines("example_files\\응급실in2.txt"))
dis = [[0] * 5 for _ in range(5)] que = deque() que.append((0, 0)) matrix[0][0] = 1 while que: print(que) temp = que.popleft() for i in range(4): x = temp[0] + dx[i] y = temp[1] + dy[i] if 0 <= x < 5 and 0 <= y < 5 and matrix[x][y] == 0: matrix[x][y] = matrix[temp[0]][temp[1]] + 1 que.append((x, y)) if matrix[4][4] == 0: print(-1) else: print(matrix[4][4] - 1) for m in matrix: print(m) def beauti(lst): for l in lst: print(l) if __name__ == "__main__": short_out(file_reader.read_lines("example_files\\미로탈출in1.txt")) teachers_short_out([])
elif x != 0: if x == size: dfs(0, y + 1) else: val[y][x] += val[y][x - 1] dfs(x + 1, y) else: if x == 0: val[y][x] += val[y - 1][x] dfs(x + 1, y) elif x == size: val[y][x] += min(val[y - 1][x], val[y][x - 1]) dfs(0, y + 1) else: val[y][x] += min(val[y - 1][x], val[y][x - 1]) dfs(x + 1, y) dfs(0, 0) return val[-1][-1] if __name__ == "__main__": import timeit # print(valley(read_lines("./example_files/계곡탈출in3.txt"))) # print(valley2(read_lines("./example_files/계곡탈출in3.txt"))) print(valley3(read_lines("./example_files/계곡탈출in1.txt"))) # print(dfs_kind(50)) map
map(lambda x: (int(x.split()[0]), int(x.split()[1])), lst[1:])) print(weight_value) answer = [] def dfs(cur_weight, cur_value): nonlocal weight_value if cur_weight < 0: return for weight, value in weight_value: if cur_weight - weight >= 0: answer.append(cur_value + value) dfs(cur_weight - weight, cur_value + value) dfs(max_weight, 0) return max(answer) if __name__ == "__main__": print(packing(read_lines("./example_files/가방담기in3.txt"))) # 1: 28, 2: 42, 3: 90 f = open("./example_files/가방담기in1.txt", "r") n, limit_kg = map(int, f.readline().split()) wbag_idx = [0] * (limit_kg + 1) for i in range(n): w, v = map(int, f.readline().split()) for j in range(w, limit_kg + 1): wbag_idx[j] = max(wbag_idx[j], wbag_idx[j - w] + v) print(wbag_idx)
def dfs(x, y): nonlocal cnt if x == l_index and y == l_index: cnt += 1 else: for i in range(4): xx = x + dx[i] yy = y + dy[i] if 0 <= xx <= l_index and 0 <= yy <= l_index and matrix[xx][ yy] == 0: matrix[xx][yy] = 8 beautiful_print(matrix) dfs(xx, yy) matrix[xx][yy] = 0 beautiful_print(matrix) matrix[0][0] = 8 dfs(0, 0) def beautiful_print(matrix): for xx in matrix: print(xx) print() if __name__ == "__main__": all_way(read_lines("example_files\\미로경로수in1.txt")) print(teachers_all_way("example_files\\미로경로수in1.txt"))
for i, m in enumerate(l): if m == 1: plague((e, i)) count += 1 print_2d(town) print() return count if __name__ == "__main__": # print(block_count(read_lines("example_files/town1.txt"))) # print(block_count(read_lines("example_files/town2.txt"))) # print(block_count(read_lines("example_files/town3.txt"))) lines = read_lines("example_files/town1.txt") matrix = [] n = int(lines[0]) for i in lines[1:-1]: matrix.append(list(map(int, i.split()))) print_2d(matrix) dx = [-1, 0, 1, 0] dy = [0, 1, 0, -1] def dfs(row, col, sep): global h_cnt h_cnt += 1 matrix[row][col] = sep for i in range(4): nrow = row + dx[i]
from morning.file_reader import read_lines from collections import deque def sinking_ship(lst=[]): m = int(lst[0].split()[1]) second_line = deque(sorted(list(map(int, lst[1].split())))) count = 0 while len(second_line) > 1: if second_line[0] + second_line[-1] > m: second_line.pop() count += 1 else: second_line.pop(0) second_line.pop() count += 1 return count if __name__ == "__main__": print(sinking_ship(read_lines("example_files\\침몰타이타닉1.txt"))) print(sinking_ship(read_lines("example_files\\침몰타이타닉2.txt"))) print(sinking_ship(read_lines("example_files\\침몰타이타닉4.txt")))
from morning.file_reader import read_lines def travel(lst): max_nodes = int(lst[0].split()[0]) all_nodes = lst[0].split()[1] print(max_nodes, all_nodes) origin_node = list(map(lambda x: list(map(int, x.split())), lst[1:])) count = 0 def walk(node, pos=1): nonlocal count if len(node) == 0: return if pos == max_nodes: count += 1 return for e, n in enumerate(node): if pos in n: pos = n[int(not n.index(pos))] walk(node[:e] + node[e + 1:], pos) walk(origin_node) return count if __name__ == "__main__": print(travel(read_lines("example_files/상태변화in1.txt")))
from morning.file_reader import read_lines def partial(origin, lst): lst = list(map(to_int_index, lst)) for first, second in lst: origin = origin[:first] + origin[first:second][::-1] + origin[second:] return origin def to_int_index(two): first, second = two.split(" ") return int(first) - 1, int(second) if __name__ == "__main__": origin = [i for i in range(1, 21)] print(partial(origin, read_lines("./example_files/구간자리in1.txt")))