Advertisement
Imper70r

Drunken_Sailor_Extra

Sep 18th, 2021
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. from random import randint
  2.  
  3.  
  4. def sailor_walk(border, x, y):
  5.     rnd = randint(1, 4)
  6.     if rnd == 1:    # Up
  7.         y += 1
  8.     elif rnd == 2:  # Down
  9.         y -= 1
  10.     elif rnd == 3:  # Left
  11.         x -= 1
  12.     else:           # Right
  13.         x += 1
  14.  
  15.     return x, y
  16.  
  17.  
  18. def new_sailor(x, y):
  19.     x = 0
  20.     y = 0
  21.     return x, y
  22.  
  23.  
  24. def check_sailor_fell(border, x, y):
  25.     if abs(x) > border or abs(y) > border:
  26.         return True
  27.     return False
  28.  
  29.  
  30. def print_result(i, num_sailors, sailors_fell):
  31.     if sailors_fell > 0:
  32.         print(f"({i+1}) Out of {num_sailors} drunk sailors, "
  33.               f"{sailors_fell} ({((sailors_fell/num_sailors)*100):.2f}%) "
  34.               f"fell into the water")
  35.     else:
  36.         print(f"Out of {num_sailors} drunken sailors, no sailors fell!")
  37.  
  38.  
  39. def run(sailors_fell, x, y, rep, grid_size, num_steps, num_sailors):
  40.     for i in range(num_sailors):
  41.         x, y = new_sailor(x, y)
  42.         for j in range(num_steps):
  43.             x, y = sailor_walk(grid_size, x, y)
  44.             if check_sailor_fell(grid_size, x, y):
  45.                 sailors_fell += 1
  46.                 break
  47.     print_result(rep, num_sailors, sailors_fell)
  48.     return sailors_fell
  49.  
  50.  
  51. sailors_fell = 0
  52. x = 0
  53. y = 0
  54.  
  55. grid_size = int(input("Enter the size: "))
  56. num_steps = int(input("Enter the number of steps: "))
  57. num_sailors = int(input("Enter the number of sailors: "))
  58.  
  59. min_sailors_fell = 100
  60. max_sailors_fell = 0
  61.  
  62. # Find how many iterations it will take for 1/3 of the sailors to fall
  63. i = 0
  64. curr_sailors_fell = 0
  65. while(curr_sailors_fell < 30):
  66.     curr_sailors_fell = run(sailors_fell, x, y, i,
  67.                             grid_size, num_steps, num_sailors)
  68.     i += 1
  69.  
  70. # # Find the min and max sailors fell in n iterations
  71. # for i in range(10000):
  72. #     curr_sailors_fell = run(sailors_fell, x, y, i,
  73. #                             grid_size, num_steps, num_sailors)
  74.  
  75. #     if curr_sailors_fell < min_sailors_fell:
  76. #         min_sailors_fell = curr_sailors_fell
  77. #     elif curr_sailors_fell > max_sailors_fell:
  78. #         max_sailors_fell = curr_sailors_fell
  79.  
  80. # print(f"Min sailors fell: {min_sailors_fell} "
  81. #       f"({(min_sailors_fell/num_sailors)*100:.2f}%)")
  82. # print(f"Max sailors fell: {max_sailors_fell} "
  83. #       f"({(max_sailors_fell/num_sailors)*100:.2f}%)")
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement