Advertisement
Nenogzar

02. Fishing Competition - 3

Jun 18th, 2024
458
1
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 1 0
  1. def directions(position, direction, rows, matrix):
  2.     row, col = position
  3.     matrix[row][col] = "-"
  4.  
  5.     if direction == "up":
  6.         row = (row - 1) % rows
  7.     elif direction == "down":
  8.         row = (row + 1) % rows
  9.     elif direction == "left":
  10.         col = (col - 1) % rows
  11.     elif direction == "right":
  12.         col = (col + 1) % rows
  13.  
  14.     return row, col, matrix
  15.  
  16.  
  17. def print_matrix(matrix):
  18.     for row in matrix:
  19.         print("".join(row))
  20.  
  21.  
  22. rows = int(input())
  23. fishing_area = []
  24. captain, whirlpool, empty, passage, quota = "S", "W", "-", 0, 20
  25. position = None
  26.  
  27. for row_ in range(rows):
  28.     line = list(input())
  29.     if captain in line:
  30.         position = (row_, line.index(captain))
  31.     fishing_area.append(line)
  32.  
  33. direction = input()
  34.  
  35. while direction != "collect the nets":
  36.     n_row, n_col, fishing_area = directions(position, direction, rows, fishing_area)
  37.     new_position = n_row, n_col
  38.  
  39.     step = fishing_area[n_row][n_col]
  40.  
  41.     if step.isdigit():
  42.         passage += int(step)
  43.  
  44.     if step == whirlpool:
  45.         passage = 0
  46.         print(f"You fell into a whirlpool! The ship sank and you lost the fish you caught. "
  47.               f"Last coordinates of the ship: [{n_row},{n_col}]")
  48.         exit()
  49.  
  50.     fishing_area[n_row][n_col] = captain
  51.     position = new_position
  52.     direction = input()
  53.  
  54. if passage >= quota:
  55.     print(f"Success! You managed to reach the quota!")
  56. else:
  57.     print(f"You didn't catch enough fish and didn't reach the quota! You need {quota - passage} tons of fish more.")
  58. if passage > 0:
  59.     print(f"Amount of fish caught: {passage} tons.")
  60.  
  61. print_matrix(fishing_area)
  62.  
Advertisement
Comments
  • FurTactics
    188 days
    # Python 0.19 KB | 1 0
    1. # Nice code, but in directions better make so:
    2.     elif direction == "left":
    3.         col = (col - 1) % len(matrix[row])
    4.     elif direction == "right":
    5.         col = (col + 1) % len(matrix[row])
    • Nenogzar
      187 days
      # Python 1.73 KB | 0 0
      1. # Thanks for your suggestion.
      2. # I improved the code to avoid the if loop.
      3. # to reduce from O(log/n) to O(n2)
      4.  
      5.  
      6. def directions(position, direction, matrix):
      7.     row, col = position
      8.     matrix[row][col] = "-"
      9.  
      10.     moves = {'up': (-1, 0),
      11.              'down': (1, 0),
      12.              'left': (0, -1),
      13.              'right': (0, 1)}
      14.  
      15.     d_row, d_col = moves[direction]
      16.     row = (row + d_row) % len(matrix[row])
      17.     col = (col + d_col) % len(matrix[row])
      18.     return row, col, matrix
      19.  
      20.  
      21. def print_matrix(matrix):
      22.     for row in matrix:
      23.         print("".join(row))
      24.  
      25.  
      26. rows = int(input())
      27. fishing_area = []
      28. captain, whirlpool, empty, passage, quota = "S", "W", "-", 0, 20
      29. position = None
      30.  
      31. for row_ in range(rows):
      32.     line = list(input())
      33.     if captain in line:
      34.         position = (row_, line.index(captain))
      35.     fishing_area.append(line)
      36.  
      37. direction = input()
      38.  
      39. while direction != "collect the nets":
      40.     n_row, n_col, fishing_area = directions(position, direction, fishing_area)
      41.     new_position = n_row, n_col
      42.  
      43.     step = fishing_area[n_row][n_col]
      44.  
      45.     if step.isdigit():
      46.         passage += int(step)
      47.  
      48.     if step == whirlpool:
      49.         passage = 0
      50.         print(f"You fell into a whirlpool! The ship sank and you lost the fish you caught. "
      51.               f"Last coordinates of the ship: [{n_row},{n_col}]")
      52.         exit()
      53.  
      54.     fishing_area[n_row][n_col] = captain
      55.     position = new_position
      56.     direction = input()
      57.  
      58. if passage >= quota:
      59.     print(f"Success! You managed to reach the quota!")
      60. else:
      61.     print(f"You didn't catch enough fish and didn't reach the quota! You need {quota - passage} tons of fish more.")
      62. if passage > 0:
      63.     print(f"Amount of fish caught: {passage} tons.")
      64.  
      65. print_matrix(fishing_area)
      66.  
      67.  
      68.  
      69.  
  • Nenogzar
    187 days
    # text 4.29 KB | 0 0
    1. # ******* TASK CONDITION ******* #
    2.  
    3. You are a longtime captain of an old fishing vessel.
    4. The new fishing season begins and you prepare your ship to set sail in search of the big catch…
    5.  
    6. You will be given an integer n for the size of the fishing area with a square shape.
    7. On the next n lines, you will receive the rows of the fishing area.
    8. You will be placed in a random position, marked with the letter 'S'.
    9. There will be fishing passages on random positions, marked with a single digit.
    10.  
    11. There will be whirlpools marked with 'W'.
    12. All of the empty positions will be marked with '-'.
    13.  
    14. Each turn until the "collect the nets" command is received you will be given commands for your movement.
    15. Move commands will be: "up", "down", "left", and "right".
    16.  
    17. • If you move to a fish passage,
    18. you collect the amount equal to the digit there, the passage disappears and should be replaced by '-'.
    19. • If you fall into a whirlpool – the ship sinks and loses its catch, the program ends.
    20. • If you leave the fishing area (go out of the boundaries of the matrix)
    21. depending on the move command you will be moved to the opposite side of the one you were on.
    22. /Example: In a 3x3 matrix you are at position [1,2] and receive the command "right"
    23. you will be moved to position [1,0]./
    24.  
    25. You need at least 20 tons of fish to be considered a successful season.
    26. Keep in mind that even if the quota is reached the ship continues to move.
    27.  
    28.  
    29. Input
    30. • On the first line, you are given the integer n – the size of the square matrix.
    31. • The next n lines hold the values for every row.
    32. • On each of the next lines, you will get a move command.
    33.  
    34. Output
    35. • On the first line:
    36.  If the ship falls into a whirlpool, print only this message and stop the program:
    37. o "You fell into a whirlpool! The ship sank and you lost the fish you caught. Last coordinates of the ship: [n,n]"
    38.  
    39.  If the ship reaches the quota:
    40. o "Success! You managed to reach the quota!"
    41.  
    42.  If the ship did not reach the quota:
    43. o "You didn't catch enough fish and didn't reach the quota! You need {lack of fish} tons of fish more."
    44.  
    45. • On the next lines.
    46.  If the catch quantity is bigger than zero, print:
    47. o "Amount of fish caught: {quantity} tons."
    48.  
    49. else: do not print anything.
    50.  
    51.  If you didn't get into a whirlpool, print the matrix.
    52. Constraints
    53. • The size of the square matrix will be between [2…10].
    54. • Only the letters 'S' and 'W' will be present in the matrix.
    55. • The fish passages are represented by single positive digits /tons/ between [1…9].
    56. • It is expected that there will only be either zero or one whirpool present, marked with the letter - 'W'.
    57. • Your position will be marked with 'S'.
    58. Examples
    59.  
    60. Input
    61. 4
    62. ---S
    63. ----
    64. 9-5-
    65. 34--
    66. down
    67. down
    68. right
    69. down
    70. collect the nets
    71.  
    72.  
    73. Output
    74. You didn't catch enough fish and didn't reach the quota! You need 8 tons of fish more.
    75. Amount of fish caught: 12 tons.
    76. ----
    77. ----
    78. --5-
    79. S4--
    80.  
    81.  
    82. Comment
    83. The first command is "down".
    84. The ship moves to position [1,3] followed by the command "down" [2,3] and then the command "right".
    85. The ship leaves the matrix's boundaries and transfers to the opposite side at position [2,0].
    86. The ship comes across a fish passage with a quantity of 9 tons and gets it.
    87. After executing the third command, the fishing area will appear as follows:
    88. ----
    89. ----
    90. S-5-
    91. 34--
    92. Then you receive the command "down" again. You move to the passage of 3 tons and add them to the others 9.
    93. Your catch is 9 + 3 = 12 tons. In the end, you get the command "collect the nets" and the program ends.
    94.  
    95.  
    96.  
    97. Input
    98. 5
    99. S---9
    100. 777-1
    101. W333-
    102. 11111
    103. -----
    104. down
    105. down
    106. right
    107. down
    108. collect the nets
    109.  
    110.  
    111. Output
    112. You fell into a whirlpool! The ship sank and you lost the fish you caught. Last coordinates of the ship: [2,0]
    113.  
    114.  
    115. Comment
    116. The first command is "down". The ship moves to position [1,0] and gets 7 tons of fish.
    117. Follow the command "down" -> [2,0] The ship falls into a whirlpool and sinks.
    118. You lose the entire catch and the program ends.
    119.  
    120.  
    121. Input
    122. 5
    123. S---9
    124. 777-1
    125. --5--
    126. 11W11
    127. 988--
    128. down
    129. down
    130. down
    131. down
    132. down
    133. down
    134. right
    135. right
    136. right
    137. collect the nets
    138.  
    139.  
    140. Output
    141. Success! You managed to reach the quota!
    142. Amount of fish caught: 31 tons.
    143. ----9
    144. ---S1
    145. --5--
    146. -1W11
    147. -88--
    148.  
    149.  
    150. Comment
    151. Result is: 7 + 1 + 9 +7 + 7 = 31. You succeeded!
Add Comment
Please, Sign In to add comment
Advertisement