From 5ff6ecdb26884b3814dc159830715f7b16acaf44 Mon Sep 17 00:00:00 2001 From: Famosi Date: Mon, 30 Nov 2020 15:38:53 +0100 Subject: [PATCH 1/3] Added hot-potato python --- .DS_Store | Bin 12292 -> 6148 bytes Algorithm/.DS_Store | Bin 0 -> 6148 bytes Algorithm/hot_potato/hot_potato.py | 60 +++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 Algorithm/.DS_Store create mode 100644 Algorithm/hot_potato/hot_potato.py diff --git a/.DS_Store b/.DS_Store index 6b19b25ca24f98b36d6bffae549b9dbe6dc5f790..a9923b1a02b0a28f22e04c10c91fb67e3cf29398 100644 GIT binary patch delta 433 zcmZokXfcprU|?W$DortDU=RQ@Ie-{MGjUEV6q~3gIoUvmMH0woUEeQfgYS67>u=_nYPo7U)4L9t!wMHd1<4wvb@n*hMvJj9tB?i%U?V(I}2is_YO1I<=wU~=@m8$j~je0 zY|*yqe=a_bsBV=6DhX5)s3cHH;Qo_Rw5plE8f}0r`i5OYL;o-U+R{ z4sJ@}^}SMff6*s3_h&`fPM7VS&^i>1LumuFR+bpG0Oxrja@y&#y%X91w_1Q(tsJ>l zRwy+e9bDn$R+|oOb+05)N#LLaUgFWHy-l6mq&6<>6YuS0l{TjWNu_-nM4RgTmRI_P zy-8KQ3G#+l>(^7Xy#o4XRHu*IJB+plhZSphP2Pgn1{Hb(zO%NN;eW{Rk30URErIPZ z?vhvdTbvQJ;o4&kJ$Qw0mlYx*Zt8nOWI?-ds z-Hgvy8J}|zAI+{{E}`w%H9oESEOnx>n{oOxTWJNczpr++2C4RKI&+^6k0r zf1UyR517?^#%!s-*W$gE z$&~%N}$ZDbKo|N}(pN5sy6yjeN}K zC3=ar=e)He;k#x3ylGSRe8x?lBjz^ddoTZb{YWcsX1MWzxNX*{wRdt&axW2&J8JKt zJu^0kbR9H zL8pB#kWi2DN=?lropF)o##~O~``)k**kx&@_FH=Jg1u-jrbtD=p^qb7ei)=8%MR=6 z_%1cj66sIF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~f_z9!V7yAAgY^awdk;0$vvWD!uCFLt?N%)S!SGJ`ynoyCvmLYVO8n3?Yhz|Rw z2t%Ap>HUl(rk~(^R+YT0ICxzBzwXG`?B{{|JS5?RzVQLHme+ax{y%^C|Noq*x>XXW zB=G-~Ky74haqc2(s~xgF{nlP^ea5x-g4W5o`A_k*{!=`y{}fOATtqvqKF{uimL9MF X{7(SFPZAIKcn8#ea?Xb9|t z<9kUq9GfRWWV{D2znAwXA-|NQVT|#{Fsw3GWQD zemWd9{QeF0F8s6`82eJncxL;-Rn!@^iaVz=N&P74^i^>bb|B^UI!Z!0YRF*{4%9fF z4p^3L+pS`0Jgy&>MeXQdQWoRmTBR)N`-hW>ZI$+_C+F>lr|0p@Jx%;8&9vTbk* z?_exl>!a6CVwv1Sd|ErLAtVNf0b*bk88By^wYiFmqcsr&#K5l@!1FU*?=XPyLDl4c-IQhHYf__ nWg2HCV5qAYa`7tO1yurmfd-&!Fw+Pg5c&~NG*Ckf{3!!pcqLRM literal 0 HcmV?d00001 diff --git a/Algorithm/hot_potato/hot_potato.py b/Algorithm/hot_potato/hot_potato.py new file mode 100644 index 0000000..782f13b --- /dev/null +++ b/Algorithm/hot_potato/hot_potato.py @@ -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 == 3: + 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 = 2 +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}') From aefba06e452a56832f0e5a508314ad50a2e903e7 Mon Sep 17 00:00:00 2001 From: Famosi Date: Tue, 1 Dec 2020 09:29:00 +0100 Subject: [PATCH 2/3] Fix sum in get_next_idx() --- Algorithm/hot_potato/hot_potato.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Algorithm/hot_potato/hot_potato.py b/Algorithm/hot_potato/hot_potato.py index 782f13b..4ee278c 100644 --- a/Algorithm/hot_potato/hot_potato.py +++ b/Algorithm/hot_potato/hot_potato.py @@ -10,7 +10,7 @@ def get_next_idx(index, N, I, players): index = index-N if players[index] == 'Alive': sum += 1 - if sum == 3: + if sum == I: return index index += 1 From 54734f3f4e68b4e88684f4cc660564d603f6d026 Mon Sep 17 00:00:00 2001 From: Famosi Date: Tue, 1 Dec 2020 09:30:45 +0100 Subject: [PATCH 3/3] Set N=7 I=3 --- Algorithm/hot_potato/hot_potato.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Algorithm/hot_potato/hot_potato.py b/Algorithm/hot_potato/hot_potato.py index 4ee278c..808fdf4 100644 --- a/Algorithm/hot_potato/hot_potato.py +++ b/Algorithm/hot_potato/hot_potato.py @@ -44,7 +44,7 @@ def hot_potato(N, I, players): if has_to_stop(players): return -N = 2 +N = 7 I = 3 assert I <= N