Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hot-potato python #255

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
Binary file added Algorithm/.DS_Store
Binary file not shown.
60 changes: 60 additions & 0 deletions Algorithm/hot_potato/hot_potato.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
def init_game(players, N):
for player_idx in range(0, N):
players[player_idx] = 'Alive'

def get_next_idx(index, N, I, players):
sum = 0
index = index+1
while True:
if index >= N:
index = index-N
if players[index] == 'Alive':
sum += 1
if sum == I:
return index
index += 1

def get_good_idx(players):
for i, pl in enumerate(players):
if pl == 'Alive':
return i+1

def has_to_stop(players):
stop = False
alives = 0
for pl in players:
if pl == 'Alive':
alives += 1
if alives == 1:
stop = True
return stop

def hot_potato(N, I, players):
idx = I-1
loop = 1
while True:
if loop == 1:
players[idx] = 'Dead'
loop += 1
continue

idx = get_next_idx(idx, N, I, players)
players[idx] = 'Dead'

if has_to_stop(players):
return

N = 7
I = 3
assert I <= N

players = list(range(N))

init_game(players, N)
print(f'Players: {players}\n')

hot_potato(N, I, players)
print(f'Alive Player: {players}\n')

goodIdx = get_good_idx(players)
print(f'Position to win: {goodIdx}')