Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Computer Lab 08, Automated testing

  • Q/A
  • Practical work

Practical work

Josephus problem

Read about the Josephus problem.

Josephus permutation

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.

Survivor

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!

Homework

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