Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Josephus

By: a guest on Nov 29th, 2011  |  syntax: Python  |  size: 0.56 KB  |  hits: 72  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.