Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def create_matrix(source):
- matrix = []
- for row in source.split('\n')[1:]: # split string into list of strings on newline, and loop through each line
- matrix.append(row[3:].split()) # split string from position 3 on space (default for split)
- return matrix
- def print_matrix(matrix):
- print(' ', end="")
- for idx in range(len(matrix[0])):
- print(f'{idx: 2x} ', end="")
- print()
- for idx, row in enumerate(matrix):
- print(f'{idx * 10:0>2} ', end="")
- for cell in row:
- print(f'{cell:>2} ', end="")
- print()
- def find_target(matrix, target):
- locations = []
- for idx_r, row in enumerate(matrix):
- for idx_c, cell in enumerate(row):
- if cell == target:
- locations.append((idx_r, idx_c))
- return locations
- def print_locations(locations):
- for row, col in locations:
- print(f'{row * 10:0>2}{col:0>2x}')
- ss = """ 0 1 2 3 4 5 6 7 8 9 a b c d e f
- 00 __ __ __ __ __ __ __ __ __ __ __ __ UU __ __ __
- 10 UU __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
- 20 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
- 30 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
- 40 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
- 50 __ __ __ __ __ __ __ __ UU __ __ __ __ __ __ __
- 60 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
- 70 __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ """
- matrix = create_matrix(ss)
- print_matrix(matrix)
- locations = find_target(matrix, "UU")
- if locations:
- print("\nTarget UU found in locations:-")
- print_locations(locations)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement