Advertisement
This is comment for paste
02. Fishing Competition - 3
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Thanks for your suggestion.
- # I improved the code to avoid the if loop.
- # to reduce from O(log/n) to O(n2)
- def directions(position, direction, matrix):
- row, col = position
- matrix[row][col] = "-"
- moves = {'up': (-1, 0),
- 'down': (1, 0),
- 'left': (0, -1),
- 'right': (0, 1)}
- d_row, d_col = moves[direction]
- row = (row + d_row) % len(matrix[row])
- col = (col + d_col) % len(matrix[row])
- return row, col, matrix
- def print_matrix(matrix):
- for row in matrix:
- print("".join(row))
- rows = int(input())
- fishing_area = []
- captain, whirlpool, empty, passage, quota = "S", "W", "-", 0, 20
- position = None
- for row_ in range(rows):
- line = list(input())
- if captain in line:
- position = (row_, line.index(captain))
- fishing_area.append(line)
- direction = input()
- while direction != "collect the nets":
- n_row, n_col, fishing_area = directions(position, direction, fishing_area)
- new_position = n_row, n_col
- step = fishing_area[n_row][n_col]
- if step.isdigit():
- passage += int(step)
- if step == whirlpool:
- passage = 0
- print(f"You fell into a whirlpool! The ship sank and you lost the fish you caught. "
- f"Last coordinates of the ship: [{n_row},{n_col}]")
- exit()
- fishing_area[n_row][n_col] = captain
- position = new_position
- direction = input()
- if passage >= quota:
- print(f"Success! You managed to reach the quota!")
- else:
- print(f"You didn't catch enough fish and didn't reach the quota! You need {quota - passage} tons of fish more.")
- if passage > 0:
- print(f"Amount of fish caught: {passage} tons.")
- print_matrix(fishing_area)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement