Skip to content

Commit 0bb4a6d

Browse files
committed
Create josephus problem.py
1 parent e891509 commit 0bb4a6d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def josephus(person, k, index):
2+
3+
# when only one person is left
4+
if len(person) == 1:
5+
print(person[0])
6+
return
7+
8+
# find the index of first person which will die
9+
index = ((index+k)%len(person))
10+
11+
# remove the first person which is going to be killed
12+
person.pop(index)
13+
14+
# recursive call for n-1 persons
15+
Josh(person,k,index)
16+
17+
n = 14
18+
k = 2
19+
k-=1
20+
21+
index = 0
22+
23+
person=[]
24+
for i in range(1,n+1):
25+
person.append(i)
26+
27+
josephus(person,k,index)

0 commit comments

Comments
 (0)