Skip to content

Instantly share code, notes, and snippets.

@OriBenHur
Last active December 12, 2020 17:56
Show Gist options
  • Save OriBenHur/d50f983463d0e2255df0005cf4e9157a to your computer and use it in GitHub Desktop.
Save OriBenHur/d50f983463d0e2255df0005cf4e9157a to your computer and use it in GitHub Desktop.
import random
def assign_random_num_to_soldier(top: int) -> list:
random_number_list = list()
for _ in range(top):
random_number_list.append(random.randint(1, top))
return random_number_list
def generate_list(soldier: int, complete_list: list) -> list:
return complete_list[:soldier] + complete_list[soldier + 1:]
def guess(self_assign_number: int, soldiers_list: list, top: int) -> int:
sum_all_num_in_list = sum(soldiers_list)
mod = sum_all_num_in_list % top
answer = ((top + self_assign_number) - mod) % top + 1
return answer
def do_work(top_range: int):
ground_truth = assign_random_num_to_soldier(top_range)
for soldier_num in range(top_range):
if guess(soldier_num , generate_list(soldier_num, ground_truth), top_range) == ground_truth[soldier_num]:
yield True
else:
yield False
def tester(top_range, number_of_runs):
count = 0
for i in range(number_of_runs):
done = False
for j in do_work(top_range):
if j:
done = True
break
if done:
count += 1
print(f'{count} out of {number_of_runs} are ok', end='\r')
else:
print('No!')
if __name__ == '__main__':
tester(100, 50000)
# print(do_work(100).__next__())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment