Best Python code snippet using pytest-benchmark
degree_freedom_queen.py
Source:degree_freedom_queen.py
1import numpy as np2def degree_freedom_queen(p_k1, p_k2, p_q1, s):3 """4 This function returns a matrix of ones and zeros where 1 specify a location in which the Queen can move to. The king5 will never choose an unsafe location.6 :param p_k1: position of King 17 :param p_k2: position of King 28 :param p_q1: position of Queen9 :param s: board10 :return: dfQ1: Degrees of Freedom of the Queen, a_q1: Allowed actions for the Queen, dfQ1_: Squares the Queen is threatening11 """12 size_board = s.shape[0]13 dfQ1 = np.zeros([size_board, size_board], dtype=int)14 dfQ1[p_q1[0], p_q1[1]] = 115 # King 2 reach16 k2r = [[p_k2[0] - 1, p_k2[1]], # up17 [p_k2[0] + 1, p_k2[1]], # down18 [p_k2[0], p_k2[1] - 1], # left19 [p_k2[0], p_k2[1] + 1], # right20 [p_k2[0] - 1, p_k2[1] - 1], # up-left21 [p_k2[0] - 1, p_k2[1] + 1], # up-right22 [p_k2[0] + 1, p_k2[1] - 1], # down-left23 [p_k2[0] + 1, p_k2[1] + 1]] # down-right24 k2r = np.array(k2r)25 26 #|4|0|5|27 #|2| |3|28 #|6|1|7|29 30 # King 1 reach31 k1r = [[p_k1[0] - 1, p_k1[1]], # up32 [p_k1[0] + 1, p_k1[1]], # down33 [p_k1[0], p_k1[1] - 1], # left34 [p_k1[0], p_k1[1] + 1], # right35 [p_k1[0] - 1, p_k1[1] - 1], # up-left36 [p_k1[0] - 1, p_k1[1] + 1], # up-right37 [p_k1[0] + 1, p_k1[1] - 1], # down-left38 [p_k1[0] + 1, p_k1[1] + 1]] # down-right39 k1r = np.array(k1r)40 dfQ1_ = np.zeros([size_board, size_board], dtype=int) # King without King 2 reach41 dfQ1_[p_q1[0], p_q1[1]] = 142 # Queen43 blocked = np.zeros(8, dtype=int)44 blocked2 = np.zeros(8, dtype=int)45 a_q1 = np.zeros([8 * (size_board - 1), 1], dtype=int)46 for j in range(size_board):47 # allow_down48 if p_q1[0] + j < size_board - 1:49 if (p_q1[0] + j + 1 == p_k1[0] and p_q1[1] == p_k1[1]) or ((p_q1[0] + j + 1 == p_k2[0] and p_q1[1] == p_k2[1])):50 blocked[0] = 151 if p_q1[0] + j + 1 == p_k1[0] and p_q1[1] == p_k1[1]:52 blocked2[0] = 153 if blocked[0] == 0:54 tmp = np.zeros([8], dtype=int)55 for i in range(k2r.shape[0]):56 if p_q1[0] + j + 1 != k2r[i, 0] or p_q1[1] != k2r[i, 1]:57 tmp[i] = 158 if np.all(tmp):59 dfQ1[p_q1[0] + j + 1, p_q1[1]] = 160 a_q1[j] = 161 else:62 for i in range(len(tmp)):63 if tmp[i] == 0:64 for ii in range(k1r.shape[0]):65 if p_q1[0] + j + 1 == k1r[ii, 0] and p_q1[1] == k1r[ii, 1]:66 dfQ1[p_q1[0] + j + 1, p_q1[1]] = 167 a_q1[j] = 168 if blocked2[0] == 0:69 dfQ1_[p_q1[0] + j + 1, p_q1[1]] = 170 # allow_up71 if p_q1[0] - j > 0:72 if (p_q1[0] - j - 1 == p_k1[0] and p_q1[1] == p_k1[1]) or ((p_q1[0] - j - 1 == p_k2[0] and p_q1[1] == p_k2[1])):73 blocked[1] = 174 if p_q1[0] - j - 1 == p_k1[0] and p_q1[1] == p_k1[1]:75 blocked2[1] = 176 if blocked[1] == 0:77 tmp = np.zeros([8], dtype=int)78 for i in range(k2r.shape[0]):79 if p_q1[0] - j - 1 != k2r[i, 0] or p_q1[1] != k2r[i, 1]:80 tmp[i] = 181 if np.all(tmp):82 dfQ1[p_q1[0] - j - 1, p_q1[1]] = 183 a_q1[j + (size_board - 1)] = 184 else:85 for i in range(len(tmp)):86 if tmp[i] == 0:87 for ii in range(k1r.shape[0]):88 if p_q1[0] - j - 1 == k1r[ii, 0] and p_q1[1] == k1r[ii, 1]:89 dfQ1[p_q1[0] - j - 1, p_q1[1]] = 190 a_q1[j + (size_board - 1)] = 191 if blocked2[1] == 0:92 dfQ1_[p_q1[0] - j - 1, p_q1[1]] = 193 # allow_right94 if p_q1[1] + j < size_board - 1:95 if (p_q1[0] == p_k1[0] and p_q1[1] + j + 1 == p_k1[1]) or ((p_q1[0] == p_k2[0] and p_q1[1] + j + 1 == p_k2[1])):96 blocked[2] = 197 if p_q1[0] == p_k1[0] and p_q1[1] + j + 1 == p_k1[1]:98 blocked2[2] = 199 if blocked[2] == 0:100 tmp = np.zeros([8], dtype=int)101 for i in range(k2r.shape[0]):102 if p_q1[0] != k2r[i, 0] or p_q1[1] + j + 1 != k2r[i, 1]:103 tmp[i] = 1104 if np.all(tmp):105 dfQ1[p_q1[0], p_q1[1] + j + 1] = 1106 a_q1[j + 2 * (size_board - 1)] = 1107 else:108 for i in range(len(tmp)):109 if tmp[i] == 0:110 for ii in range(k1r.shape[0]):111 if p_q1[0] == k1r[ii, 0] and p_q1[1] + j + 1 == k1r[ii, 1]:112 dfQ1[p_q1[0], p_q1[1] + j + 1] = 1113 a_q1[j + 2 * (size_board - 1)] = 1114 if blocked2[2] == 0:115 dfQ1_[p_q1[0], p_q1[1] + j + 1] = 1116 # allow_left117 if p_q1[1] - j > 0:118 if (p_q1[0] == p_k1[0] and p_q1[1] - j - 1 == p_k1[1]) or ((p_q1[0] == p_k2[0] and p_q1[1] - j - 1 == p_k2[1])):119 blocked[3] = 1120 if p_q1[0] == p_k1[0] and p_q1[1] - j - 1 == p_k1[1]:121 blocked2[3] = 1122 if blocked[3] == 0:123 tmp = np.zeros([8], dtype=int)124 for i in range(k2r.shape[0]):125 if p_q1[0] != k2r[i, 0] or p_q1[1] - j - 1 != k2r[i, 1]:126 tmp[i] = 1127 if np.all(tmp):128 dfQ1[p_q1[0], p_q1[1] - j - 1] = 1129 a_q1[j + 3 * (size_board - 1)] = 1130 else:131 for i in range(len(tmp)):132 if tmp[i] == 0:133 for ii in range(k1r.shape[0]):134 if p_q1[0] == k1r[ii, 0] and p_q1[1] - j - 1 == k1r[ii, 1]:135 dfQ1[p_q1[0], p_q1[1] - j - 1] = 1136 a_q1[j + 3 * (size_board - 1)] = 1137 if blocked2[3] == 0:138 dfQ1_[p_q1[0], p_q1[1] - j - 1] = 1139 # allow_down_right140 if p_q1[0] + j < size_board - 1 and p_q1[1] + j < size_board - 1:141 if (p_q1[0] + j + 1 == p_k1[0] and p_q1[1] + j + 1 == p_k1[1]) or ((p_q1[0] + j + 1 == p_k2[0] and p_q1[1] + j + 1 == p_k2[1])):142 blocked[4] = 1143 if p_q1[0] + j + 1 == p_k1[0] and p_q1[1] + j + 1 == p_k1[1]:144 blocked2[4] = 1145 if blocked[4] == 0:146 tmp = np.zeros([8], dtype=int)147 for i in range(k2r.shape[0]):148 if p_q1[0] + j + 1 != k2r[i, 0] or p_q1[1] + j + 1 != k2r[i, 1]:149 tmp[i] = 1150 if np.all(tmp):151 dfQ1[p_q1[0] + j + 1, p_q1[1] + j + 1] = 1152 a_q1[j + 4 * (size_board - 1)] = 1153 else:154 for i in range(len(tmp)):155 if tmp[i] == 0:156 for ii in range(k1r.shape[0]):157 if p_q1[0] + j + 1 == k1r[ii, 0] and p_q1[1] + j + 1 == k1r[ii, 1]:158 dfQ1[p_q1[0] + j + 1, p_q1[1] + j + 1] = 1159 a_q1[j + 4 * (size_board - 1)] = 1160 if blocked2[4] == 0:161 dfQ1_[p_q1[0] + j + 1, p_q1[1] + j + 1] = 1162 # allow_down_left163 if p_q1[0] + j < size_board - 1 and p_q1[1] - j > 0:164 if (p_q1[0] + j + 1 == p_k1[0] and p_q1[1] - j - 1 == p_k1[1]) or ((p_q1[0] + j + 1 == p_k2[0] and p_q1[1] - j - 1 == p_k2[1])):165 blocked[5] = 1166 if p_q1[0] + j + 1 == p_k1[0] and p_q1[1] - j - 1 == p_k1[1]:167 blocked2[5] = 1168 if blocked[5] == 0:169 tmp = np.zeros([8], dtype=int)170 for i in range(k2r.shape[0]):171 if p_q1[0] + j + 1 != k2r[i, 0] or p_q1[1] - j - 1 != k2r[i, 1]:172 tmp[i] = 1173 if np.all(tmp):174 dfQ1[p_q1[0] + j + 1, p_q1[1] - j - 1] = 1175 a_q1[j + 5 * (size_board - 1)] = 1176 else:177 for i in range(len(tmp)):178 if tmp[i] == 0:179 for ii in range(k1r.shape[0]):180 if p_q1[0] + j + 1 == k1r[ii, 0] and p_q1[1] - j - 1 == k1r[ii, 1]:181 dfQ1[p_q1[0] + j + 1, p_q1[1] - j - 1] = 1182 a_q1[j + 5 * (size_board - 1)] = 1183 if blocked2[5] == 0:184 dfQ1_[p_q1[0] + j + 1, p_q1[1] - j - 1] = 1185 # allow_up_right186 if p_q1[0] - j > 0 and p_q1[1] + j < size_board - 1:187 if (p_q1[0] - j - 1 == p_k1[0] and p_q1[1] + j + 1 == p_k1[1]) or ((p_q1[0] - j - 1 == p_k2[0] and p_q1[1] + j + 1 == p_k2[1])):188 blocked[6] = 1189 if p_q1[0] - j - 1 == p_k1[0] and p_q1[1] + j + 1 == p_k1[1]:190 blocked2[6] = 1191 if blocked[6] == 0:192 tmp = np.zeros([8], dtype=int)193 for i in range(k2r.shape[0]):194 if p_q1[0] - j - 1 != k2r[i, 0] or p_q1[1] + j + 1 != k2r[i, 1]:195 tmp[i] = 1196 if np.all(tmp):197 dfQ1[p_q1[0] - j - 1, p_q1[1] + j + 1] = 1198 a_q1[j + 6 * (size_board - 1)] = 1199 else:200 for i in range(len(tmp)):201 if tmp[i] == 0:202 for ii in range(k1r.shape[0]):203 if p_q1[0] - j - 1 == k1r[ii, 0] and p_q1[1] + j + 1 == k1r[ii, 1]:204 dfQ1[p_q1[0] - j - 1, p_q1[1] + j + 1] = 1205 a_q1[j + 6 * (size_board - 1)] = 1206 if blocked2[6] == 0:207 dfQ1_[p_q1[0] - j - 1, p_q1[1] + j + 1] = 1208 # allow_up_left209 if p_q1[0] - j > 0 and p_q1[1] - j > 0:210 if (p_q1[0] - j - 1 == p_k1[0] and p_q1[1] - j - 1 == p_k1[1]) or ((p_q1[0] - j - 1 == p_k2[0] and p_q1[1] - j - 1 == p_k2[1])):211 blocked[7] = 1212 if p_q1[0] - j - 1 == p_k1[0] and p_q1[1] - j - 1 == p_k1[1]:213 blocked2[7] = 1214 if blocked[7] == 0:215 tmp = np.zeros([8], dtype=int)216 for i in range(k2r.shape[0]):217 if p_q1[0] - j - 1 != k2r[i, 0] or p_q1[1] - j - 1 != k2r[i, 1]:218 tmp[i] = 1219 if np.all(tmp):220 dfQ1[p_q1[0] - j - 1, p_q1[1] - j - 1] = 1221 a_q1[j + 7 * (size_board - 1)] = 1222 else:223 for i in range(len(tmp)):224 if tmp[i] == 0:225 for ii in range(k1r.shape[0]):226 if p_q1[0] - j - 1 == k1r[ii, 0] and p_q1[1] - j - 1 == k1r[ii, 1]:227 dfQ1[p_q1[0] - j - 1, p_q1[1] - j - 1] = 1228 a_q1[j + 7 * (size_board - 1)] = 1229 if blocked2[7] == 0:230 dfQ1_[p_q1[0] - j - 1, p_q1[1] - j - 1] = 1231 dfQ1[p_q1[0], p_q1[1]] = 0232 dfQ1[p_k1[0], p_k1[1]] = 0233 if p_k2[0] != np.inf:234 dfQ1[p_k2[0], p_k2[1]] = 0...
countWaysPQR.py
Source:countWaysPQR.py
1def countWaysPQR(p, q, r):2 s = p + q + r3 dp = [[[[0 for x in range(3)] for i in range(q + 1)] for j in range(p + 1)] for k in range(s + 1)]4 dp[1][0][0][2] = 15 dp[1][1][0][0] = 16 dp[1][0][1][1] = 17 for i in range(2, s + 1):8 for p1 in range(min(i + 1, p + 1)):9 for q1 in range(min(i + 1, q + 1)):10 r1 = i - (p1 + q1)11 if r1 < 0:12 break13 if r1 == i:14 continue15 if r1 == 0:16 if q1 > 0 and p1 > 0:17 if p1 == q1:18 dp[i][p1][q1][0] = 119 dp[i][p1][q1][1] = 120 elif p1 - q1 == 1:21 dp[i][p1][q1][0] = 122 elif q1 - p1 == 1:23 dp[i][p1][q1][1] = 124 else:25 if p1 == 0 or q1 == 0:26 if p1 == r1:27 dp[i][p1][q1][0] = 128 dp[i][p1][q1][2] = 129 elif q1 == r1:30 dp[i][p1][q1][1] = 131 dp[i][p1][q1][2] = 132 elif q1 - r1 == 1:33 dp[i][p1][q1][1] = 134 elif p1 - r1 == 1:35 dp[i][p1][q1][0] = 136 elif r1 - p1 == 1 or r1 - q1 == 1:37 dp[i][p1][q1][2] = 138 else:39 dp[i][p1][q1][2] = dp[i - 1][p1][q1][0] + dp[i - 1][p1][q1][1]40 dp[i][p1][q1][1] = dp[i - 1][p1][q1 - 1][0] + dp[i - 1][p1][q1 - 1][2]41 dp[i][p1][q1][0] = dp[i - 1][p1 - 1][q1][1] + dp[i - 1][p1 - 1][q1][2]42 return sum(dp[s][p][q])...
1006. Clumsy Factorial.py
Source:1006. Clumsy Factorial.py
1class Solution:2 def clumsy(self, N: int) -> int:3 opt = 04 q1 = []5 for i in range(N, 0, -1):6 q1.append(i)7 if i == 1:8 break9 if opt == 0:10 q1.append("*")11 elif opt == 1:12 q1.append("/")13 elif opt == 2:14 q1.append("+")15 else:16 q1.append("-")17 opt += 118 if opt == 4:19 opt = 020 for i in range(0, len(q1)):21 if q1[i] == "*":22 q1[i + 1] = q1[i - 1] * q1[i + 1]23 q1[i - 1] = "null"24 q1[i] = "null"25 elif q1[i] == "/":26 q1[i + 1] = int(q1[i - 1] / q1[i + 1])27 q1[i - 1] = "null"28 q1[i] = "null"29 q1 = [x for x in q1 if x != "null"]30 for i in range(0, len(q1)):31 if q1[i] == "+":32 q1[i + 1] = q1[i - 1] + q1[i + 1]33 elif q1[i] == "-":34 q1[i + 1] = q1[i - 1] - q1[i + 1]35 return q1[-1]36if __name__ == '__main__':37 s = Solution()38 print(s.clumsy(10))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!