Warning

This page is located in archive.

- Q/A
- Practical work

Read about the Josephus problem.

In module `josephus.py`

, create function `josephus_permutation`

that will output items of the given sequence in the order of execution.

def josephus_permutation(seq, start=0, skip=0): """ Return Josephus permutation of the given sequence. :param seq: Any sequence (list, string, ...) to be permuted. :param start: int, an index of item where the counting shall start. :param skip: int, how many items shall be skipped :return: permuted list of items of the same length as the original seq >>> josephus_permutation([1, 2, 3]) [1, 2, 3] >>> josephus_permutation([1, 2, 3, 4, 5], start=3) [4, 5, 1, 2, 3] >>> josephus_permutation([1, 2, 3, 4, 5], skip=3) [4, 3, 5, 2, 1] >>> josephus_permutation([1, 2, 3, 4, 5], start=3, skip=2) [1, 4, 3, 5, 2] """

**Test the function** during development.

In module `josephus.py`

, create function `survivor`

which will return the index of the last item in the Josephus permutation.
You should implement the function without actually constructing the permutation!
(But you can use function `josephus_permutation`

to create your test cases.)

def survivor(seq, start=0, skip=0): """ Return the last element of Josephus permutation of the given sequence :param seq: Any sequence (list, string, ...) to be permuted. :param start: int, an index of item where the counting shall start. :param skip: int, how many items shall be skipped :return: item of seq, the last one in the respective Jos. permutation >>> survivor([1, 2, 3]) 3 >>> survivor([1, 2, 3, 4, 5], start=3) 3 >>> survivor([1, 2, 3, 4, 5], skip=3) 1 >>> survivor([1, 2, 3, 4, 5], start=3, skip=2) 2 """

**Create your own additional test cases!**

- Finish the above tasks.
- Read chapter 20 - Dictionaries of [Wenthworth].

courses/be5b33prg/labs/week_08.txt ยท Last modified: 2018/08/13 09:48 (external edit)