description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
def call(n, m, matrix): res = [([-1] * m) for i in range(n)] row = [0] * n col = [0] * m for i in range(n): for j in range(m): if matrix[i][j]: row[i] = 1 col[j] = 1 if sum(row) + sum(col) != 0: for i in range(n): for j in range(m): if matrix[i][j]: res[i][j] = 0 elif row[i] == 1 or col[j] == 1: res[i][j] = 1 else: res[i][j] = 2 return res t = int(input()) while t > 0: n, m = list(map(int, input().split())) matrix = [list(map(int, list(input()))) for i in range(n)] res = call(n, m, matrix) for row in res: for ele in row: print(ele, end=" ") print() t -= 1
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
T = int(input()) for i in range(0, T): inputs = input().split() inputs = list(map(int, inputs)) N = inputs[0] M = inputs[1] matrix = [] output_matrix = [[(-1) for q in range(0, M)] for p in range(0, N)] impossible = True for j in range(0, N): row = list(input()) row = list(map(int, row)) if 1 in row: impossible = False matrix.append(row) if impossible == True: for j in range(0, N): print(*output_matrix[j], sep=" ") else: for p in range(0, N): for q in range(0, M): if matrix[p][q] == 1: output_matrix[p][q] = 0 elif 1 in matrix[p] or 1 in [row[q] for row in matrix]: output_matrix[p][q] = 1 else: output_matrix[p][q] = 2 for j in range(0, N): print(*output_matrix[j], sep=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
mod = 1000000007 read_int = lambda: int(input().strip()) read_str = lambda: input().strip() read_str_arr = lambda: input().strip().split() read_int_arr = lambda: [int(x) for x in input().strip().split()] def solve(): N, M = read_int_arr() r = set() c = set() rc = set() for i in range(N): row = input() for j in range(M): if row[j] == "1": r.add(i) c.add(j) rc.add(tuple([i, j])) ans = [([None] * M) for _ in range(N)] for i in range(N): for j in range(M): if (i, j) in rc: ans[i][j] = 0 elif i in r or j in c: ans[i][j] = 1 elif len(rc) == 0: ans[i][j] = -1 else: ans[i][j] = 2 for row in ans: print(*row) for _ in range(int(input())): solve()
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for _ in range(int(input())): n, m = map(int, input().split()) arr = [] for _ in range(n): s = input() arr.append(s) res = [([-1] * m) for i in range(n)] r = [0] * n col = [0] * m for i in range(n): for j in range(m): if arr[i][j] == "1": r[i] = 1 col[j] = 1 if sum(r) + sum(col) != 0: for i in range(n): for j in range(m): if arr[i][j] == "1": res[i][j] = 0 elif r[i] == 1 or col[j] == 1: res[i][j] = 1 else: res[i][j] = 2 for i in res: print(*i)
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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) for _ in range(t): flag = 0 n, m = [int(x) for x in input().split()] A = [None] * n for i in range(n): sint = input() inp = [None] * m for j in range(m): inp[j] = int(sint[j]) A[i] = inp B = [[(-1) for j in range(m)] for i in range(n)] row = {} column = {} for i in range(n): row[i] = 0 for j in range(m): column[j] = 0 for i in range(n): for j in range(m): if A[i][j] == 1: flag = 1 row[i] = 1 column[j] = 1 B[i][j] = 0 if flag == 0: pass else: for i in range(n): for j in range(m): if A[i][j] == 0: if row[i] == 1 or column[j] == 1: B[i][j] = 1 else: B[i][j] = 2 for i in range(n): print(*B[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
mod = 1000000007 def integer_list(): return list(map(int, input().split())) t = int(input()) for _ in range(t): r, c = integer_list() matix = [list(input()) for i in range(r)] row_ones = set() col_ones = set() for i in range(r): for j in range(c): if matix[i][j] == "1": row_ones.add(i) col_ones.add(j) ans = [[(-1) for i in range(c)] for j in range(r)] if not row_ones and not col_ones: for i in range(r): print(*ans[i]) else: ans = [[(2) for i in range(c)] for j in range(r)] for i in range(r): if i in row_ones: for j in range(c): if matix[i][j] == "1": ans[i][j] = "0" else: ans[i][j] = "1" for i in range(c): if i in col_ones: for j in range(r): if matix[j][i] == "1": ans[j][i] = "0" else: ans[j][i] = "1" for i in range(r): print(*ans[i])
ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
T = int(input()) for _ in range(T): N, M = map(int, input().split()) R = [(False) for i in range(N)] C = [(False) for i in range(M)] mat = [] for n in range(N): r = list(input().strip()) for m in range(M): if r[m] == "1": R[n] = True C[m] = True mat.append(r) if sum(R) or sum(C): for n in range(N): for m in range(M): if mat[n][m] == "1": mat[n][m] = "0" elif R[n] or C[m]: mat[n][m] = "1" else: mat[n][m] = "2" for row in mat: print(*row, sep=" ") else: for i in range(N): print(*([-1] * M), sep=" ")
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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING IF VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR STRING
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for _ in range(int(input())): r, c = map(int, input().split()) a = [] s = 0 for i in range(r): b = list(input()) a.append(b) s += b.count("1") if s == 0: for i in range(r): for j in range(c): print("-1", end=" ") print() else: d = [] for j in range(c): b = [] for i in range(r): if a[i][j] == "1": a[i][j] = 1 else: a[i][j] = 0 b.append(a[i][j]) d.append(b) for i in range(r): for j in range(c): if a[i][j] == 1: print(0, end=" ") elif sum(a[i]) > 0 or sum(d[j]) > 0: print(1, end=" ") else: print(2, end=" ") print()
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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = input() t = int(t) while t: t = t - 1 n_m = input() n_m = n_m.split(" ") n = int(n_m[0]) m = int(n_m[1]) matrix = [] for i in range(n): row = input() row = list(row) row = list(map(int, row)) matrix.append(row) v_rows = [False] * n v_cols = [False] * m ans = [[(-1) for j in range(m)] for i in range(n)] exist = False for i in range(n): for j in range(m): if matrix[i][j] == 1: exist = True v_rows[i] = True v_cols[j] = True for i in range(n): for j in range(m): if matrix[i][j] == 1: ans[i][j] = 0 elif v_rows[i] == True: ans[i][j] = 1 elif v_cols[j] == True: ans[i][j] = 1 elif exist: ans[i][j] = 2 else: ans[i][j] = -1 for i in range(n): col_j = "" for j in range(m): col_j = col_j + str(ans[i][j]) + " " print(col_j)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
def findcol(j): for _ in range(N): if r[_][j] == "1": return 1 return 0 def findrow(i): for _ in range(M): if r[i][_] == "1": return 1 return 0 T = int(input()) while T: T = T - 1 l = input().split() N, M = int(l[0]), int(l[1]) ans = [[(0) for j in range(M)] for i in range(N)] r = [] found = 0 for _ in range(N): r.append(input()) for __ in range(M): if found == 0 and r[_][__] == "1": found = 1 if found == 0: for _ in range(N): for __ in range(M): print("-1 ", end="") print() else: for i in range(N): for j in range(M): if r[i][j] == "1": ans[i][j] = 0 elif findcol(j) or findrow(i): ans[i][j] = 1 else: ans[i][j] = 2 print(ans[i][j], end=" ") print()
FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) for _ in range(t): n, m = map(int, input().split()) mx = [] r = [] c = [] z = 0 for i in range(n): a = list(input()) z += a.count("1") r.append(a.count("1")) mx.append(a) for i in range(m): for j in range(n): if mx[j][i] == "1": c.append(1) break else: c.append(0) if z == 0: ans = n * [m * [-1]] print("\n".join(" ".join(str(i) for i in row) for row in ans)) else: for i in range(n): a = "" for j in range(m): if mx[i][j] == "1": a += "0 " elif c[j] != 0 or r[i] != 0: a += "1 " else: a += "2 " print(a[:-1])
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 LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR LIST BIN_OP VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR STRING IF VAR VAR NUMBER VAR VAR NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for t in range(int(input())): l = [] n, m = map(int, input().split()) for i in range(n): x = input() l.append(x) row = [0] * (n + 1) col = [0] * (m + 1) fl = 0 for i in range(n): for j in range(m): if int(l[i][j]): row[i] = 1 col[j] = 1 fl = 1 if not fl: for i in range(n): for j in range(m): print(-1, end=" ") print() continue for i in range(n): for j in range(m): if not int(l[i][j]): if row[i] or col[j]: print(1, end=" ") else: print(2, end=" ") else: print(0, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) while t: dim = input().split() n = int(dim[0]) m = int(dim[1]) a = [] for i in range(n): temp = [] s = input() for j in range(m): temp.append(int(s[j])) a.append(temp) res = [[(0) for i in range(m)] for j in range(n)] row = [0] * n col = [0] * m for i in range(n): for j in range(m): if a[i][j] == 1: row[i] = 1 col[j] = 1 r = 0 c = 0 for i in range(n): if row[i] == 0: r += 1 for j in range(m): if col[j] == 0: c += 1 for i in range(n): for j in range(m): if r == n and c == m: res[i][j] = -1 elif a[i][j] == 1: res[i][j] = 0 elif row[i] == 1 or col[j] == 1: res[i][j] = 1 else: res[i][j] = 2 for i in range(n): for j in range(m): print(res[i][j], end=" ") print() t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) for _ in range(t): n, m = map(int, input().split()) arr = [] result = [] for _ in range(n): temp = list(input()) result_temp = [0] * len(temp) arr.append(temp) result.append(result_temp) flag = 0 for i in range(n): for j in range(m): if arr[i][j] == "1": flag = 1 break if flag == 0: for i in range(n): for j in range(m): print(-1, end=" ") print() else: for i in range(n): for j in range(m): if arr[i][j] == "1": result[i][j] = 0 else: f = 0 for k in range(n): if arr[k][j] == "1": f = 1 break for k in range(m): if arr[i][k] == "1": f = 1 break if f == 1: result[i][j] = 1 else: result[i][j] = 2 for i in range(n): for j in range(m): print(result[i][j], end=" ") print()
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 LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for _ in range(int(input())): x, y = map(int, input().split()) l = [[int(x) for x in input().strip()] for y in range(x)] ans = [([2] * y) for k in range(x)] temp = 0 rows = set() col = set() for i in range(x): for j in range(y): if l[i][j] == 1: temp = 1 rows.add(i) col.add(j) ans[i][j] = 0 for ii in rows: for var in range(y): if ans[ii][var] == 2: ans[ii][var] = 1 for jj in col: for var in range(x): if ans[var][jj] == 2: ans[var][jj] = 1 if temp == 0: ans = [([-1] * y) for k in range(x)] for li in ans: print(*li)
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 VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) while t > 0: t -= 1 n, m = input().split(" ") n, m = int(n), int(m) a = [] for i in range(n): row = input() r = list(row) for i in range(m): r[i] = int(r[i]) a.append(r) s = 0 r = [] c = [] for i in range(n): r.append(sum(a[i])) s += r[i] for i in range(m): v = 0 for j in range(n): v += a[j][i] c.append(v) f = [[(0) for x in range(m)] for y in range(n)] for i in range(n): for j in range(m): if s == 0: f[i][j] = "-1" elif a[i][j] != 0: f[i][j] = "0" elif r[i] != 0 or c[j] != 0: f[i][j] = "1" else: f[i][j] = "2" for i in range(n): print(" ".join(f[i]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR STRING IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) for _ in range(t): n, m = map(int, input().split()) matrix = [] colCheck = [False] * m rowCheck = [False] * n atleast1 = False for i in range(n): rows = [int(i) for i in list(input())] if 1 in rows: rowCheck[i] = True atleast1 = True for j in range(m): if rows[j] == 1: colCheck[j] = True matrix.append(rows) for i in range(n): for j in range(m): if matrix[i][j] == 1: print(0, end=" ") elif rowCheck[i] == True or colCheck[j] == True: print(1, end=" ") elif atleast1 == True: print(2, end=" ") else: print(-1, end=" ") print()
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 LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) while t > 0: t -= 1 n, m = [int(x) for x in input().split()] row = [0] * n col = [0] * m arr = [] for i in range(n): temp = [0] * m arr.append([0] * m) s = str(input()) for j in range(m): p = int(s[j]) arr[i][j] = p row[i] = max(row[i], p) col[j] = max(col[j], p) if row.count(1) == 0 and col.count(1) == 0: for i in range(n): print(*([-1] * m)) continue else: for i in range(n): temp = [] for j in range(m): if arr[i][j] == 1: temp.append(0) elif row[i] == 1 or col[j] == 1: temp.append(1) else: temp.append(2) print(*temp)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for _ in range(int(input())): n, m = map(int, input().split()) l = ["0"] * n check = 0 for t in range(n): l1 = list(str(input())) l[t] = l1 hello = set(l1) if "1" in hello: check = 1 if check == 0: main = [["-1"] * m] * n for t in main: ans = " ".join(t) print(ans) else: main = [] for t in range(n): k = [] for y in range(m): k.append("0") main.append(k) for y in range(n): for i in range(m): elem = l[y][i] if elem == "1": main[y][i] = "0" else: check1 = l[y] c, fu = 0, 0 if "1" in check1: main[y][i] = "1" c = 1 if c == 0: check2 = [l[x][i] for x in range(n)] if "1" in check2: main[y][i] = "1" fu = 1 if c == 0 and fu == 0: main[y][i] = "2" for t in main: ans = " ".join(t) print(ans)
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 BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP LIST STRING VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF STRING VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF STRING VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for t in range(int(input())): n, m = map(int, input().split()) a = [] for i in range(n): a.append(input()) s = list(set(a)) if len(s) == 1 and "1" not in s[0]: for i in range(n): for i in range(m): print("-1", end=" ") print() else: r = set() c = set() for i in range(n): if "1" in a[i]: r.add(i) for i in range(m): for j in range(n): if a[j][i] == "1": c.add(i) break for i in range(n): for j in range(m): if a[i][j] == "0": if i in r or j in c: print("1", end=" ") else: print("2", end=" ") else: print("0", end=" ") print()
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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF STRING VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for _ in range(int(input())): n, m = map(int, input().split()) arr = [] for _ in range(n): lst = list(input()) arr.append(lst) row = [0] * n col = [0] * m flag = 0 for i in range(n): for j in range(m): if arr[i][j] == "1": flag = 1 row[i] = 1 col[j] = 1 if flag: ans = [([0] * m) for _ in range(n)] for i in range(n): for j in range(m): if arr[i][j] == "0": if row[i] == 1 or col[j] == 1: ans[i][j] = 1 else: ans[i][j] = 2 else: ans = [([-1] * m) for _ in range(n)] for item in ans: print(*item)
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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) while t > 0: n, m = map(int, input().split(" ")) matrix = [[(0) for i in range(m)] for j in range(n)] fin = [[(0) for i in range(m)] for j in range(n)] check = [[(0) for i in range(m)] for j in range(n)] go = [[(0) for i in range(m)] for j in range(n)] res = [] ans = [] for i in range(0, n): ch = input() ch = list(ch) for j in range(0, m): matrix[i][j] = int(ch[j]) res.insert(0, [sum(x) for x in matrix]) res.insert(1, [sum(x) for x in zip(*matrix)]) for i in range(0, n): for j in range(0, m): fin[i][j] = res[0][i] + res[1][j] ans.insert(0, [sum(x) for x in fin]) ans.insert(1, [sum(x) for x in zip(*fin)]) for i in range(0, n): for j in range(0, m): check[i][j] = ans[0][i] + ans[1][j] for i in range(0, n): for j in range(0, m): if matrix[i][j] == 1: go[i][j] = 0 elif matrix[i][j] == 0: if fin[i][j] >= 1: go[i][j] = 1 elif fin[i][j] == 0: if check[i][j] >= 1: go[i][j] = 2 elif check[i][j] == 0: go[i][j] = -1 for i in range(0, n): print(*go[i]) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) while t > 0: a = input().split() n = int(a[0]) m = int(a[1]) l = [] for i in range(n): a = input() l.append(a) k = [[int(0) for i in range(m)] for j in range(n)] flag = 1 cols = [(False) for i in range(m)] rows = [(False) for i in range(n)] for i in range(n): for j in range(m): if l[i][j] == "1": flag = 0 if not rows[i]: rows[i] = True for x in range(m): k[i][x] = 1 if not cols[j]: cols[j] = True for x in range(n): k[x][j] = 1 for i in range(n): for j in range(m): if flag == 1: print(-1, " ", end="") elif l[i][j] == "1": print(0, " ", end="") elif k[i][j] == 0: print(2, " ", end="") else: print(k[i][j], " ", end="") print() t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING STRING IF VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER STRING STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR NUMBER
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) while t: n, m = map(int, input().split(" ")) a = [] b = [] c = [] e = 0 f = [] g = [] for i in range(n): a.append(input()) b = [] if int(a[i]) > 0: f.append(1) else: f.append(0) for j in range(m): b.append(int(a[i][j])) c.append(b) d = [sum(x) for x in zip(*c)] if sum(d) == 0: for i in range(n): e = 1 for j in range(m): print(-1, end=" ") print() else: for i in f: b = [] for j in d: b.append(i or j) g.append(b) for i in range(n): for j in range(m): if g[i][j] == c[i][j] and g[i][j] == 0: print(2, end=" ") elif c[i][j] == 1: print(0, end=" ") else: print(1, end=" ") print() print() t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for _ in range(int(input())): n, m = map(int, input().split()) a = {} b = {} l = [] for i in range(n): a[i] = 0 for i in range(m): b[i] = 0 for i in range(n): l.append(input()) for i in range(n): for j in range(m): if l[i][j] == "1": a[i] = 1 b[j] = 2 if sum(a.values()) + sum(b.values()) == 0: for i in range(n): for j in range(m): print(-1, end=" ") print() else: ans = [] for i in range(n): ans.append([0] * m) for i in range(n): for j in range(m): if l[i][j] == "1": ans[i][j] = 0 elif a[i] or b[j]: ans[i][j] = 1 else: ans[i][j] = 2 for i in range(n): for j in range(m): print(ans[i][j], end=" ") print()
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 DICT ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for t in range(int(input())): n, m = map(int, input().split(" ")) l1 = [] l = [] l2 = [] flag = 0 for i in range(n): l1 = list(map(int, input())) l2.append([-1] * m) l.append(l1) for i in l: if 1 in i: flag = 1 break if flag == 0: for i in l2: print(*i, sep=" ") continue for i in range(n): for j in range(m): if l[i][j] == 1: print(0, end=" ") else: flag = 0 for k in range(m): if l[i][k] == 1: flag = 1 print(1, end=" ") break if flag == 0: for k in range(n): if l[k][j] == 1: flag = 1 print(1, end=" ") break if flag == 0: print(2, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for _ in range(int(input())): N, M = map(int, input().split()) all_zero = True cols = [(0) for _ in range(M)] rows = [(0) for _ in range(N)] res = [["1" for _ in range(M)] for _ in range(N)] for i in range(N): A_row = input() for j in range(M): if A_row[j] == "1": all_zero = False cols[j] = 1 rows[i] = 1 res[i][j] = "0" for i in range(N): if all_zero: print("-1 " * (M - 1) + "-1") else: if not rows[i]: res[i] = [str(2 - cols[j]) for j in range(M)] print(" ".join(res[i]))
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 NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER STRING IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for _ in range(int(input())): n, m = map(int, input().split()) dp = [[(0) for i in range(m)] for i in range(n)] f = 0 mat = [] for i in range(n): l = list(input()) l = [int(i) for i in l] mat.append(l) if 1 in l: f = 1 if f == 0: for i in range(n): z = [-1] * m print(*z) else: bad_row = set() bad_col = set() for i in range(n): for j in range(m): if mat[i][j] == 1: dp[i][j] = 0 bad_row.add(i) bad_col.add(j) for i in bad_row: for j in range(m): if mat[i][j] != 1: dp[i][j] = 1 for j in bad_col: for i in range(n): if mat[i][j] != 1: dp[i][j] = 1 for i in range(n): for j in range(m): if i not in bad_row and j not in bad_col: dp[i][j] = 2 for i in range(n): print(*dp[i])
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 NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) while t > 0: t = t - 1 n, m = map(int, input().split()) L = [] for i in range(n): L.append([int(j) for j in input()]) p = [] q = [] res = [[-1] * m] * n for i in range(n): if 1 in L[i]: res[i] = [1] * m for i in range(m): if 1 in [j[i] for j in L]: for j in range(n): res[j][i] = 1 if sum(sum(j) for j in L) == 0: for i in range(n): print(*([-1] * m)) else: for i in range(n): for j in range(m): if L[i][j] == 1: res[i][j] = 0 if res[i][j] == -1: res[i][j] = 2 for i in range(n): print(*res[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) for _ in range(t): n, m = map(int, input().split()) l = [] ans = [] flag = 0 for i in range(n): k = list(input()) z = list(map(int, k)) l.append(z) if 1 in z and flag == 0: flag = 1 hash_rows = {} hash_columns = {} for i in range(n): hash_rows[i] = False for i in range(m): hash_columns[i] = False for i in range(n): for j in range(m): if l[i][j] == 1: hash_rows[i] = True hash_columns[j] = True for i in range(n): for j in range(m): if l[i][j] == 0: if hash_rows[i] == True or hash_columns[j] == True: l[i][j] = 1 elif flag == 1: l[i][j] = 2 else: l[i][j] = -1 else: l[i][j] = 0 for i in range(n): print(*l[i])
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 LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
GI = lambda: int(input()) GIS = lambda: map(int, input().split()) LGIS = lambda: list(GIS()) def main(): for t in range(GI()): n, m = GIS() mat = [[int(c) for c in input()] for _ in range(n)] cr = [int(any(x for x in row)) for row in mat] cc = [int(any(x for x in col)) for col in zip(*mat)] nat = [([-1] * m) for i in range(n)] if any((any(cr), any(cc))): for r in range(n): for c in range(m): if mat[r][c]: res = 0 elif cr[r] or cc[c]: res = 1 else: res = 2 nat[r][c] = res for row in nat: for c in row: print(c, end=" ") print() main()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
def findS(s, N, M): rows = [] cols = [] for i in range(M): temp = 0 for j in range(N): temp = temp | s[j][i] cols.append(temp) for i in s: temp = 0 for j in i: temp = temp | j rows.append(temp) y = [] d = {(0): 1, (1): 0} if rows.count(0) == N and cols.count(0) == M: return [[-1] * M] * N for i in s: temp = [] for j in i: temp.append(d[j]) y.append(temp) if 0 in rows and 0 in cols: for i in range(N): for j in range(M): if rows[i] == 0 and cols[j] == 0: y[i][j] = 2 return y T = int(input()) for i in range(T): N, M = [int(x) for x in input().split(" ")] A, t = [], [] for j in range(N): t = [int(x) for x in input()] A.append(t) x = findS(A, N, M) for i in x: print(" ".join([str(x) for x in i]))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER IF FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR RETURN BIN_OP LIST BIN_OP LIST NUMBER VAR VAR FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR 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 VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) for i in range(t): a = [] n, m = map(int, input().split()) for k in range(n): s = input() b = [] for j in s: b.append(int(j)) a.append(b) r = [(0) for j in range(n)] c = [(0) for j in range(m)] flag = 0 res = [[(0) for j in range(m)] for k in range(n)] for j in range(n): for k in range(m): if a[j][k] == 1: res[j][k] = 0 flag = 1 r[j] = 1 c[k] = 1 if flag == 0: for j in range(n): for k in range(m): print("-1", end=" ") print() else: for j in range(n): for k in range(m): if a[j][k] == 0: if r[j] == 1 or c[k] == 1: res[j][k] = 1 else: res[j][k] = 2 for j in range(n): for k in range(m): print(res[j][k], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) for i in range(t): n, m = map(int, input().split()) l = [] for j in range(n): mat = list(input()) for k in range(len(mat)): mat[k] = int(mat[k]) l.append(mat) l1 = [] q1 = [] q2 = [] for j in range(n): q1.append(sum(l[j])) for j in range(m): s = 0 for k in range(n): s += l[k][j] q2.append(s) p = [] for j in range(n): p1 = [] for k in range(m): p1.append([q1[j], q2[k]]) p.append(p1) check1 = 0 check2 = 0 for j in q1: if j != 0: check1 = 1 for j in q2: if j != 0: check2 = 1 if check1 == 0 and check2 == 0: for j in range(n): for k in range(m): print("-1", end=" ") print() else: for j in range(n): f = [] for k in range(m): if p[j][k][0] > 0 or p[j][k][1] > 0: if l[j][k] == 1: f.append(0) else: f.append(1) elif p[j][k][0] == 0 and p[j][k][1] == 0: f.append(2) l1.append(f) for j in range(n): for k in range(m): print(l1[j][k], end=" ") print()
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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for _ in range(int(input())): arr = [] N, M = list(map(int, input().split())) for i in range(N): Z = input() arr.append(Z) ans = [([-1] * M) for _ in range(N)] r = set() c = set() for i in range(N): for j in range(M): if arr[i][j] == "1": r.add(i) c.add(j) if len(c) != 0: for i in range(N): for j in range(M): if arr[i][j] == "1": ans[i][j] = 0 elif i in r or j in c: ans[i][j] = 1 else: ans[i][j] = 2 for i in range(N): print(*ans[i])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) while t: t -= 1 n, m = map(int, input().split()) a = [[int(d) for d in input()] for i in range(n)] if sum(sum(i) for i in a): sol = [[2] * m] * n for i in range(n): if 1 in a[i]: sol[i] = [1] * m for j in range(m): sol[i][j] -= a[i][j] for j in range(m): if 1 in [i[j] for i in a]: for i in range(n): sol[i][j] = 1 - a[i][j] else: sol = [[-1] * m] * n print("\n".join(" ".join(str(i) for i in row) for row in sol))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
for _ in range(int(input())): n, m = [int(i) for i in input().split()] a = [] for i in range(n): a.append([int(i) for i in input()]) r = [] c = [] for i in range(n): for j in range(m): if a[i][j] == 1: r.append(i) break for j in range(m): for i in range(n): if a[i][j] == 1: c.append(j) break if r == []: for i in range(n): print("-1 " * m) continue for i in range(n): for j in range(m): if a[i][j] == 1: print(0, end=" ") elif i in r or j in c: print(1, end=" ") else: print(2, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [list(map(int, input())) for _ in range(n)] if all(a[i][j] == 0 for i in range(n) for j in range(m)): for i in range(n): print(*([-1] * m)) else: b = [([2] * m) for _ in range(n)] rows = set(i for i in range(n) for j in range(m) if a[i][j]) cols = set(j for i in range(n) for j in range(m) if a[i][j]) for i in range(n): print( *( 0 if a[i][j] else 1 if i in rows or j in cols else 2 for j in range(m) ) )
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 FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
t = int(input()) while t > 0: t -= 1 n, m = [int(x) for x in input().split()] matrix = [] cnt = 0 for i in range(n): s = list(input()) cnt += s.count("0") matrix.append(s) matrix2 = [] for i in range(m): temp = [] for j in range(n): temp.append(matrix[j][i]) matrix2.append(temp) ans = [] for i in range(n): ans.append([-1] * m) if cnt != n * m: for i in range(n): for j in range(m): if matrix[i][j] != "1": l1, l2 = matrix[i], matrix2[j] if len(set(l1)) > 1 or len(set(l2)) > 1: ans[i][j] = 1 else: ans[i][j] = 2 else: ans[i][j] = 0 for row in ans: print(*row)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR IF VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a matrix of integers $A$ with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each element of this matrix is either $0$ or $1$. A move consists of the following steps: - Choose two different rows $r_1$ and $r_2$ or two different columns $c_1$ and $c_2$. - Apply the bitwise OR operation with the second row/column on the first row/column. Formally, if you chose two rows, this means you should change $A_{r_1, k}$ to $A_{r_1, k} \lor A_{r_2, k}$ for each $1 \le k \le M$; if you chose two columns, then you should change $A_{k, c_1}$ to $A_{k, c_1} \lor A_{k, c_2}$ for each $1 \le k \le N$. For each element of the matrix, compute the minimum number of moves required to make it equal to $1$ or determine that it is impossible. Note that these answers are independent, i.e. we are starting with the initial matrix for each of them. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains $M$ integers $A_{i, 1}, A_{i, 2}, \dots, A_{i, M}$ NOT separated by spaces. -----Output----- For each test case, print $N$ lines. For each valid $i$, the $i$-th of these lines should contain $M$ space-separated integers; for each valid $j$, the $j$-th of these integers should be the minimum number of moves required to make $A_{i, j}$ equal to $1$, or $-1$ if it is impossible. -----Constraints----- - $1 \le T \le 100$ - $1 \le N, M \le 1,000$ - $A_{i, j} \in \{0, 1\}$ for each valid $i, j$ - the sum of $N \cdot M$ for all test cases does not exceed $1,000,000$ -----Example Input----- 1 3 3 010 000 001 -----Example Output----- 1 0 1 2 1 1 1 1 0
def modifyMatrix(mat): row = [0] * R col = [0] * C for i in range(0, R): row[i] = 0 for i in range(0, C): col[i] = 0 for i in range(0, R): for j in range(0, C): if mat[i][j] == 1: row[i] = 1 col[j] = 1 for i in range(0, R): for j in range(0, C): if row[i] == 1 or col[j] == 1: mat[i][j] = 1 def printMatrix(mat): for i in range(0, R): for j in range(0, C): print(mat[i][j], end=" ") print() T = int(input()) while T > 0: n, m = input().split() n = int(n) m = int(m) R = n C = m a = [None] * n rout = 0 one = [] for i in range(n): a[i] = input() a[i] = list(a[i]) for j in range(m): a[i][j] = int(a[i][j]) if a[i][j] == 1: one.append([i, j]) rout = 1 aa = a[:] if rout == 0: for i in range(n): for j in range(m): aa[i][j] = -1 printMatrix(aa) else: modifyMatrix(aa) for i in range(n): for j in range(m): if aa[i][j] == 0: aa[i][j] = 2 for i in range(len(one)): aa[one[i][0]][one[i][1]] = 0 printMatrix(aa) T -= 1
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): sign = -1 if a >= 0 and b < 0 or a < 0 and b >= 0 else 1 a = abs(a) b = abs(b) result = len(range(0, a - b + 1, b)) if sign == -1: result *= -1 negative_limit = -(2**31) positive_limit = 2**31 - 1 result = min(max(result, negative_limit), positive_limit) return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, A: int, B: int) -> int: if A == -2147483648 and B == -1: return 2147483647 ans, sign = 0, 1 if A < 0: A, sign = -A, -sign if B < 0: B, sign = -B, -sign if A == B: return sign while A >= B: b = 0 while B << b <= A: b += 1 A -= B << b - 1 ans += 1 << b - 1 return -ans if sign < 0 else ans
CLASS_DEF FUNC_DEF VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR WHILE VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN VAR NUMBER VAR VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): tmp, q = 0, 0 sign = 1 if (a < 0) ^ (b < 0) == 0 else -1 a = abs(a) b = abs(b) for i in range(31, -1, -1): if tmp + (b << i) <= a: tmp += b << i q |= 1 << i return q * sign
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): minus_sign = (a < 0) != (b < 0) a = abs(a) b = abs(b) c, _ = self.itter(a, b, 1) return -c if minus_sign else c def itter(self, a, b2, c): if b2 > a: return 0, a c2, remainder = self.itter(a, b2 + b2, c + c) if b2 > remainder: return c2, remainder else: return c2 + c, remainder - b2
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR VAR RETURN BIN_OP VAR VAR BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
def solve(a, b): if b > a: return 0 else: k = 0 while 2**k * b <= a: k += 1 k -= 1 ans = 2**k + solve(a - 2**k * b, b) return ans class Solution: def divide(self, a, b): if a < 0 and b < 0 or a > 0 and b > 0: sign = 1 else: sign = -1 ans = solve(abs(a), abs(b)) return sign * ans
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): if b == 0: return 4294967295 div = abs(a) // abs(b) if (a < 0 or b < 0) and not (a < 0 and b < 0): return -div return div
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, dividend, divisor): sngl = False if dividend < 0 and divisor < 0: pass elif dividend < 0 or divisor < 0: sngl = True dividend = abs(dividend) divisor = abs(divisor) if divisor > dividend: return 0 result = len(range(divisor, dividend + 1, divisor)) if sngl: return -result minus_limit = -(2**31) plus_limit = 2**31 - 1 result = min(result, plus_limit) return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR RETURN VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): neg = -1 if (a < 0) ^ (b < 0) else 1 a = abs(a) b = abs(b) quotent = 0 temp = 0 for i in range(31, -1, -1): if temp + (b << i) <= a: temp += b << i quotent |= 1 << i return neg * quotent
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): c = 1 if a < 0: c = -1 * c a = -a if b < 0: b = -b c = -1 * c n = len(range(b, a + 1, b)) return n * c
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): if a == 0: return 0 if b == 0: return -1 sign = 1 if a < 0: a = -a sign = -sign if b < 0: b = -b sign = -sign if a == b: return sign quo = 0 while a >= b: x = 0 while a - (b << x) >= 0: x += 1 x -= 1 quo += 1 << x a -= b << x return ~quo + 1 if sign < 0 else quo
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): ans = 0 flag = False if a < 0 and b > 0 or a > 0 and b < 0: flag = True a = max(a, -1 * a) b = max(b, -1 * b) a, b = max(a, -1 * a), max(b, -1 * b) while a >= b: for i in range(32): if a < b << i: break i += 1 a -= b << i - 1 ans += 1 << i - 1 if flag: return -1 * ans return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR RETURN BIN_OP NUMBER VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): temp = 0 ans = 0 neg = False if (a < 0) ^ (b < 0): neg = True if a < 0: a = -a if b < 0: b = -b for i in range(31, -1, -1): if temp + (b << i) <= a: temp = temp + (b << i) ans = ans | 1 << i if neg: ans = -ans return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR ASSIGN VAR VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): sign = 1 if a < 0: sign *= -1 a = -a if b < 0: sign *= -1 b = -b return a // b * sign
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): x, y = abs(a), abs(b) if a < 0 and b > 0 or a > 0 and b < 0: return -1 * (x // y) return x // y
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP NUMBER BIN_OP VAR VAR RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, dividend, divisor): INT_MIN = -2147483647 - 1 INT_MAX = 2147483647 if dividend == INT_MIN and divisor == -1: return INT_MAX dvd = abs(dividend) dvs = abs(divisor) ans = 0 sign = -1 if (dividend > 0) ^ (divisor > 0) else 1 while dvd >= dvs: temp = dvs steps = 1 while temp << 1 <= dvd: temp <<= 1 steps <<= 1 dvd -= temp ans += steps return sign * ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): flag = 0 if a < 0: flag = 1 a = abs(a) if b < 0: if flag == 1: flag = 0 else: flag = 1 b = abs(b) t = 0 q = 0 for i in range(31, -1, -1): if t + (b << i) <= a: t += b << i q = q | 1 << i if flag == 1: return -q return q
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER RETURN VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): if a == b: return 1 boolean = (a >= 0) == (b >= 0) a = abs(a) b = abs(b) res = 0 while a >= b: count = 0 while a >= b << 1 + count: count += 1 res += 1 << count a = a - (b << count) if boolean: return res return -res
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR RETURN VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): if a * b < 1: if a / b == int(a / b): return a // b return a // b + 1 return a // b
CLASS_DEF FUNC_DEF IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): if b == 0: return float("inf") if a >= 0 else float("-inf") if a == 0: return 0 sign = -1 if (a < 0) ^ (b < 0) else 1 dividend, divisor = abs(a), abs(b) quotient = 0 temp = 0 for i in range(31, -1, -1): if temp + (divisor << i) <= dividend: temp += divisor << i quotient |= 1 << i return sign * quotient
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): divident = a divisor = b if divident == 0 or divisor == 0: return 0 if divident > 0 and divisor > 0 or divident < 0 and divisor < 0: sign = 1 elif divident < 0 and divisor > 0 or divident > 0 and divisor < 0: sign = -1 divident = abs(divident) divisor = abs(divisor) quotient = 0 while divident >= divisor: x = 0 while divident >= divisor << x: x += 1 x -= 1 quotient += 1 << x divident -= divisor << x return quotient * sign
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, A, B): if A == -2147483648 and B == -1: return 2147483647 a, b, res = abs(A), abs(B), 0 for x in range(32)[::-1]: if (a >> x) - b >= 0: res += 1 << x a -= b << x return res if (A > 0) == (B > 0) else -res
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER VAR NUMBER VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): sign = -1 if (a < 0) ^ (b < 0) else 1 divident, divisor = a, b divident = abs(divident) divisor = abs(divisor) q = 0 for i in range(32, -1, -1): if divisor << i <= divident: divident -= divisor << i q = q | 1 << i if sign == 1: return q return -q
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER RETURN VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, di, d): a = abs(di) b = abs(d) ans = 0 while a >= b: sum = b count = 1 while sum <= a - sum: sum += sum count += count ans += count a -= sum if di < 0 and d > 0 or di > 0 and d < 0: return -ans return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): if b < 0 and a > 0: return a // (b * -1) * -1 elif b > 0 and a < 0: return a * -1 // b * -1 else: return a // b
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): c = 1 a1 = a b1 = b if a < 0: a = -a if b < 0: b = -b n = len(range(b, a + 1, b)) if a1 < 0 and b1 > 0 or a1 > 0 and b1 < 0: return -n return n
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): q = 0 a1 = a b1 = b if a < 0 and b > 0: a = -a if b < 0 and a > 0: b = -b if a < 0 and b < 0: a = -a b = -b while a >= b: for i in range(pow(10, 3)): if b << i > a: break a -= b << i - 1 q += 1 << i - 1 if a1 < 0 and b1 > 0: return -q if b1 < 0 and a1 > 0: return -q if a1 < 0 and b1 < 0: return q return q
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): limit = 2**31 res = int(a / b) if res > limit - 1: return limit - 1 if res < -limit: return -limit return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR RETURN VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): if a == 0 or b == 0: if b == 0: if a >= 0: return 4294967295 return -4294967295 else: return 0 ans = 0 sign = 1 if a < 0 and b > 0 or a > 0 and b < 0: sign = -1 a = abs(a) b = abs(b) def fun(p, q, c): temp = 0 for k in range(32): t = p if q & 1 << k: t = t << k temp += t if temp <= c: return True return False i, j = 0, a while i <= j: mid = (i + j) // 2 if fun(mid, b, a): ans = max(ans, mid) i = mid + 1 else: j = mid - 1 if sign == -1: ans = -ans return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, A, B): isNeg = 1 if (A < 0 or B < 0) and not (A < 0 and B < 0): isNeg = -1 A = abs(A) B = abs(B) i = 31 temp = 0 q = 0 while i >= 0: if (B << i) + temp <= A: temp = (B << i) + temp q += 1 << i i -= 1 return q * isNeg
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): result = 0 sign = 1 if a < 0 and b < 0: sign = 1 elif a < 0 or b < 0: sign = -1 a, b = abs(a), abs(b) while a - b >= 0: count = 0 while a - (b << 1 << count) >= 0: count += 1 result += 1 << count a = a - (b << count) if sign == 1: return result elif sign == -1: return -result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): s = 0 k = b l = a if b < 0: b = abs(b) if a < 0: a = abs(a) p = len(str(a)) q = 10 ** (p - 1) z = b * q i = 1 while a >= b: if z <= a: a = a - z s = s + q else: q = 10 ** (p - 1 - i) z = b * q i = i + 1 if k < 0 and l < 0: return s if k < 0 or l < 0: return s - 2 * s return s
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): i = 31 ans = 0 t = 0 sign = -1 if (a < 0) ^ (b < 0) else 1 a = abs(a) b = abs(b) while i >= 0: if t + (b << i) <= a: ans += 1 << i t += b << i i -= 1 if sign == -1: ans *= -1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): sign = -1 if a < 0 or b < 0 else 1 if a < 0 and b < 0: sign = 1 a = abs(a) b = abs(b) result = 0 for i in range(31, -1, -1): if b << i <= a: result += 1 << i a -= b << i return sign * result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): if a // b > 0: return a // b else: if int(a / b) == a // b: return a // b return a // b + 1
CLASS_DEF FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, a, b): a_neg, b_neg = False, False if a < 0: a_neg = True if b < 0: b_neg = True a = abs(a) b = abs(b) q = 0 while a >= b: c = 0 while a >= b << c: c += 1 c -= 1 q = q + (1 << c) a = a - (b << c) return -q if a_neg ^ b_neg else q
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR VAR VAR
Given two integers dividend and divisor. Find the quotient after dividing dividend by divisor without using multiplication, division and mod operator. Example 1: Input: a = 10, b= 3 Output: 3 Exaplanation: 10/3 gives quotient as 3 and remainder as 1. Example 2: Input: a = 43, b = -8 Output: -5 Explanation: 43/-8 gives quotient as -5 and remainder as 3. Your task: You don't have to read input or print anything. Your task is to complete the function divide() which takes two integers a and b as input and returns the quotient after dividing a by b. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints : -10^9 <= a,b <= 10^9
class Solution: def divide(self, dividend, divisor): dd = dividend ds = divisor is_neg = False if dd < 0 and ds >= 0: is_neg = True elif dd >= 0 and ds < 0: is_neg = True dd = abs(dd) ds = abs(ds) original = ds total = 0 multi = 1 while dd >= original: if dd >= ds: dd = dd - ds ds = ds + ds total += multi multi = multi + multi else: ds = original multi = 1 if is_neg: total = -total total = max(total, -(2**31)) total = min(total, 2**31 - 1) return total
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is really interested in the XOR operation. He wants to take a sequence $A_{0}, A_{1}, \ldots, A_{N-1}$ and for each $i$ from $0$ to $K-1$ inclusive (in this order, one by one), perform the following operation: Let's denote $a = A_{i\%N}$ and $b = A_{N-(i\%N)-1}$ before this operation. Change $A_{i\%N}$ to $a \oplus b$, i.e. $a$ XOR $b$. Since Chef is busy, he asked you to find the final sequence he should get after performing these operations. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains $N$ space-separated integers $A_{0}, A_{1}, \ldots, A_{N-1}$. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers ― the elements of the final sequence. ------ Constraints ------ $1 ≤ T ≤ 70$ $1 ≤ N ≤ 10^{4}$ $1 ≤ K ≤ 10^{12}$ $1 ≤ A_{i} ≤ 10^{7}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ K ≤ 10^{6}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 2 2 1 2 ----- Sample Output 1 ------ 3 1
n = int(input()) for i in range(n): m, k = map(int, input().split()) l = list(map(int, input().split())) if k >= m * 3: k = m * 3 + k % (m * 3) for j in range(k): r = j % m a = l[r] b = l[m - r - 1] l[r] = a ^ b print(*l)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is really interested in the XOR operation. He wants to take a sequence $A_{0}, A_{1}, \ldots, A_{N-1}$ and for each $i$ from $0$ to $K-1$ inclusive (in this order, one by one), perform the following operation: Let's denote $a = A_{i\%N}$ and $b = A_{N-(i\%N)-1}$ before this operation. Change $A_{i\%N}$ to $a \oplus b$, i.e. $a$ XOR $b$. Since Chef is busy, he asked you to find the final sequence he should get after performing these operations. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains $N$ space-separated integers $A_{0}, A_{1}, \ldots, A_{N-1}$. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers ― the elements of the final sequence. ------ Constraints ------ $1 ≤ T ≤ 70$ $1 ≤ N ≤ 10^{4}$ $1 ≤ K ≤ 10^{12}$ $1 ≤ A_{i} ≤ 10^{7}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ K ≤ 10^{6}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 2 2 1 2 ----- Sample Output 1 ------ 3 1
t = int(input()) while t > 0: n, k = list(map(int, input().split())) l = list(map(int, input().split())) i = 0 if k >= n * 3: k = n * 3 + k % (n * 3) while i < k: a = l[i % n] b = l[n - i % n - 1] l[i % n] = a ^ b i += 1 for i in l: print(i, " ", end="") t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL 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 NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is really interested in the XOR operation. He wants to take a sequence $A_{0}, A_{1}, \ldots, A_{N-1}$ and for each $i$ from $0$ to $K-1$ inclusive (in this order, one by one), perform the following operation: Let's denote $a = A_{i\%N}$ and $b = A_{N-(i\%N)-1}$ before this operation. Change $A_{i\%N}$ to $a \oplus b$, i.e. $a$ XOR $b$. Since Chef is busy, he asked you to find the final sequence he should get after performing these operations. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains $N$ space-separated integers $A_{0}, A_{1}, \ldots, A_{N-1}$. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers ― the elements of the final sequence. ------ Constraints ------ $1 ≤ T ≤ 70$ $1 ≤ N ≤ 10^{4}$ $1 ≤ K ≤ 10^{12}$ $1 ≤ A_{i} ≤ 10^{7}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ K ≤ 10^{6}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 2 2 1 2 ----- Sample Output 1 ------ 3 1
t = int(input()) for i in range(0, t): n, k = list(map(int, input().split())) l = list(map(int, input().split())) if k > n and n % 2 == 1: l[n // 2] = 0 num = k // n % 3 for j in range(0, num): for x in range(0, n): a = l[x % n] b = l[n - x % n - 1] l[x % n] = a ^ b for j in range(0, k % n): a = l[j % n] b = l[n - j % n - 1] l[j % n] = a ^ b print(*l, sep=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is really interested in the XOR operation. He wants to take a sequence $A_{0}, A_{1}, \ldots, A_{N-1}$ and for each $i$ from $0$ to $K-1$ inclusive (in this order, one by one), perform the following operation: Let's denote $a = A_{i\%N}$ and $b = A_{N-(i\%N)-1}$ before this operation. Change $A_{i\%N}$ to $a \oplus b$, i.e. $a$ XOR $b$. Since Chef is busy, he asked you to find the final sequence he should get after performing these operations. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains $N$ space-separated integers $A_{0}, A_{1}, \ldots, A_{N-1}$. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers ― the elements of the final sequence. ------ Constraints ------ $1 ≤ T ≤ 70$ $1 ≤ N ≤ 10^{4}$ $1 ≤ K ≤ 10^{12}$ $1 ≤ A_{i} ≤ 10^{7}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ K ≤ 10^{6}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 2 2 1 2 ----- Sample Output 1 ------ 3 1
t = int(input()) for i in range(t): n, k = map(int, input().split()) ar = list(map(int, input().split())) b = n * 3 if k > b: k = k % b + b for i in range(0, k): ar[i % n] = ar[i % n] ^ ar[n - i % n - 1] print(*ar)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is really interested in the XOR operation. He wants to take a sequence $A_{0}, A_{1}, \ldots, A_{N-1}$ and for each $i$ from $0$ to $K-1$ inclusive (in this order, one by one), perform the following operation: Let's denote $a = A_{i\%N}$ and $b = A_{N-(i\%N)-1}$ before this operation. Change $A_{i\%N}$ to $a \oplus b$, i.e. $a$ XOR $b$. Since Chef is busy, he asked you to find the final sequence he should get after performing these operations. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains $N$ space-separated integers $A_{0}, A_{1}, \ldots, A_{N-1}$. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers ― the elements of the final sequence. ------ Constraints ------ $1 ≤ T ≤ 70$ $1 ≤ N ≤ 10^{4}$ $1 ≤ K ≤ 10^{12}$ $1 ≤ A_{i} ≤ 10^{7}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ K ≤ 10^{6}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 2 2 1 2 ----- Sample Output 1 ------ 3 1
def sol(n, k, arr): if k >= n * 3: k = n * 3 + k % (n * 3) for i in range(k): arr[i % n] = arr[i % n] ^ arr[n - i % n - 1] return arr y = int(input()) for i in range(y): n, k = map(int, input().split()) arr = list(map(int, input().split())) print(*sol(n, k, arr))
FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is really interested in the XOR operation. He wants to take a sequence $A_{0}, A_{1}, \ldots, A_{N-1}$ and for each $i$ from $0$ to $K-1$ inclusive (in this order, one by one), perform the following operation: Let's denote $a = A_{i\%N}$ and $b = A_{N-(i\%N)-1}$ before this operation. Change $A_{i\%N}$ to $a \oplus b$, i.e. $a$ XOR $b$. Since Chef is busy, he asked you to find the final sequence he should get after performing these operations. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains $N$ space-separated integers $A_{0}, A_{1}, \ldots, A_{N-1}$. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers ― the elements of the final sequence. ------ Constraints ------ $1 ≤ T ≤ 70$ $1 ≤ N ≤ 10^{4}$ $1 ≤ K ≤ 10^{12}$ $1 ≤ A_{i} ≤ 10^{7}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ K ≤ 10^{6}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 2 2 1 2 ----- Sample Output 1 ------ 3 1
for i in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) if k >= n and n % 2 == 1: a[n // 2] = 0 x = k // n % 3 for j in range(x): for i in range(n): b = a[i] c = a[n - i - 1] a[i] = b ^ c y = k % n for i in range(y): b = a[i] c = a[n - i - 1] a[i] = b ^ c print(*a)
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 IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef is really interested in the XOR operation. He wants to take a sequence $A_{0}, A_{1}, \ldots, A_{N-1}$ and for each $i$ from $0$ to $K-1$ inclusive (in this order, one by one), perform the following operation: Let's denote $a = A_{i\%N}$ and $b = A_{N-(i\%N)-1}$ before this operation. Change $A_{i\%N}$ to $a \oplus b$, i.e. $a$ XOR $b$. Since Chef is busy, he asked you to find the final sequence he should get after performing these operations. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains $N$ space-separated integers $A_{0}, A_{1}, \ldots, A_{N-1}$. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers ― the elements of the final sequence. ------ Constraints ------ $1 ≤ T ≤ 70$ $1 ≤ N ≤ 10^{4}$ $1 ≤ K ≤ 10^{12}$ $1 ≤ A_{i} ≤ 10^{7}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ K ≤ 10^{6}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 2 2 1 2 ----- Sample Output 1 ------ 3 1
n = int(input()) for i in range(n): N, K = map(int, input().split()) lst = list(map(int, input().split())) a = lst[0] if K > N * 3: K = K % (N * 3) + N * 3 for i in range(K): a = lst[i % N] b = lst[N - i % N - 1] out = a ^ b a = out lst[i % N] = out print(*lst)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: wdMap = {} def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: res = [] self.wdMap = self.hashwords(words) for puzzle in puzzles: res.append(self.checkValidWord(puzzle, 0, 0)) return res def checkValidWord(self, puzzle, i, id): if i == len(puzzle): return self.wdMap.get(id, 0) indx = ord(puzzle[i]) - ord("a") nextid = id | 1 << indx if i == 0: return self.checkValidWord(puzzle, i + 1, nextid) else: return self.checkValidWord(puzzle, i + 1, id) + self.checkValidWord( puzzle, i + 1, nextid ) def hashwords(self, words): wdMap = {} for word in words: letter_id = 0 for letter in word: indx = ord(letter) - ord("a") letter_id = letter_id | 1 << indx wdMap[letter_id] = wdMap.get(letter_id, 0) + 1 return wdMap
CLASS_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Puzzle: def __init__(self, puzzle): self.first_letter = puzzle[0] self.puzzle = set(puzzle) class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: cnt = collections.Counter(frozenset(w) for w in words) res = [0] * len(puzzles) for i, puzzle in enumerate(puzzles): for bin_i in range(2**6, 2**7): mask = bin(bin_i)[2:] subset = frozenset([puzzle[j] for j in range(7) if mask[j] == "1"]) res[i] += cnt[subset] return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR STRING VAR VAR VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: orda = ord("a") mask = defaultdict(int) for w in words: m = 0 for c in w: m |= 1 << ord(c) - orda mask[m] += 1 res = [] for p in puzzles: ones = [] for c in p: ones.append(1 << ord(c) - orda) valid = [ones[0]] for i in range(1, 7): valid.extend([(ones[i] + v) for v in valid]) novw = 0 for v in valid: if v in mask: novw += mask[v] res.append(novw) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
def to_mask(word): ret = 0 for c in word: ret |= 1 << ord(c) - 97 return ret def get_good_masks(word): BASE = 1 << ord(word[0]) - 97 ret = [] for choice in range(64): mask = BASE for i in range(6): if 1 << i & choice: mask |= 1 << ord(word[i + 1]) - 97 ret.append(mask) return ret class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: mask_frequency = defaultdict(int) for word in words: mask_frequency[to_mask(word)] += 1 N = len(puzzles) ans = [0] * N for i, puzzle in enumerate(puzzles): for mask in get_good_masks(puzzle): ans[i] += mask_frequency[mask] return ans
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: ans = [] cnt = Counter() for w in words: w = set(w) if len(w) > 7: continue w_bit = 0 for c in w: w_bit |= 1 << ord(c) - ord("a") cnt[w_bit] += 1 for p in puzzles: bfs = [1 << ord(p[0]) - ord("a")] for c in p[1:]: bfs += [(m | 1 << ord(c) - ord("a")) for m in bfs] ans.append(sum(cnt[m] for m in bfs)) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER FOR VAR VAR ASSIGN VAR LIST BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: def helper(mask, pIdx, i): str1 = puzzles[pIdx] if i >= len(str1): return mask2 = mask mask2 |= 1 << ord(str1[i]) - ord("a") if mask2 ^ mask: d[mask2].add(pIdx) helper(mask2, pIdx, i + 1) helper(mask, pIdx, i + 1) d = collections.defaultdict(set) n = len(puzzles) for i, p in enumerate(puzzles): mask = 1 << ord(p[0]) - ord("a") d[mask].add(i) helper(mask, i, 1) res = [0] * len(puzzles) for w in words: mask = 0 for c in w: mask |= 1 << ord(c) - ord("a") for idx in d[mask]: res[idx] += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution(object): def findNumOfValidWords(self, words, puzzles): def get_bit(word): x = 0 for ch in word: x |= 1 << ord(ch) - ord("a") return x bitdic = collections.defaultdict(int) words = [set(list(x)) for x in words] for word in words: bitdic[get_bit(word)] += 1 res = [] for puzzle in puzzles: count = 0 first = puzzle[0] bfs = [1 << ord(first) - ord("a")] for ch in set(puzzle[1:]): bfs += [(x | 1 << ord(ch) - ord("a")) for x in bfs] res.append(sum(bitdic[x] for x in set(bfs))) return res
CLASS_DEF VAR FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: res = [] counter = collections.defaultdict(int) for word in words: mask = 0 for c in word: mask |= 1 << ord(c) - ord("a") counter[mask] += 1 for puzzle in puzzles: mask = 0 for c in puzzle: mask |= 1 << ord(c) - ord("a") first = 1 << ord(puzzle[0]) - ord("a") c = 0 sub = mask while True: if sub & first == first and sub in counter: c += counter[sub] if sub == 0: break sub = sub - 1 & mask res.append(c) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: res = [] cnt = collections.Counter("".join(sorted(set(w))) for w in words) for p in puzzles: bfs = [p[0]] for c in p[1:]: bfs += [(s + c) for s in bfs] res.append(sum(cnt["".join(sorted(s))] for s in bfs)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: def wordToInt(word: str) -> int: n = 0 ls = 0 for c in word: cn = 1 << ord(c) - 97 ls += 1 if cn & n == 0 else 0 if ls > 7: return None n |= cn return n def validWords(ws, puzzle: str) -> int: return sum(ws[n] if n in ws else 0 for n in getPossibleNums(puzzle, 0)) def getPossibleNums(puzzle: str, i: int) -> List[int]: cn = 1 << ord(puzzle[i]) - 97 if i == len(puzzle) - 1: return [cn, 0] elif i == 0: return [(n | cn) for n in getPossibleNums(puzzle, 1)] else: tmp = getPossibleNums(puzzle, i + 1) return [(n | cn) for n in tmp] + tmp ws = {} for w in words: n = wordToInt(w) if n is not None: ws[n] = 1 + (ws[n] if n in ws else 0) return list(map(lambda p: validWords(ws, p), puzzles))
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NONE VAR VAR RETURN VAR VAR FUNC_DEF VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN LIST VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words, puzzles): count = collections.Counter() for w in words: if len(set(w)) > 7: continue m = 0 for c in w: m |= 1 << ord(c) - 97 count[m] += 1 res = [] for p in puzzles: bfs = [1 << ord(p[0]) - 97] for c in p[1:]: bfs += [(m | 1 << ord(c) - 97) for m in bfs] res.append(sum(count[m] for m in bfs)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words, puzzles): count = collections.Counter(frozenset(w) for w in words) res = [] for p in puzzles: cur = 0 for k in range(7): for c in itertools.combinations(p[1:], k): cur += count[frozenset(tuple(p[0]) + c)] res.append(cur) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Node: def __init__(self): self.ch = {} self.cnt = 0 def build(self, words): for w in words: n = self for c in w: if c not in n.ch: n.ch[c] = Node() n = n.ch[c] n.cnt += 1 def search(self, w, first): ans = 0 if first: ans += self.cnt for c in w: if c not in self.ch: continue if c == w[0]: ans += self.ch[c].search(w, True) else: ans += self.ch[c].search(w, first) return ans class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: root = Node() root.build([sorted(set(x)) for x in words]) ans = [] for p in puzzles: ans.append(root.search(p, False)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR FOR VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: cnt = Counter(frozenset(w) for w in words if len(set(w)) <= 7) ans = [] for p in puzzles: cur = 0 for k in range(7): for i in itertools.combinations(p[1:], k): cur += cnt[frozenset((p[0],) + i)] ans.append(cur) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Puzzle: def __init__(self, puzzle): self.first_letter = puzzle[0] self.puzzle = set(puzzle) class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: cnt = collections.Counter(frozenset(w) for w in words) res = [0] * len(puzzles) for i, puzzle in enumerate(puzzles): subsets = [puzzle[0]] for c in puzzle[1:]: subsets += [(sub + c) for sub in subsets] res[i] += sum(cnt[frozenset(sub)] for sub in subsets) return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: def getNum(s: str) -> int: res = 0 for c in s: res |= 1 << ord(c) - 97 return res counter = Counter([getNum(set(word)) for word in words if len(set(word)) < 8]) ans = [] dic = dict() for puzzle in puzzles: sub = [1 << ord(puzzle[0]) - 97] for c in puzzle[1:]: sub += [(m | 1 << ord(c) - 97) for m in sub] ans.append(sum(counter[w] for w in sub)) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
def gist(word): ans = 0 for ch in word: ans |= 1 << ord(ch) - ord("a") return ans def subsets(arr, start=0, progress=0): if start == len(arr): yield progress else: yield from subsets(arr, start + 1, progress | arr[start]) yield from subsets(arr, start + 1, progress) def head_subsets(puzzle): bits = [gist(ch) for ch in puzzle] for mask in subsets(bits[1:]): yield mask | bits[0] def count_solutions(words, puzzles): gist_count = {} for word in words: g = gist(word) gist_count.setdefault(g, 0) gist_count[g] += 1 ans = [] for puzzle in puzzles: count = sum(gist_count.get(g, 0) for g in head_subsets(puzzle)) ans.append(count) return ans class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: return count_solutions(words, puzzles)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN VAR FUNC_DEF NUMBER NUMBER IF VAR FUNC_CALL VAR VAR EXPR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER EXPR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR