description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let...
import sys readline = sys.stdin.readline n, m = map(int, input().split()) ans = True graph = [[] for i in range(n)] for _ in range(m): a, b = map(int, readline().split()) a -= 1 b -= 1 graph[a].append(b) graph[b].append(a) s1 = set(graph[0]) s2 = set() if len(graph[0]) != 0: s2 = set(graph[grap...
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR V...
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let...
def main(): vertex, edge = map(int, input().split()) data = {key: set() for key in range(1, vertex + 1)} for i in range(edge): a, b = map(int, input().split()) data[a].add(b) data[b].add(a) v = [] temp = [None, None, None] for node in data.values(): if temp[0] and...
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST NONE NONE NON...
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let...
n, m = map(int, input().split()) G = [[] for _ in range(n)] for i in range(m): a, b = map(int, input().split()) a -= 1 b -= 1 G[a].append(b) G[b].append(a) if len(G[0]) < 2: print(-1) return res = [1] * n for a in G[0]: res[a] = 2 a2 = G[0][0] for b in G[a2]: if res[b] == 2: ...
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETUR...
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let...
import sys sys.setrecursionlimit(10**9) input = sys.stdin.readline class UnionFind: def __init__(self, N): self.parent = [i for i in range(N)] self.size = [(1) for _ in range(N)] def find(self, x): if self.parent[x] == x: return x else: return self.fi...
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VA...
You have a simple undirected graph consisting of $n$ vertices and $m$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. Let's make a definition. Let $v_1$ and $v_2$ be two some nonempty subsets of vertices that do not intersect. Let...
n, m = map(int, input().split()) ad = [[] for _ in range(n)] es = [] for _ in range(m): v, u = map(int, input().split()) v -= 1 u -= 1 es.append((min(v, u), max(v, u))) es = sorted(es, key=lambda x: (x[0], x[1])) for e in es: v, u = e ad[v].append(str(u)) ad[u].append(str(v)) ad = ["".join(a...
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER V...
Konrad is a Human Relations consultant working for VoltModder, a large electrical equipment producer. Today, he has been tasked with evaluating the level of happiness in the company. There are $n$ people working for VoltModder, numbered from $1$ to $n$. Each employee earns a different amount of money in the company β€” ...
import sys n, m = list(map(int, sys.stdin.readline().strip().split())) L = [(0) for i in range(0, n)] H = [[] for i in range(0, n)] for i in range(0, m): x, y = list(map(int, sys.stdin.readline().strip().split())) x = x - 1 y = y - 1 if x > y: x, y = y, x L[y] = L[y] + 1 H[x].append(y) ...
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMB...
Konrad is a Human Relations consultant working for VoltModder, a large electrical equipment producer. Today, he has been tasked with evaluating the level of happiness in the company. There are $n$ people working for VoltModder, numbered from $1$ to $n$. Each employee earns a different amount of money in the company β€” ...
import sys input = sys.stdin.readline n, m = list(map(int, input().split())) IN = [0] * n OUT = [0] * n INSET = [[] for i in range(n)] for i in range(m): x, y = sorted(map(int, input().split())) IN[x - 1] += 1 OUT[y - 1] += 1 INSET[x - 1].append(y - 1) ANS = 0 for i in range(n): ANS += IN[i] * OUT[...
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NU...
Konrad is a Human Relations consultant working for VoltModder, a large electrical equipment producer. Today, he has been tasked with evaluating the level of happiness in the company. There are $n$ people working for VoltModder, numbered from $1$ to $n$. Each employee earns a different amount of money in the company β€” ...
n, m = map(int, input().split()) out, inp = [0] * (n + 1), [0] * (n + 1) inset = [[] for i in range(n + 1)] for i in range(m): x, y = map(int, input().split()) if x > y: z = x x = y y = z out[y] += 1 inp[x] += 1 inset[x].append(y) q = int(input()) res = 0 for ind in range(1, ...
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VA...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
listed = set() def goal(x): return all([(len(x[i]) == 0) for i in range(1, len(x))]) def moves(x): for i in range(len(x)): if x[i]: for j in range(len(x)): if len(x[j]) == 0 or x[i][-1] < x[j][-1]: yield i, j def bfs(x): def tt(d): retur...
ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR VAR VAR FUNC_DEF FUNC_DEF ...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
import sys def generate(move, N): next = [] i = 0 available = [1, 2, 3, 4] while i < N: if len(available) == 0: break elif move[i] in available: available.remove(move[i]) else: i = i + 1 continue for m in available: ...
IMPORT FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_O...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
N = int(input().strip()) a = [int(a_temp) for a_temp in input().strip().split(" ")] goal_state = tuple(reversed(range(N))), tuple(), tuple(), tuple() initial_state = [[], [], [], []] for i, rod in enumerate(a): initial_state[rod - 1].insert(0, i) initial_state = tuple(tuple(rod) for rod in initial_state) initial_st...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST LIST LIST LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NU...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
def allowed_moves(x): for i in range(len(x)): if x[i]: for j in range(len(x)): if not x[j] or x[i][-1] < x[j][-1]: yield i, j def reached_goal(x): return all([(len(x[i]) == 0) for i in range(1, len(x))]) def tupler(z): z[1:4] = sorted(z[1:4], key=l...
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBE...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
import sys N = int(input().strip()) a = [int(a_temp) for a_temp in input().strip().split(" ")] def move1(a, c): p, q = c if not p in a: return False i = a.index(p) if q in a[:i]: return False return a[:i] + tuple([q]) + a[i + 1 :] def func(N, a): changes = [ (1, 2), ...
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER FUNC_...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
import sys def getRod(position, disc): return position >> disc * 2 & 3 def setRod(position, disc, rod): position = position & ~(3 << disc * 2) | rod << disc * 2 return position def getPosition(rods): position = 0 for i in range(len(rods)): position = setRod(position, i, rods[i]) re...
IMPORT FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
import sys n = int(input().strip()) A = [int(a_temp) for a_temp in input().strip().split(" ")] def fewestmovesHanoi(n, A): if A == [1] * n: return 0 forward = set(["1" * n]) countf = 0 reverse = set(["".join(str(a) for a in A)]) countr = 0 while True: if countf == countr: ...
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF IF VAR BIN_OP LIST NUMBER VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP STRING VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL STRING FUNC_CALL VAR VAR VAR VA...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
N = 10 a = [0] * (2 << 2 * N + 1) b = [0] * 4 n = int(input()) K = 0 t = 0 for x in map(int, input().split()): K |= x - 1 << t * 2 t += 1 a[0] = 1 Q = [] Q.append(0) ind = 0 while True: x = Q[ind] ind += 1 if x == K: print(a[x] - 1) exit(0) for i in range(4): b[i] = 1000 ...
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
from itertools import product P = [*product(range(4), repeat=2)] def gen_moves(arrangement, n): tops = [-1] * 4 arr = arrangement for j in range(n): tops[arr % 4] = j arr //= 4 for i, j in P: if tops[i] > tops[j]: yield arrangement + 4 ** tops[i] * (j - i) def di...
ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR EXPR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VA...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
N = int(input().strip()) a = [(int(_) - 1) for _ in input().split(" ")] rep = [""] * 4 for ii in range(N): rep[a[ii]] += str(ii) rep[0] += ";" for ii in range(1, 4): rep[ii] += ":" rep.sort() key = "".join(rep) used = set([key]) moves = [[][:]] * 6 bfs, substr, count = [rep], str(N - 1) + ";", 0 while substr[0]...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST STRING NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR F...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
import sys MAX_MOVES = 100 def valid_moves(state, K): pegs, tops = [-1] * K, [] for r, peg in enumerate(state): if pegs[peg] < 0: pegs[peg] = r for top_r, top_peg in tops: yield top_r, top_peg, peg tops.append((r, peg)) for dst_peg, peg_r in enu...
IMPORT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR EXPR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR VAR VAR EXPR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR RETURN ...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
N = 4 n = int(input()) a = tuple(map(lambda x: int(x) - 1, input().split())) def next_states(state): has = [False] * N for i, v in enumerate(state): if has[v]: continue has[v] = True for j in range(N): if not has[j]: yield state[:i] + (j,) + stat...
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR BIN_OP BIN_OP VAR VA...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
import itertools as it import sys N = int(input().strip()) InitialLoc = [int(a_temp) for a_temp in input().strip().split(" ")] Transformations = list(it.permutations(range(4), 2)) nBlock = len(InitialLoc) StateSize = 2 * nBlock powersOf2 = [(2**m) for m in range(StateSize - 1, -1, -1)] InitialLoc = [(m - 1) for m in I...
IMPORT IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR ...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
N = int(input().strip()) bitfull = 4**N - 1 def move(state, size, rto): shift = (size - 1) * 2 mask = 3 << shift invmask = bitfull ^ mask rbit = rto << shift return state & invmask | rbit def getmins(state): mins = [N + 1] * 4 size = 1 mask = 3 found = 0 while found < 4 and s...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER NUMBER ASSIG...
The Tower of Hanoi is a famous game consisting of $3$ rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the fol...
def bfs(start, num): def move(data): res_temp = [11, 11, 11, 11] res = [] data_temp = data for i in range(1, num + 1): temp = data_temp % 4 data_temp >>= 2 if res_temp[temp] == 11: res_temp[temp] = i for i in range(4): ...
FUNC_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN V...
qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. oekh jqia yi je vydt jxu djx ucyhf. Input jxu ydfkj sediyiji ev q iydwbu ydjuwuh d (1 ≀ d ≀ 11184) β€” jxu edu-rqiut ydtun ev jxu ucyhf je vydt. Output ekjfkj q iydwbu dkcruh. Examples Input 1 Output 13
def primesieve(n): primes = [2] table = [0, 0, 1] + [1, 0] * (n // 2 - 1) if len(table) == n: table.append(1) i = 3 while i <= n: if table[i] == 1: primes.append(i) for k in range(i, n // i + 1, 2): table[i * k] = 0 i += 2 return pr...
FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER NUMBER BIN_OP LIST NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ...
qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. oekh jqia yi je vydt jxu djx ucyhf. Input jxu ydfkj sediyiji ev q iydwbu ydjuwuh d (1 ≀ d ≀ 11184) β€” jxu edu-rqiut ydtun ev jxu ucyhf je vydt. Output ekjfkj q iydwbu dkcruh. Examples Input 1 Output 13
from sys import stdin, stdout def get(): return stdin.readline().strip() def getf(sp=" "): return [int(i) for i in get().split(sp)] def put(a, end="\n"): stdout.write(str(a) + end) def putf(a, sep=" ", end="\n"): stdout.write(sep.join([str(i) for i in a]) + end) def prime(n): i = 2 whi...
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_DEF STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF STRING STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VA...
qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. oekh jqia yi je vydt jxu djx ucyhf. Input jxu ydfkj sediyiji ev q iydwbu ydjuwuh d (1 ≀ d ≀ 11184) β€” jxu edu-rqiut ydtun ev jxu ucyhf je vydt. Output ekjfkj q iydwbu dkcruh. Examples Input 1 Output 13
import sys def main(): nb = 1000 * 1000 tab = [True] * (nb + 1) premier = [2] tab[0] = False tab[1] = False for i in range(2, nb + 1): if tab[i]: j = i * i while j < nb + 1: tab[j] = False j += i cible = int(input()) x = 0...
IMPORT FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VA...
qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. oekh jqia yi je vydt jxu djx ucyhf. Input jxu ydfkj sediyiji ev q iydwbu ydjuwuh d (1 ≀ d ≀ 11184) β€” jxu edu-rqiut ydtun ev jxu ucyhf je vydt. Output ekjfkj q iydwbu dkcruh. Examples Input 1 Output 13
import sys def solve(): (d,) = rv() cur = 2 while d > 0: if isprime(cur): rev = int(str(cur)[::-1]) if rev != cur and isprime(rev): d -= 1 if d > 0: cur += 1 print(cur) def isprime(cur): check = 2 while check * check <= cur:...
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN N...
qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. oekh jqia yi je vydt jxu djx ucyhf. Input jxu ydfkj sediyiji ev q iydwbu ydjuwuh d (1 ≀ d ≀ 11184) β€” jxu edu-rqiut ydtun ev jxu ucyhf je vydt. Output ekjfkj q iydwbu dkcruh. Examples Input 1 Output 13
N = 1000000 def rev(z): res = 0 while z: res *= 10 res += z % 10 z //= 10 return res def revv(z): res = rev(z) if res != z: return res def rwh_primes_x(n): sieve = [True] * n for i in range(3, int(n**0.5) + 1, 2): if sieve[i]: sieve[i...
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN...
qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. oekh jqia yi je vydt jxu djx ucyhf. Input jxu ydfkj sediyiji ev q iydwbu ydjuwuh d (1 ≀ d ≀ 11184) β€” jxu edu-rqiut ydtun ev jxu ucyhf je vydt. Output ekjfkj q iydwbu dkcruh. Examples Input 1 Output 13
def prime(x): if x == 2: return 1 if ~x & 1: return 0 i = 3 while i * i <= x: if x % i == 0: return 0 i += 1 return 1 n = int(input()) cnt = 0 for i in range(2, int(1000000000.0), 1): if not prime(i): continue if i == int(str(i)[::-1]): ...
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR IF VAR...
qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. oekh jqia yi je vydt jxu djx ucyhf. Input jxu ydfkj sediyiji ev q iydwbu ydjuwuh d (1 ≀ d ≀ 11184) β€” jxu edu-rqiut ydtun ev jxu ucyhf je vydt. Output ekjfkj q iydwbu dkcruh. Examples Input 1 Output 13
def f(n): i = 2 while i * i <= n: if n % i == 0: return False i += 1 return True n = int(input()) for i in range(10, 1000000): if i != int(str(i)[::-1]) and f(i) and f(int(str(i)[::-1])): n -= 1 if n == 0: print(i) break
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER I...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
import sys sys.setrecursionlimit(5000) tests = int(input()) def generateKsum(seq, k, prevInd): if k == 1: return seq[prevInd:] result = [] for i in range(prevInd, len(seq)): elem = seq[i] for sums in generateKsum(seq, k - 1, i): result.append(elem + sums) return re...
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR V...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
import itertools as it T = int(input().strip()) for Loopi in range(T): N, k = [int(m) for m in input().strip().split()] S = [int(m) for m in input().strip().split()] SCount = {} for i1 in S: if SCount.get(i1) == None: SCount[i1] = 1 else: SCount[i1] += 1 SUni...
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VA...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
t = int(input()) for tk in range(t): [n, k] = [int(x) for x in input().split()] ksums = [int(x) for x in input().split()] ksums.sort() seq = [] seq.append(ksums.pop(0) // k) if n == 1: print(seq[0]) continue partitions = [] def addItem(listsofar, maxn): if len(li...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER AS...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
from itertools import combinations_with_replacement def print_results(res_set): output = " ".join([str(e) for e in res_set]) print(output) def is_set_for_index(aSet, index): for e in aSet: if e != index: return False return True T = int(input()) for num_tests in range(T): i...
FUNC_DEF ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
import itertools as it for _ in range(int(input())): n, k = map(int, input().split()) ewan = sorted(map(int, input().split())) ans = [ewan[0] // k] vals = {} for x in ewan[1:]: if vals.setdefault(x, 0) > 0: vals[x] -= 1 else: vals[x] -= 1 new = x ...
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR DICT FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER A...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
def comb(n, k): S = 1 for i in range(k): S *= n - i S //= i + 1 return S def Ksums(w, K): if K == 1: return w elif len(w) == 1: return [(K * w[0][0], comb(K + w[0][1] - 1, w[0][1] - 1))] elif len(w) == 2: return [ ( j * w[0][0...
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NU...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
cache = dict() def permutation(data, k): key = tuple([k] + data) if key in cache: return cache[key] ret = [data[-1] * k] if len(data) == 1: return ret for i in range(k): for x in permutation(data[:-1], k - i): ret.append(data[-1] * i + x) cache[key] = sorted...
ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP LIST VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR V...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
def partial_sums(K, start, A): if start == len(A) - 1: yield K * A[-1] else: for k in range(K): p = k * A[start] for s in partial_sums(K - k, start + 1, A): yield p + s yield K * A[start] for case in range(int(input().strip())): N, K = input(...
FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR BIN_OP VAR VAR EXPR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL F...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
inf = 10 ^ 18 + 1000 def merge(a, b): if len(a) == 0: return b[:] if len(b) == 0: return a[:] tmp = [] i = 0 j = 0 while i < len(a) or j < len(b): if i < len(a): ai = a[i] else: ai = inf if j < len(b): bj = b[j] ...
ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR A...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
t = int(input()) for _ in range(t): n, k = [int(x) for x in input().split(" ")] s = [int(x) for x in input().split(" ")] s.sort() a = [s[0] // k] sac = [[a[0] * i] for i in range(k + 1)] p = 0 while len(a) < n: while p < len(sac[k]) and s[p] == sac[k][p]: p += 1 a...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
import itertools import sys def combinations(factors, k): return itertools.combinations_with_replacement(factors, k) def cartesian_factors(seq, n, k): def _backsolve(seq, n, k, factors=[]): if len(seq) == 0: return factors if len(factors) == n: return factors ...
IMPORT IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF FUNC_DEF LIST IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR LIST VAR BIN_OP VAR NUMBER FOR VAR VAR ...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
from itertools import combinations_with_replacement as cwr for _ in range(int(input())): n, k = list(map(int, input().rstrip().split())) a = sorted(list(map(int, input().rstrip().split()))) values = [] used = {} for i in a: if not values: values.append(i // k) contin...
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL ...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
import sys sys.setrecursionlimit(2000) num_cases = int(input().strip()) test_cases = [] while num_cases > 0: n, k = list(map(int, input().strip().split(" "))) ksum = list(map(int, input().strip().split(" "))) test_cases.append((n, k, ksum)) num_cases -= 1 class Solution: def solver(self, n, k, k...
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VA...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
import sys class SumConstructor: def __init__(self, k, s_0, in_list, a_0, in_floor): self.steps = [k - 1] self.values = [s_0] self.newSums = {s_0: 1} self.read = 0 self.next = 1 self.floor = s_0 - a_0 self.input = in_list self.output = [a_0] de...
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR DICT VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMB...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
def allcomb(seq, k): if k == 0: return [0] lst = list() seqbis = list(seq) for a in seq: for b in allcomb(seqbis, k - 1): lst.append(a + b) seqbis.remove(a) return lst def addSum(already, seq, nxt, k): for p in range(1, k + 1): for l in allcomb(seq, ...
FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXP...
Alice thinks of a non-decreasing sequence of non-negative integers and wants Bob to guess it by providing him the set of all its K-sums with repetitions. What is this? Let the sequence be {A[1], A[2], ..., A[N]} and K be some positive integer that both Alice and Bob know. Alice gives Bob the set of all possible value...
def insertion_sort(a): for i in range(0, len(a) - 1): j = i while a[j] > a[j + 1] and j >= 0: temp = a[j] a[j] = a[j + 1] a[j + 1] = temp j -= 1 return a def merge_lists(a, b): final_list = [] pos_a = 0 pos_b = 0 while pos_a + pos...
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR...
A rectangle with sides $A$ and $B$ is cut into rectangles with cuts parallel to its sides. For example, if $p$ horizontal and $q$ vertical cuts were made, $(p + 1) \cdot (q + 1)$ rectangles were left after the cutting. After the cutting, rectangles were of $n$ different types. Two rectangles are different if at least o...
n = int(input()) w = [] h = [] c = [] cntw = {} cnth = {} multMayotC = 0 cntC = 0 def insert1(a, b, c): if not a in b: b[a] = c else: b[a] = b[a] + c def multMayot(a, b): if a % b == 0: return b else: return multMayot(b, a % b) for i in range(0, n): a, b, d = ma...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR...
The aim of this kata is to determine the number of sub-function calls made by an unknown function. You have to write a function named `count_calls` which: * takes as parameter a function and its arguments (args, kwargs) * calls the function * returns a tuple containing: * the number of function calls made inside it...
import sys def count_calls(f, *args, **kwargs): total = 0 def count(frame, event, arg): nonlocal total if event == "call": total += 1 sys.settrace(count) ret = f(*args, **kwargs) return total - 1, ret
IMPORT FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
The aim of this kata is to determine the number of sub-function calls made by an unknown function. You have to write a function named `count_calls` which: * takes as parameter a function and its arguments (args, kwargs) * calls the function * returns a tuple containing: * the number of function calls made inside it...
import sys calls = 0 orig_func = "" def profiler(frame, event, arg): nonlocal calls if event == "call": if frame.f_code.co_name != orig_func: calls += 1 return profiler def count_calls(func, *args, **kwargs): nonlocal orig_func nonlocal calls sys.settrace(profiler) o...
IMPORT ASSIGN VAR NUMBER ASSIGN VAR STRING FUNC_DEF IF VAR STRING IF VAR VAR VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NONE ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR VAR
The aim of this kata is to determine the number of sub-function calls made by an unknown function. You have to write a function named `count_calls` which: * takes as parameter a function and its arguments (args, kwargs) * calls the function * returns a tuple containing: * the number of function calls made inside it...
import sys def count_calls(func, *args, **kwargs): def tracefunc(frame, event, arg): nonlocal calls if event == "call": calls += 1 return tracefunc calls = -1 sys.settrace(tracefunc) rv = func(*args, **kwargs) return calls, rv
IMPORT FUNC_DEF FUNC_DEF IF VAR STRING VAR NUMBER RETURN VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
The aim of this kata is to determine the number of sub-function calls made by an unknown function. You have to write a function named `count_calls` which: * takes as parameter a function and its arguments (args, kwargs) * calls the function * returns a tuple containing: * the number of function calls made inside it...
import sys calls = 0 def trace(frame, event, arg): nonlocal calls if event == "call": calls += 1 sys.settrace(trace) def count_calls(func, *args, **kwargs): nonlocal calls calls = -1 result = func(*args, **kwargs) return calls, result
IMPORT ASSIGN VAR NUMBER FUNC_DEF IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
The aim of this kata is to determine the number of sub-function calls made by an unknown function. You have to write a function named `count_calls` which: * takes as parameter a function and its arguments (args, kwargs) * calls the function * returns a tuple containing: * the number of function calls made inside it...
import sys def count_calls(func, *args, **kwargs): tracer = Tracer() sys.settrace(tracer.my_tracer) rv = func(*args, **kwargs) return tracer.count, rv class Tracer: count: int = -1 def my_tracer(self, frame, event, arg=None): if event == "call": self.count += 1 r...
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR CLASS_DEF VAR VAR NUMBER FUNC_DEF NONE IF VAR STRING VAR NUMBER RETURN VAR
The aim of this kata is to determine the number of sub-function calls made by an unknown function. You have to write a function named `count_calls` which: * takes as parameter a function and its arguments (args, kwargs) * calls the function * returns a tuple containing: * the number of function calls made inside it...
import sys def count_calls(func, *args, **kwargs): tracer = Tracer(func) sys.settrace(tracer.my_tracer) rv = func(*args, **kwargs) return tracer.count, rv class Tracer: count: int = 0 func_name: str recursive: bool = False def __init__(self, func: callable): self.func_name =...
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR CLASS_DEF VAR VAR NUMBER VAR VAR VAR VAR NUMBER FUNC_DEF VAR ASSIGN VAR VAR FUNC_DEF NONE ASSIGN VAR VAR IF VAR STRING VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR
The aim of this kata is to determine the number of sub-function calls made by an unknown function. You have to write a function named `count_calls` which: * takes as parameter a function and its arguments (args, kwargs) * calls the function * returns a tuple containing: * the number of function calls made inside it...
import sys def count_calls(func, *args, **kwargs): def handler(_, line, k): handler.count += int(line == "call") handler.__dict__["count"] = 0 sys.settrace(handler) rv = func(*args, **kwargs) return handler.count - 1, rv
IMPORT FUNC_DEF FUNC_DEF VAR FUNC_CALL VAR VAR STRING ASSIGN VAR STRING NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last diffe...
import sys sys.setrecursionlimit(10**6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [...
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC...
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last diffe...
for tests in range(int(input())): n, d = map(int, input().split()) upper_bound, lower_bound, lv = n * (n - 1) // 2, 0, 0 for i in range(1, n + 1): if not i & i - 1: lv += 1 lower_bound += lv - 1 if not d in range(lower_bound, upper_bound + 1): print("NO") else: ...
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ...
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last diffe...
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n, d = map(int, input().split()) MIN = sum([(i.bit_length() - 1) for i in range(1, n + 1)]) MAX = (n - 1) * n // 2 if MIN <= d <= MAX: print("YES") else: print("NO") continue TREE = [(i // 2) f...
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_C...
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last diffe...
t = int(input()) for loop in range(t): n, d = map(int, input().split()) lis = [1] * n nsum = 0 for i in range(n): nsum += i sub = nsum - d if d > nsum: print("NO") continue ableind = 1 pickind = n - 1 while pickind > ableind: if sub > pickind - ableind...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WH...
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last diffe...
t = int(input()) for _ in range(t): n, d = map(int, input().split()) dsum = n * (n - 1) // 2 if d > dsum: print("NO") continue tree = [[i] for i in range(n)] lmin = 1 while dsum > d: l = len(tree) - 1 while dsum > d and l > lmin: l -= 1 dsu...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR V...
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last diffe...
t = int(input()) pows = [(2**i) for i in range(21)] for _ in range(t): n, d = map(int, input().split()) mins = 0 for k in range(20): if pows[k + 1] <= n: mins += k * pows[k] else: mins += k * (n - (pows[k] - 1)) break maxs = n * (n - 1) // 2 if d <...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMB...
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last diffe...
t = int(input()) for T in range(t): n, d = map(int, input().strip().split()) u = (n - 1) * n // 2 if d > u: print("NO") continue l = t = 0 for i in range(1, 1 + n): l += t if not i & i + 1: t += 1 if d < l: print("NO") continue p = ...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR VAR IF BIN_OP VAR BIN_OP...
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last diffe...
def solve(n, d): arr = [[x] for x in range(1, n + 1)] S = sum(range(1, n)) cur = 1 while S > d and cur != len(arr): need = S - d can = len(arr) - 1 - cur if can < need: S -= can arr[cur] += arr[-1] arr.pop() else: S -= need ...
FUNC_DEF ASSIGN VAR LIST VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR...
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last diffe...
for _ in range(int(input())): n, d = map(int, input().split()) ld, rd = 0, n * (n - 1) // 2 tn = n cd = 0 mvl = 1 while tn: vl = min(tn, mvl) ld += cd * vl cd += 1 tn -= vl mvl *= 2 if not ld <= d <= rd: print("NO") continue parent ...
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VA...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): ans = "" ds = [] dI = [+1, 0, 0, -1] dJ = [0, -1, +1, 0] def path(i, j, ans, vis): if i == n - 1 and j == n - 1: ds.append(ans) return dir = "DLRU" for k in range(4...
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_O...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: @staticmethod def findAllPaths(m, n, i, j, ans, path): if i < 0 or j < 0 or i >= n or j >= n or not m[i][j]: return ans if i == n - 1 and j == n - 1: ans.append(path) return m[i][j] = 0 Solution.findAllPaths(m, n, i + 1, j, ans...
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
import sys class Solution: def findPath(self, m, n): if m[0][0] == 0: return [-1] import sys sys.setrecursionlimit(10**9) visited = [[(False) for j in range(n)] for i in range(n)] dx = [1, 0, 0, -1] dy = [0, -1, 1, 0] d = ["D", "L", "R", "U"] ...
IMPORT CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER NUMBER RETURN LIST NUMBER IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def __init__(self): self.ans = [] def findPath(self, m, n): self.util(n, m, 0, 0, []) return self.ans def util(self, n, m, x, y, path): if x == n - 1 and y == n - 1 and m[x][y] == 1: self.ans.append("".join(path)) return if n...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER LIST RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CA...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def getAns(self, m, n, i, j, res, ans): if i == n - 1 and j == n - 1: res.append(ans) return if i < 0 or j < 0 or j >= n or i >= n or m[i][j] == 0: return c = m[i][j] m[i][j] = 0 top = self.getAns(m, n, i - 1, j, res, ans +...
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VA...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): if n == 0 or m[0][0] == 0 or m[n - 1][n - 1] == 0: return [-1] moves = [(-1, 0, "U"), (1, 0, "D"), (0, -1, "L"), (0, 1, "R")] result = [] def dfs(r, c, slate): if r < 0 or r >= n or c < 0 or c >= n or m[r][c] != 1: ...
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN LIST NUMBER ASSIGN VAR LIST NUMBER NUMBER STRING NUMBER NUMBER STRING NUMBER NUMBER STRING NUMBER NUMBER STRING ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN IF V...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): res = [] visited = [[(0) for i in range(n)] for j in range(n)] i = j = 0 if m[i][j] == 1: self.func(m, n, i, j, "", res, visited) return res def func(self, m, n, i, j, sub, res, visited): if i == n - 1 and j ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR STRING VAR VAR RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR NUMBER VAR VAR BIN...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): if m[n - 1][n - 1] == 0 or m[0][0] == 0: return [] lst = [] visited = [[(0) for i in range(n)] for j in range(n)] self.dfs(0, 0, m, visited, lst, "", n) return lst def dfs(self, i, j, m, visited, lst, ans, n): ...
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER RETURN LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR STRING VAR RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CAL...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def f(self, i, j, mat, n, ans, vis, s): if i == n - 1 and j == n - 1: ans.append("".join(s)) return if i < n - 1: if mat[i + 1][j] == 1 and vis[i + 1][j] == 0: s.append("D") vis[i + 1][j] = 1 self.f(...
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VA...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: ans = [] arr = [] def gen(self, grid, n, i, j, visited): if i == n - 1 and j == n - 1: g = self.arr.copy() self.ans.append("".join(g)) return visited[i][j] = 1 if i - 1 >= 0 and visited[i - 1][j] == 0 and grid[i - 1][j] == 1: ...
CLASS_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER V...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): result = [] getPaths(m, n, result, "", 0, 0) return result def getPaths(m, n, result, temp, r, c): if m[r][c] != 1: return m[r][c] = 2 if r == n - 1 and c == n - 1: result.append(temp) nbrs = getNeighbours(m, r, c) ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR STRING NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def solve(self, m, n, x, y, visited, path, answer): if x == n - 1 and y == n - 1 and m[x][y] != 0: answer.append(path) return if -1 < x < n and -1 < y < n and not visited[x][y] and m[x][y] != 0: visited[x][y] = 1 self.solve(m, n, x + 1...
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NU...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def isValid(self, x, y, m, n): if x < 0 or y < 0 or x >= n or y >= n: return False if m[x][y] == 0: return False return True def sol(self, x, y, m, n, path, visited, dest): if x == n - 1 and y == n - 1: dest.append(path) ...
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VA...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): vis = [[(0) for i in range(n)] for j in range(n)] if m[0][0] == 0: return [] return self.func(m, n, "", [], 0, 0, vis) def func(self, m, n, str1, paths, i, j, vis): if i == n - 1 and j == n - 1 and m[i][j] == 1: ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER RETURN LIST RETURN FUNC_CALL VAR VAR VAR STRING LIST NUMBER NUMBER VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR NUMBER IF BI...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, a, n): vis = [[(0) for i in range(n)] for j in range(n)] drow = [-1, 0, 1, 0] dcol = [0, 1, 0, -1] ans = [] def solve(row, col, s): if row == n - 1 and col == n - 1: ans.append(s) return ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER AS...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): def dfs(x, y, path, res, visited): if x < 0 or x >= n or y < 0 or y >= n or m[x][y] == 0: return if x == n - 1 and y == n - 1: res.append(path) return if (x, y) in visited: ...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VA...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): ans = [] def path_helper(i, j, m, n, vis, direction): if i == n - 1 and j == n - 1: vis[i][j] = 1 ans.append(direction) vis[i][j] = 0 return if i < 0 or j < 0 or i >= n...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBE...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): dir_ = [(-1, 0, "U"), (0, 1, "R"), (1, 0, "D"), (0, -1, "L")] ans = [] pathVisited = set() def isValid(x, y): return -1 < x <= n - 1 and -1 < y <= n - 1 and m[x][y] != 0 def rec(i, j, path): if (i, j) == (n ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER STRING NUMBER NUMBER STRING NUMBER NUMBER STRING NUMBER NUMBER STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): ans = [] def isSafe(m, visited, n, scrx, scry): if ( scrx >= 0 and scry >= 0 and scrx < n and scry < n and m[scrx][scry] == 1 and visited[scrx][scry...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR F...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): def ax(vis, i, j, s): if i < 0 or j < 0 or j >= n or i >= n: return False if m[i][j] == 0 or vis[i][j] == 1: return False if i == n - 1 and j == n - 1: r.append(s) ...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING EXPR FUNC_CALL V...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def solve(self, i, j, ans, res, mat, visited, n): if i == n - 1 and j == n - 1: res.append(ans) return if i < n - 1 and mat[i + 1][j] == 1 and visited[i + 1][j] != "v": visited[i][j] = "v" self.solve(i + 1, j, ans + "D", res, mat, visi...
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING IF VAR...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: dirs = list() visited = list() def findPath(self, m, n): result = list() self.visited = [([0] * n) for i in range(n)] self.dirs = [[0, 1], [1, 0], [-1, 0], [0, -1]] if m[0][0] == 1: self.helper(m, n, result, 0, 0, list()) return result ...
CLASS_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER FUNC_...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): ans = [] visited = [[(0) for _ in range(n)] for _ in range(n)] path = "" if m[0][0] == 0 or m[n - 1][n - 1] == 0: return [-1] self.solve(0, 0, m, n, ans, visited, path) return ans def isSafe(self, n, x, y, vi...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN LIST NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR V...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, matrix, n): ans = list() def util(i, j, coveredCoord, path): if (i, j) in coveredCoord or i < 0 or i >= n or j < 0 or j >= n: return if matrix[i][j] == 0: return if i == j == n - 1: ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN IF VAR VAR VAR NUMBER RETURN IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR ...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
fans = [] moves = [[0, 1, "R"], [0, -1, "L"], [1, 0, "D"], [-1, 0, "U"]] def valid(i, j, vis, arr): if ( i < 0 or j < 0 or i >= len(arr) or j >= len(arr[0]) or (i, j) in vis or arr[i][j] == 0 ): return False return True def mgc(arr, i, j, vis, ans)...
ASSIGN VAR LIST ASSIGN VAR LIST LIST NUMBER NUMBER STRING LIST NUMBER NUMBER STRING LIST NUMBER NUMBER STRING LIST NUMBER NUMBER STRING FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR BIN_OP FUNC_CALL...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): def solve(i, j, move): if i == n - 1 and j == n - 1: ans.append(move) return d = "DLRU" for k in range(4): ni = i + di[k] nj = j + dj[k] if ( ...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXP...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution(object): def findPath(self, board, n): def backtracking(row, col, combination): if not (0 <= row < n and 0 <= col < n): return if board[row][col] == "V" or board[row][col] == 0: return if row == col == n - 1: ...
CLASS_DEF VAR FUNC_DEF FUNC_DEF IF NUMBER VAR VAR NUMBER VAR VAR RETURN IF VAR VAR VAR STRING VAR VAR VAR NUMBER RETURN IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EX...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
def sol(row, col, n, mat, path, ans, dRow, dCol): if col == n - 1 and row == n - 1: ans.append(path) return ans d = "DLRU" for i in range(4): nextRow = row + dRow[i] nextCol = col + dCol[i] if ( nextRow < n and nextCol < n and nextR...
FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): matrix = m if matrix[0][0] == 0 or matrix[n - 1][n - 1] == 0: return [-1] visited_vals = [(0, 0)] solutions = [] def helper(i, j, direction, path): path += direction if i >= n - 1 and j >= n - 1: ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR IF VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN LIST NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST FUNC_DEF VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPathHelper(self, i, j, a, n, ans, move, vis): if i == n - 1 and j == n - 1: ans.append(move) return if i + 1 < n and not vis[i + 1][j] and a[i + 1][j] == 1: vis[i][j] = 1 self.findPathHelper(i + 1, j, a, n, ans, move + "D", vis...
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR STRING VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
def isValid(i, j, m, n, curr_ans, vis, fin_ans): return i >= 0 and j >= 0 and i < n and j < n and m[i][j] == 1 and vis[i][j] == False def findPathUtil(i, j, m, n, ans, vis, fin_ans): vis[i][j] = True if i == n - 1 and j == n - 1: fin_ans.append(ans) if isValid(i + 1, j, m, n, ans, vis, fin_ans...
FUNC_DEF RETURN VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER V...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findpaths(self, matrix, n, i, j, seq): if i == n - 1 and j == n - 1: self.res.append(seq) return if i < 0 or j < 0 or i == n or j == n: return if matrix[i][j] == 0: return matrix[i][j] = 0 for direction, (mo...
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN IF VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): visited = set() ans = [] def solve(r, c, st): if r < 0 or c < 0 or r >= n or c >= n or m[r][c] == 0 or (r, c) in visited: return if r == n - 1 and c == n - 1: ans.append(st) re...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING EXPR FUNC...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): row = 0 col = 0 arr = [] p = "" if m[n - 1][n - 1] == 0: return [] def dfs(p, row, col, arr): if row == n - 1 and col == n - 1: arr.append(p) return if row ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN LIST FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN IF VAR VAR VAR NUMBER RETURN ASSIGN...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
def rat_in_maze_rec(m, i, j, res, path, n, vis, di, dj): if i == n - 1 and j == n - 1: res.append("".join(path[:])) return dir = "DLRU" for idx in range(4): nexti = i + di[idx] nextj = j + dj[idx] if ( 0 <= nexti < n and 0 <= nextj < n ...
FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CAL...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def safe(self, x, y, n, v, m): if (x >= 0 and x < n) and (y >= 0 and y < n) and v[x][y] == 0 and m[x][y] == 1: return 1 else: return 0 def sol(self, x, y, m, n, path, ans, v): if x == n - 1 and y == n - 1: ans.append(path) ...
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP V...
Consider a rat placed at (0, 0) in a square matrix of order N * N. It has to reach the destination at (N - 1, N - 1). Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matr...
class Solution: def findPath(self, m, n): visited = [[(0) for _ in range(n)] for _ in range(n)] results = [] def path(cur_path, i, j): if i == n - 1 and j == n - 1: results.append("".join(cur_path)) return if m[i][j] == 0: ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN IF VAR VAR VAR NUMBER RETURN IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ...