Advertisement
DiYane

Josephus Permutation..

Oct 7th, 2023
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. def josephus(elements, skip):
  2.     idx = 0
  3.     result = []
  4.     while len(elements) > 0:
  5.         idx = (idx + skip - 1) % len(elements)
  6.         result.append((elements.pop(idx)))
  7.  
  8.     return result
  9.  
  10. def trim_white_spaces(elements):
  11.     result = elements
  12.     no_spaces = ''
  13.     for num in result:
  14.         if no_spaces != '':
  15.             no_spaces += ','
  16.         for char in str(num):
  17.             if ord(char) in range(48, 58):
  18.                 no_spaces += char
  19.  
  20.     return '[' + no_spaces + ']'
  21.  
  22.  
  23. elements_list = input().split()
  24. k = int(input())
  25.  
  26. print((trim_white_spaces(josephus(elements_list, k))))
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement