This problem takes its name by arguably the most important event in the life of the ancient historian Josephus: according to his tale, he and his 40 soldiers were trapped in a cave by the Romans during a siege.

Refusing to surrender to the enemy, they instead opted for mass suicide, with a twist: **they formed a circle and proceeded to kill one man every three, until one last man was left (and that it was supposed to kill himself to end the act)**.

Well, Josephus and another man were the last two and, as we now know every detail of the story, you may have correctly guessed that they didn't exactly follow through the original idea.

You are now to create a function that returns a Josephus permutation, taking as parameters the initial *array/list of items* to be permuted as if they were in a circle and counted out every *k* places until none remained.

**Tips and notes:** it helps to start counting from 1 up to n, instead of the usual range 0..n-1; k will always be >=1.

For example, with n=7 and k=3 `josephus(7,3)` should act this way.
```
[1,2,3,4,5,6,7] - initial sequence
[1,2,4,5,6,7] => 3 is counted out and goes into the result [3]
[1,2,4,5,7] => 6 is counted out and goes into the result [3,6]
[1,4,5,7] => 2 is counted out and goes into the result [3,6,2]
[1,4,5] => 7 is counted out and goes into the result [3,6,2,7]
[1,4] => 5 is counted out and goes into the result [3,6,2,7,5]
[4] => 1 is counted out and goes into the result [3,6,2,7,5,1]
[] => 4 is counted out and goes into the result [3,6,2,7,5,1,4]
```
So our final result is:
```
josephus([1,2,3,4,5,6,7],3)==[3,6,2,7,5,1,4]
```
For more info, browse the Josephus Permutation page on wikipedia; related kata: Josephus Survivor.

Also, [live game demo](https://iguacel.github.io/josephus/) by [OmniZoetrope](https://www.codewars.com/users/OmniZoetrope).

Source: codewars | Difficulty: EASY
Tags: Combinatorics

Mathematics

Starter code: def josephus(items,k):
	

Solutions:
def josephus(xs, k):
	(i, ys) = (0, [])
	while len(xs) > 0:
		i = (i + k - 1) % len(xs)
		ys.append(xs.pop(i))
	return ys


def josephus(n, k):
	(i, ans) = (0, [])
	while n:
		i = (i + k - 1) % len(n)
		ans.append(n.pop(i))
	return ans


from collections import deque

def josephus(items, k):
	q = deque(items)
	return [[q.rotate(1 - k), q.popleft()][1] for _ in items]


class Circle(object):

	def __init__(self, items):
		self.items = items[:]

	def pop(self, index):
		norm_index = index % len(self.items)
		return (self.items.pop(norm_index), norm_index)

	def not_empty(self):
		return self.items

def josephus_iter(items, k):
	circle = Circle(items)
	i = k - 1
	while circle.not_empty():
		(item, i) = circle.pop(i)
		yield item
		i += k - 1

def josephus(items, k):
	return list(josephus_iter(items, k))


def josephus(items, k):
	if len(items) == 0:
		return []
	if len(items) == 1:
		return [items[0]]
	i = (k - 1) % len(items)
	return [items[i]] + josephus(items[i + 1:] + items[:i], k)


def josephus(items, k):
	out = []
	i = 0
	while len(items) > 0:
		i = (i + k - 1) % len(items)
		out.append(items[i])
		del items[i]
	return out


def josephus(n, k):
	k -= 1
	i = k
	result = []
	while n:
		if i < len(n):
			result.append(n.pop(i))
			i += k
		else:
			while i >= len(n):
				i -= len(n)
	return result


def josephus(items, k):
	cursor = 0
	killed = []
	while len(items):
		cursor = (cursor + k - 1) % len(items)
		killed.append(items.pop(cursor))
	return killed


def josephus(itemlist, interval):
	length = len(itemlist)
	if interval == 1:
		return itemlist
	count = 1
	disposal = []
	while len(disposal) != length:
		index = 0
		for item in itemlist:
			if count == interval:
				disposal.append(item)
				itemlist.pop(index)
				count = 1
				try:
					print(itemlist[index])
				except:
					break
			count += 1
			index += 1
	return disposal


def josephus(items, k):
	(i, res) = (0, [])
	while items:
		i = (i + k - 1) % len(items)
		res += [items.pop(i)]
	return res