-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJosephusPermutation.txt
More file actions
48 lines (48 loc) · 1.83 KB
/
JosephusPermutation.txt
File metadata and controls
48 lines (48 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Josephus permutation, Survivor??
#A group of n prisoners stand in a circle awaiting execution. Starting from an arbitrary position(0), the executioner kills every kth person until one person remains standing, who is then granted freedom (see examples).
#Create a function that takes 2 arguments — the number of people to be executed n, and the step size k, and returns the original position (index) of the person who survives.
List = []
List2 = []
def Josephus_Permutation():
print("Please enter your sample size")
n = input()
n = int(n)
while n>0:
List.insert(-0, n)
n-=1
print(List)
#Creates the list using a while loop
a = 1
print("Please enter your step size")
k = input()
k = int(k)
#Builds a new list of values to remove from first List.
while ((k*a) < len(List)) and (len(List)>0):
List2.insert(0, List[k*a])
print("List 2 ,",List2)
print("List ,",List)
a+=1
#Removes the eliminated values
for i in List and List2:
if i in List and List2:
List.remove(i)
print("List 2 ,",List2)
print("List ,", List,)
a = 1
# Resets the counter to 1
# Sifts through the list when it is smaller
# than the step length k using remainder division
while len(List)>=2:
k = (k)//(len(List))
k = k-1
print(k)
List2.insert(0, List[k])
print("List 2 ,",List2)
print("List ,",List)
for i in (List and List2):
if i in List and List2:
List.remove(i)
#Returns the result as the location of the survivor
print("List 2 ,",List2)
print("Index position of survivor is,", List,)
Josephus_Permutation()