Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def directions(position, direction, rows, matrix):
- row, col = position
- matrix[row][col] = "-"
- if direction == "up":
- row = (row - 1) % rows
- elif direction == "down":
- row = (row + 1) % rows
- elif direction == "left":
- col = (col - 1) % rows
- elif direction == "right":
- col = (col + 1) % rows
- 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, rows, 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
Comments
-
- # Nice code, but in directions better make so:
- elif direction == "left":
- col = (col - 1) % len(matrix[row])
- elif direction == "right":
- col = (col + 1) % len(matrix[row])
-
- # 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)
-
- # ******* TASK CONDITION ******* #
- You are a longtime captain of an old fishing vessel.
- The new fishing season begins and you prepare your ship to set sail in search of the big catch…
- You will be given an integer n for the size of the fishing area with a square shape.
- On the next n lines, you will receive the rows of the fishing area.
- You will be placed in a random position, marked with the letter 'S'.
- There will be fishing passages on random positions, marked with a single digit.
- There will be whirlpools marked with 'W'.
- All of the empty positions will be marked with '-'.
- Each turn until the "collect the nets" command is received you will be given commands for your movement.
- Move commands will be: "up", "down", "left", and "right".
- • If you move to a fish passage,
- you collect the amount equal to the digit there, the passage disappears and should be replaced by '-'.
- • If you fall into a whirlpool – the ship sinks and loses its catch, the program ends.
- • If you leave the fishing area (go out of the boundaries of the matrix)
- depending on the move command you will be moved to the opposite side of the one you were on.
- /Example: In a 3x3 matrix you are at position [1,2] and receive the command "right"
- you will be moved to position [1,0]./
- You need at least 20 tons of fish to be considered a successful season.
- Keep in mind that even if the quota is reached the ship continues to move.
- Input
- • On the first line, you are given the integer n – the size of the square matrix.
- • The next n lines hold the values for every row.
- • On each of the next lines, you will get a move command.
- Output
- • On the first line:
- If the ship falls into a whirlpool, print only this message and stop the program:
- o "You fell into a whirlpool! The ship sank and you lost the fish you caught. Last coordinates of the ship: [n,n]"
- If the ship reaches the quota:
- o "Success! You managed to reach the quota!"
- If the ship did not reach the quota:
- o "You didn't catch enough fish and didn't reach the quota! You need {lack of fish} tons of fish more."
- • On the next lines.
- If the catch quantity is bigger than zero, print:
- o "Amount of fish caught: {quantity} tons."
- else: do not print anything.
- If you didn't get into a whirlpool, print the matrix.
- Constraints
- • The size of the square matrix will be between [2…10].
- • Only the letters 'S' and 'W' will be present in the matrix.
- • The fish passages are represented by single positive digits /tons/ between [1…9].
- • It is expected that there will only be either zero or one whirpool present, marked with the letter - 'W'.
- • Your position will be marked with 'S'.
- Examples
- Input
- 4
- ---S
- ----
- 9-5-
- 34--
- down
- down
- right
- down
- collect the nets
- Output
- You didn't catch enough fish and didn't reach the quota! You need 8 tons of fish more.
- Amount of fish caught: 12 tons.
- ----
- ----
- --5-
- S4--
- Comment
- The first command is "down".
- The ship moves to position [1,3] followed by the command "down" [2,3] and then the command "right".
- The ship leaves the matrix's boundaries and transfers to the opposite side at position [2,0].
- The ship comes across a fish passage with a quantity of 9 tons and gets it.
- After executing the third command, the fishing area will appear as follows:
- ----
- ----
- S-5-
- 34--
- Then you receive the command "down" again. You move to the passage of 3 tons and add them to the others 9.
- Your catch is 9 + 3 = 12 tons. In the end, you get the command "collect the nets" and the program ends.
- Input
- 5
- S---9
- 777-1
- W333-
- 11111
- -----
- down
- down
- right
- down
- collect the nets
- Output
- You fell into a whirlpool! The ship sank and you lost the fish you caught. Last coordinates of the ship: [2,0]
- Comment
- The first command is "down". The ship moves to position [1,0] and gets 7 tons of fish.
- Follow the command "down" -> [2,0] The ship falls into a whirlpool and sinks.
- You lose the entire catch and the program ends.
- Input
- 5
- S---9
- 777-1
- --5--
- 11W11
- 988--
- down
- down
- down
- down
- down
- down
- right
- right
- right
- collect the nets
- Output
- Success! You managed to reach the quota!
- Amount of fish caught: 31 tons.
- ----9
- ---S1
- --5--
- -1W11
- -88--
- Comment
- Result is: 7 + 1 + 9 +7 + 7 = 31. You succeeded!
Add Comment
Please, Sign In to add comment
Advertisement