Best Python code snippet using fMBT_python
GregChess.py
Source:GregChess.py
...29captured_pieces = {"White(capitals)": [], "Black(lowercase)": []} #Tracks captured pieces30move_list = [] #Tracks all moves made during the game.31#Takes a piece/position and returns its index values in the board array32# Ra1 -> (7,0) : White rook is in row[7], column[0]33def piece_coords(piece):34 return (row_indexes[piece[2]],column_indexes[piece[1]])35#Takes a position on the board and returns the piece at that position, " " or "X" if empty36def piece_at(position, current_board):37 position_coords = row_indexes[position[1]],column_indexes[position[0]]38 return current_board[position_coords[0]][position_coords[1]]39#Takes a lits of piece positions and redraws the board given those positions40def update_board(piece_positions):41 new_board = copy.deepcopy(base_board)42 for piece in piece_positions:43 x, y = piece_coords(piece)44 new_board[x][y] = piece45 return new_board46#Takes a board and prints a representation for the player with a-h,1-8 chess coords47def display_board(board):48 top = " | a | b | c | d | e | f | g | h |"49 divider = " |___|___|___|___|___|___|___|___|"50 print(top)51 print(divider)52 for row in enumerate(board):53 num = str(8-row[0])54 for space in row[1]:55 num+=" | "+space[0]56 num+=" |"57 print (num)58 print(divider)59#Takes a piece and returns its color: "White or Black"60def piece_color(piece):61 if piece[0] in "RKBQGP":62 return "White(capitals)"63 return "Black(lowercase)"64def opposing_color(color):65 if color == "White(capitals)":66 return "Black(lowercase)"67 return "White(capitals)"68#Takes a piece and a destination space and updates current piece positions with the new positon.69#If the desination space already contains a piece, that piece is removed and added to the captured pieces dictionary.70#Piece format = Ra1 (white rook at a1) Desination format = a5 (space a5)71def move_piece(piece, destination, player, current_board, move=True):72 if not valid_move(piece, destination, player, current_board):73 if move:74 print ("Invalid Move")75 return False76 intended_positions = copy.deepcopy(current_piece_positions)77 intended_positions.remove(piece)78 intended_positions.append(piece[0]+destination)79 for p in current_piece_positions:80 if p[1:] == destination:81 intended_positions.remove(p)82 intended_board = update_board(intended_positions)83 if not valid_capture(piece, p):84 if move:85 print("Invalid move, can't capture your own piece!")86 return False87 elif check_for_check(player, intended_positions, intended_board):88 if move:89 print("Invalid move, can't move into check!")90 return False91 elif move:92 print(p + " captured!")93 captured_pieces[piece_color(p)]+=[p]94 current_piece_positions.remove(p)95 intended_board = update_board(intended_positions)96 if check_for_check(player, intended_positions, intended_board):97 if move :98 print("Invalid move, can't move into check!")99 return False100 if not move:101 return True102 current_piece_positions.remove(piece)103 new_piece_position = piece[0]+destination104 #Handle Pawn Promotion105 if piece[0] in "Pp":106 if destination[1] in "18":107 new_piece = "XXX"108 while new_piece not in "rkbqpRKBQP":109 new_piece = input("Promote your pawn. Enter the letter of the piece type you wish to use.")110 if new_piece not in "rkbqpRKBQP":111 print("Invalid piece type. Please select again.")112 if piece[0] == "P":113 new_piece = new_piece.upper()114 else:115 new_piece = new_piece.lower()116 new_piece_position = new_piece+destination117 move_list.append((piece, destination))118 current_piece_positions.append(new_piece_position)119 return True120#Checks if a given piece can capture another piece121def valid_capture(moving_piece, threatened_piece):122 if piece_color(moving_piece) == piece_color(threatened_piece):123 return False124 return True125#Checks if given move is valid. The move piece function already ensures that a piece cannot capture a friendly piece.126def valid_move(piece, destination, player, current_board):127 coords = piece_coords(piece)128 if destination[1] not in row_indexes or destination[0] not in column_indexes:129 return False130 destination_coords = row_indexes[destination[1]],column_indexes[destination[0]]131 if coords == destination_coords:132 return False133 if piece not in current_board[coords[0]]:134 return False135 #Check Rook136 if piece[0]in "Rr":137 return check_rook(piece, destination,coords,destination_coords,current_board)138 #Check Knight139 if piece[0]in "Kk":140 move_possibilities = []141 for pos in [(-2,-1),(-2,1),(-1,-2),(-1,2)]:142 move_possibilities.append((coords[0]+pos[0],coords[1]+pos[1]))143 move_possibilities.append((coords[0]-pos[0],coords[1]+pos[1]))144 if destination_coords not in move_possibilities:145 return False146 return True147 #Check Pawn148 if piece[0]== "P": #Checks for white pawns149 #If pawn has not moved, check if the player is trying to move it two spaces.150 if coords[0] == 6:151 if destination_coords[0] == coords[0]-2:152 if piece_at(destination,current_board) not in " X" or current_board[coords[0]-1][coords[1]] not in " X" or destination_coords[1] != coords[1]:153 return False154 return True155 #Checks pawn moves other than moving two spaces156 if destination_coords[0] != coords[0]-1: #If pawn does not move into the next row, return false157 return False158 #Pawn cannot move anywhere other than the 3 spaces in front of it.159 if abs(destination_coords[1]-coords[1]) > 1:160 return False161 if destination_coords[1] == coords[1]: #If pawn is moving straight, it cannot capture162 if piece_at(destination,current_board) not in " X":163 return False164 if abs(destination_coords[1]-coords[1]) == 1: #If pawn is moving digonally, that space must contain a piece to capture...165 if piece_at(destination,current_board) not in " X":166 return True167 #unless capturing en passent. In this case, check the last move in the movelist for a black pawn that moved 2 spaces forward and ended next to the white pawn.168 if piece[2] == "5": #In the same column the pawn is attempting to move into169 if move_list[-1][0][0] == "p" and move_list[-1][0][2] == "7" and move_list[-1][1][1] == "5" and piece_coords(move_list[-1][0])[1] == destination_coords[1]:170 captured_pieces["Black(lowercase)"]+=move_list[-1][0]171 current_piece_positions.remove(move_list[-1][0][0]+move_list[-1][1])172 return True173 return False174 return True175 if piece[0]== "p": #Checks for black pawns176 if coords[0] == 1:177 if destination_coords[0] == coords[0]+2:178 if piece_at(destination,current_board) not in " X" or current_board[coords[0]+1][coords[1]] not in " X" or destination_coords[1] != coords[1]:179 return False180 return True181 #Checks pawn moves other than moving two spaces182 if destination_coords[0] != coords[0]+1: #If pawn does not move into the next row, return false183 return False184 #Pawn cannot move aywhere other than the 3 spaces in front of it.185 if abs(destination_coords[1]-coords[1]) > 1:186 return False187 if destination_coords[1] == coords[1]: #If pawn is moving straight, it cannot capture188 if piece_at(destination,current_board) not in " X":189 return False190 if abs(destination_coords[1]-coords[1]) == 1: #If pawn is moving digonally, it must capture191 if piece_at(destination,current_board) not in " X":192 return True193 #unless capturing en passent. In this case, check the last move in the movelist for a black pawn that moved 2 spaces forward and ended next to the white pawn.194 if piece[2] == "4":195 if move_list[-1][0][0] == "P" and move_list[-1][0][2] == "2" and move_list[-1][1][1] == "4" and piece_coords(move_list[-1][0])[1] == destination_coords[1]:196 captured_pieces["White(capitals)"]+=move_list[-1][0]197 current_piece_positions.remove(move_list[-1][0][0]+move_list[-1][1])198 return True199 return False200 return True201 #Check King202 if piece[0]in "Gg":203 if abs(destination_coords[0]-coords[0]) <=1 and abs(destination_coords[1]-coords[1]) <=1:204 return True205 if abs(destination_coords[1]-coords[1]) == 2 and (destination_coords[0]-coords[0]) == 0: #Castling attempt206 for p, dest in move_list: #If the king has already moved, it can't castle207 if p[0] == piece[0]:208 return False209 if (destination_coords[1]-coords[1]) == 2: #Determine which rook to move......
solution.py
Source:solution.py
1def usable(input):2 coords = input[1]3 x = coords[0]4 y = coords[1]5 z = coords[2]6 7 for coord in [x, y, z]:8 if (-50 > int(coord[0]) or int(coord[0]) > 50) or (-50 > int(coord[1]) or int(coord[1]) > 50):9 return False10 return True11 12 13def update_cube_list(status, coords, light_up):14 if status == 'on':15 for x in range(int(coords[0][0]), int(coords[0][1])+1):16 for y in range(int(coords[1][0]), int(coords[1][1])+1):17 for z in range(int(coords[2][0]), int(coords[2][1])+1):18 light_up.add((x, y, z))19 else:20 new_light_up = set()21 for cube in light_up:22 if int(coords[0][0]) <= cube[0] <= int(coords[0][1]) and \23 int(coords[1][0]) <= cube[1] <= int(coords[1][1]) and \24 int(coords[2][0]) <= cube[2] <= int(coords[2][1]):25 # print(cube)26 continue27 else:28 new_light_up.add(cube)29 # for x in range(int(coords[0][0]), int(coords[0][1])+1):30 # for y in range(int(coords[1][0]), int(coords[1][1])+1):31 # for z in range(int(coords[2][0]), int(coords[2][1])+1):32 # if (x, y, z) in light_up:33 # light_up.remove((x, y, z))34 light_up = new_light_up35 return light_up36'''37ON SOME_AREA38OFF SOME_AREA39ON SOME_AREA40'''41def validity(off_lights, on_lights, coordinates):42 x, y, z = coordinates43 # Check for duplicates44 for check_list in [off_lights, on_lights]:45 for check in check_list:46 x_bound, y_bound, z_bound = check47 if (x_bound[0] <= x <= x_bound[1]) and \48 (y_bound[0] <= y <= y_bound[1]) and \49 (z_bound[0] <= z <= z_bound[1]):50 return 051 return 152def update(off_checks, on_checks, input):53 status, coordinate = input[0], input[1]54 lights = 055 if status == 'off':56 off_checks.append(coordinate)57 return 058 else:59 x, y, z = coordinate60 for x_pos in range(x[0], x[1]+1):61 for y_pos in range(y[0], y[1]+1):62 for z_pos in range(z[0], z[1]+1):63 lights += validity(off_checks, on_checks, [x_pos, y_pos, z_pos])64 on_checks.append(coordinate)65 return lights66def part2(inputs):67 off_checks = list()68 on_checks = list()69 lights = 070 num = len(inputs)71 counter = 172 for input in reversed(inputs):73 print('Complete %: {}'.format(counter/num))74 counter += 175 # if not usable(input):76 # continue77 lights += update(off_checks, on_checks, input)78 print(lights)79 80 81def part1(inputs):82 light_up = set()83 for input in inputs:84 # if not usable(input):85 # continue86 status = input[0]87 coords = input[1]88 light_up = update_cube_list(status, coords, light_up)89 90 return len(light_up)91 92filename = "input.txt"93lines = open(filename, 'r').read().splitlines()94inputs = []95for line in lines:96 status, coords = line.split(' ')97 coords = coords.split(',')98 x = list(map(int, coords[0].split('=')[-1].split('..')))99 y = list(map(int, coords[1].split('=')[-1].split('..')))100 z = list(map(int, coords[2].split('=')[-1].split('..')))101 inputs.append([status, [x, y, z]])102# print(inputs)103# result = part1(inputs)104# print(result)...
task2.py
Source:task2.py
1n = int(input())2horizontal = []3vertical = []4for x in range(n-1):5 input_str = input()6 horizontal.append([int(i) for i in input_str])7for y in range(n):8 input_str = input()9 vertical.append([int(i) for i in input_str])10def move(mode = 0):11 global end_moving, path12 #step right13 if coords[1] != n-1 and vertical[coords[0]][coords[1]] == mode:14 end_moving = False15 if vertical[coords[0]][coords[1]] == 0:16 vertical[coords[0]][coords[1]] = 217 else:18 vertical[coords[0]][coords[1]] = 119 coords[1] += 120 path += 'R'21 #step down22 elif coords[0] != n-1 and horizontal[coords[0]][coords[1]] == mode:23 end_moving = False24 if horizontal[coords[0]][coords[1]] == 0:25 horizontal[coords[0]][coords[1]] = 226 else:27 horizontal[coords[0]][coords[1]] = 128 coords[0] += 129 path += 'D'30 #step up31 elif coords[0] != 0 and horizontal[coords[0]-1][coords[1]] == mode:32 end_moving = False33 if horizontal[coords[0]-1][coords[1]] == 0:34 horizontal[coords[0]-1][coords[1]] = 235 else:36 horizontal[coords[0]-1][coords[1]] = 137 coords[0] -= 138 path += 'U'39 #step left40 elif coords[1] != 0 and vertical[coords[0]][coords[1]-1] == mode:41 end_moving = False42 if vertical[coords[0]][coords[1]-1] == 0:43 vertical[coords[0]][coords[1]-1] = 244 else:45 vertical[coords[0]][coords[1]-1] = 146 coords[1] -= 147 path += 'L'48coords = [0,0]49path = ''50while True:51 end_moving = True52 move(0)53 if end_moving:54 move(2)55 56 if end_moving:57 break58print(len(path))...
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!!