Best Python code snippet using hypothesis
day4.py
Source: day4.py
1import numpy as np2class BingoBoard:3 array = []4 size = 55 check = True6 def __init__(self, boardtext) -> None:7 self.array = np.zeros((self.size,self.size,2))8 board = boardtext.split()9 bct = 010 #print(f"board:{board}")11 for i in range(self.size):12 for j in range(self.size):13 self.array[i][j][0] = int(board[bct])14 bct += 115 def __str__(self) -> str:16 sret = ""17 for i in range(self.size):18 for j in range(self.size):19 sret += f"{int (self.array[i][j][0]) :2}|{int (self.array[i][j][1]) :1} "20 sret += '\n'21 return sret22 def is_bingo(self) -> bool:23 for i in range(self.size):24 for j in range(self.size):25 if self.array[i][j][1] == 1:26 row_match_count = 027 col_match_count = 028 for x in range(self.size):29 if self.array[x][j][1] == 1:30 row_match_count += 131 for y in range(self.size):32 if self.array[i][y][1] == 1:33 col_match_count += 134 if row_match_count == 5 or col_match_count == 5:35 self.check = False36 return True37 return False38 39 def draw_number(self, number):40 for i in range(self.size):41 for j in range(self.size):42 if self.array[i][j][0] == number:43 self.array[i][j][1] = 144 return45 def sum_unmarked(self) -> int:46 unmarked = 047 for i in range(self.size):48 for j in range(self.size):49 if self.array[i][j][1] == 0:50 unmarked += self.array[i][j][0]51 return unmarked52def main():53 # Part 1 ----------------------------------------------------------54 draw_list = []55 board_list = []56 57 # Read file input58 relative_path = 'src/tasks/advent_of_code_21/day4_input.txt'59 with open(relative_path, 'r') as f:60 draw_list = f.readline().strip()61 draw_list = draw_list.split(',')62 line = f.readline()63 line = f.readline()64 board = ''65 ct = 066 while line:67 if line != "\n":68 board += line.strip() + " "69 else:70 board_list.append(BingoBoard(board))71 board = ''72 line = f.readline()73 ct += 174 print(f"{len(draw_list)} draws:{draw_list}") 75 # Work through draw list checking for win conditions76 win_number = 077 unmarked_sum = 078 win_board = None 79 win_condition = False80 for i in range(len(draw_list)):81 #print(f"draw[{i}]:{draw_list[i]}")82 for b in board_list:83 b.draw_number(int (draw_list[i]))84 if b.is_bingo():85 win_number = int (draw_list[i])86 unmarked_sum = b.sum_unmarked()87 win_board = b 88 win_condition = True89 break90 if win_condition:91 break92 93 # Calculate score of first winning bingo board94 score = unmarked_sum * win_number95 print(f"winning board:\n{win_board}")96 print(f"unmarked_sum:{int (unmarked_sum)}")97 print(f"winning_number:{int (win_number)}")98 print(f"score:{int (score)}")99 # Part 2 ----------------------------------------------------------100 print("\nPart2:")101 number_of_boards = len(board_list)102 win_count = 0103 draw = 0104 while win_count < (number_of_boards) and draw < len(draw_list):105 #print(f"draw[{i}]:{draw_list[i]}")106 for b in board_list:107 if b.check: # If board not yet complete108 b.draw_number(int (draw_list[draw]))109 if b.is_bingo():110 win_number = int (draw_list[draw])111 unmarked_sum = b.sum_unmarked()112 win_board = b 113 win_count += 1114 draw += 1115 116 score = unmarked_sum * win_number117 print(f"winning board:\n{win_board}")118 print(f"unmarked_sum:{int (unmarked_sum)}")119 print(f"winning_number:{int (win_number)}")120 print(f"score:{int (score)}")121 122if __name__ == "__main__":...
day_04.py
Source: day_04.py
1from bingoboard import BingoBoard2BOARD_SIZE_LINES = 53def parse_line(l):4 return [int(x) for x in l.split()]5def load(filename):6 with open(filename) as f_obj:7 draw_list_str = f_obj.readline()8 boardlist = []9 current_board = []10 for line in f_obj:11 if len(line) > 2:12 current_board.append(parse_line(line))13 if len(current_board) == BOARD_SIZE_LINES:14 boardlist.append(current_board)15 current_board = []16 draw_list = eval(f"[{draw_list_str}]")17 return draw_list, boardlist18def solve(draw_list, boards):19 bingoboards = [BingoBoard(b) for b in boards]20 we_have_a_winner = False21 winsum = 022 while len(draw_list) > 0 and not we_have_a_winner:23 n = draw_list.pop(0)24 for b in bingoboards:25 b.mark(n)26 if b.hasWon:27 we_have_a_winner = True28 win_number = b.sum * n29 return win_number30def find_last_winner(draw_list, boards):31 bingoboards = [BingoBoard(b) for b in boards]32 last_winner = None33 while len(draw_list) > 0 and len(bingoboards) > 0:34 n = draw_list.pop(0)35 print(f"{n} was drawn {len(draw_list)} numbers left")36 for b in bingoboards:37 b.mark(n)38 for b in bingoboards:39 if b.hasWon:40 last_winner = b41 bingoboards.remove(b)42 print(f"This board has won, {len(bingoboards)} boards left:\n{b}")43 return last_winner.sum * n44if __name__ == "__main__":45 draw_list, boards = load("./input/04.txt")46 #print(solve(draw_list, boards))...
draw_route.py
Source: draw_route.py
1from instruments import converter as convert2def draw_route(file_name, route_name, *args):3 draw_list = []4 for arg in args:5 if type(arg) is str:6 draw_list += [arg]7 else:8 draw_list += arg9 three_points = lambda lst, sz: [lst[i:i + sz] for i in range(0, len(lst), sz)]10 result = three_points(draw_list, 3)11 to_list = [', '.join(item) for item in result]12 with open(f"ROUTES/{file_name}.sld", 'a+') as file:13 file.write(f'L: *{route_name}* ' + to_list[0] + '\n')14 for item in to_list[1:]:15 file.write(', ' + item + '\n')16def draw_route_tr(file_name, route_name, *args):17 draw_list = []18 for arg in args:19 if type(arg) is str:20 draw_list += [arg]21 else:22 lenght = len(arg)23 middle = int(lenght/2)24 draw_list += [arg[0]]25 draw_list += [arg[middle]]26 draw_list += [arg[-1]]27 with open(f"KST_ROUTES/{file_name}.txt", 'a+') as file:28 file.write(f'{route_name}\n')29 for num, item in enumerate(draw_list):30 file.write(f'{route_name}{num} ' + convert.str_to_tren(item) + '\n')...
Check out the latest blogs from LambdaTest on this topic:
Before we discuss the Joomla testing, let us understand the fundamentals of Joomla and how this content management system allows you to create and maintain web-based applications or websites without having to write and implement complex coding requirements.
In today’s world, an organization’s most valuable resource is its customers. However, acquiring new customers in an increasingly competitive marketplace can be challenging while maintaining a strong bond with existing clients. Implementing a customer relationship management (CRM) system will allow your organization to keep track of important customer information. This will enable you to market your services and products to these customers better.
How do we acquire knowledge? This is one of the seemingly basic but critical questions you and your team members must ask and consider. We are experts; therefore, we understand why we study and what we should learn. However, many of us do not give enough thought to how we learn.
Testing is a critical step in any web application development process. However, it can be an overwhelming task if you don’t have the right tools and expertise. A large percentage of websites still launch with errors that frustrate users and negatively affect the overall success of the site. When a website faces failure after launch, it costs time and money to fix.
Mobile application development is on the rise like never before, and it proportionally invites the need to perform thorough testing with the right mobile testing strategies. The strategies majorly involve the usage of various mobile automation testing tools. Mobile testing tools help businesses automate their application testing and cut down the extra cost, time, and chances of human error.
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!!