We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e891509 commit 0bb4a6dCopy full SHA for 0bb4a6d
1 file changed
dynamic_programming/josephus problem.py
@@ -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