Advertisement
Guest User

Josephus

a guest
Nov 29th, 2011
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  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.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement