SHARE
TWEET

Josephus

a guest Nov 29th, 2011 108 Never
  1. #
  2. # josephus.py
  3. #
  4. # function josephus(n,m) that returns a list of n people, numbered from 0 to n-1, in the order in which they are executed,
  5. # every mth person in turn, with the sole survivor as the last person in the list.
  6. #
  7.  
  8. def josephus(n, m):
  9.   lst = range(n)
  10.   while len(lst) > 1:
  11.     # mth executed person in turn
  12.     executions = lst[::m]
  13.     # difference (alive people)
  14.     lst = filter(lambda x : x not in executions, lst)
  15.     print('Executed: %s' %  executions)
  16.     print('Alive: %s' %  lst)
  17.     print('-' * 33)
  18.   print('Last survival: %s' % lst)
  19.  
  20.  
RAW Paste Data
Top