You are on page 1of 4

The Canny Skipper - A Puzzle For Demonstrating Data

Structures And Recursion

Michael Wirth
School of Computer Science
University of Guelph
Guelph, N1G2W1 Canada
mwirth@uoguelph.ca

ABSTRACT iteratively, and then translated to a binary recursive algo-


Recursion is an interesting concept in problem solving. One rithm which turns out to be extremely inefficient. The re-
interesting algorithm which has been used to illustrate vari- cursive solution for the Towers of Hanoi is elegant, but to
ous aspects of recursion is the Josephus Puzzle. This paper many students it is an enigma, and the iterative solution
describes a similar problem, that of the Canny Skipper, and is long-winded. This paper describes the use of a board
shows how it can be used, both in the form of problem solv- puzzle in teaching recursion, from deriving a non-recursive
ing via tactile play, and deriving an algorithm using data solution using data structures, to a simple recursive algo-
structures and recursion. rithm. The contribution of this paper is that it introduces
a puzzle that postulates a multi-tiered approach to teaching
recursion, which can also be used for kinesthetic learning.
Categories and Subject Descriptors
F.3.3 [Studies of Program Constructs]: Program and re- 2. THE CANNY SKIPPER
cursion schemes; D.3.3 [Language Constructs and Fea-
The Josephus puzzle [3] is a form of counting-out puz-
tures]: Recursion
zle, based on the progressive elimination of participants in
a group. A group of n people are standing in a circle, num-
General Terms bered consecutively clockwise from 1 to n. Starting with the
Algorithms first person, every k th person is removed, proceeding clock-
wise. The task is to determine the position of the remaining
survivor, Jk (n). For example, if n = 6, and k =2, then peo-
Keywords ple are removed in the following order 2, 4, 6, 3, 1, and the
recursion, data structures, Canny Skipper, Josephus Puzzle last person remaining is no. 5. The process of removing
people is known as “reduction using a step of size k ” [12].
1. INTRODUCTION
One of the core courses in intermediary computer science
is data structures. This is the course where students are ex-
posed to programming constructs such as lists, stacks, trees
and concepts such as recursion. Recursion is fundamental to
computer science [9], especially as a problem solving heuris-
tic. Recursion is, however, not a trivial topic, and is often
considered to be one of the more difficult concepts for stu-
dents to understand [1, 7], and apply as a problem solving
strategy [14, 8]. Whilst students often gain an understand-
ing of programming concepts from everyday analogies, re-
cursion offers very few such analogies [11, 18]. Students are
often bewildered by the abstract nature of recursion [17].
This is largely to do with the examples used to illustrate
its behavior which often suffer from being trivial, or inef-
fective. Fibonacci is an example which can be easily solved
Figure 1: The Canny Skipper puzzle (Courtesy of
The Puzzle Museum)
Permission to make digital or hard copies of all or part of this work for
personal or classroom use is granted without fee provided that copies are Such puzzles exist in other forms, namely board games
not made or distributed for profit or commercial advantage and that copies such as “The Canny Skipper”, designed by Unicorn Prod-
bear this notice and the full citation on the first page. To copy otherwise, to ucts, circa 1930. In this variant of the Josephus puzzle,
republish, to post on servers or to redistribute to lists, requires prior specific fifteen shipwrecked Britons in a lifeboat pick up 15 bandits
permission and/or a fee. from the sea. In heavy seas the overloaded boat begins to
WCCCE’14, May 2–3, 2014, Richmond, British Columbia, Canada.
Copyright 2014 ACM 978-1-4503-2899-9/14/05 founder, and the skipper realized he has to reduce the num-
http://dx.doi.org/10.1145/2597959.2597966 ber to their original 15. He arranges the men in horseshoe
formation and suggested every 9th man be thrown overboard run screaming from recursion thinking it impossible that a
(See Figure 2). Proceeding as planned, all the bandits are function six lines in length that calls itself twice can solve
thrown overboard, and the Britons saved. The task here is the problem for any number of discs.
to arrange the individuals in such a manner that only the The use of games to promote an understanding of com-
bandits are selected and thrown overboard. The positions puter science concepts has been well documented [4]. Puz-
of the a5 surviving crew are: zles are a form of game which are designed to test ingenuity.
Puzzle-based learning also helps develop abstract reasoning,
1, 2, 3, 4, 10, 11, 13, 14, 15, 17, 20, 21, 25, 28, critical thinking skills, and enhances creativity [5]. TOH is
29. a mechanical puzzle, however it does more to illuminate the
The game board is illustrated in Fig.1 1 . The Canny Skipper inadequacies of recursion than to facilitate an understand-
is related to the puzzle of the thirty counters [10] which is ing. Whilst the TOH can be easily solved in a hands-on
noted as: tactile manner, deriving an algorithmic solution using data
structures is not trivial, and the ubiquitous recursive solu-
“How may fifteen black and fifteen white counters tion although elegant often leaves students confused. Part of
be arranged in a circle so that a person counting the allure of using puzzles such as the Canny Skipper is that
round the circle and throwing out every ninth they provide a multi-tiered approach to learning recursion.
counter, till only fifteen counters are left, will In the first tier, students use Canny Skipper as a kinesthetic
find that the counters left are all white and those learning activity, exploring different approaches to solving
thrown out have been all black ?” the puzzle. In the next level a simple iterative solution is
derived. Now Canny Skipper can be used to illustrate data
This classical problem has numerous variants. In most cases structures such as circular arrays and linked-lists, and in the
there seem to be fifteen preferred individuals which are dis- final tier, a recursive solution can be formulated.
tinguished from fifteen other individuals. One of the most
widely known variants, from late 18th century editions of
Jacques Ozanam’s Recreations mathematiques et physiques
involves Christians and Turks. A 17th century Japanese ver-
sion involves children and step-children of the Emperor, and
the sea voyage is replaced by a second wife who wishes to
deprive her step-children of an inheritance. There are also
numerous versions from India, Scotland, Ireland, Europe,
the Middle East (14th C.), and Scandinavia. In classical
times, the solutions to these puzzles were often in the form
of rules such as:
From numbers, aid, and art,
Never will fame depart.
By substituting the vowels a, e, i, o, u with the values 1, 2,
3, 4, 5 respectively, the arrangement of the numbers can be
recalled [19]. For the Canny Skipper this becomes:
From numbers, aid, and art, Never will fame depart.
4 5 2 13 1 1 2 2 3 1 2 2 1
This implies four Britons are to be placed together, followed
by 5 bandits, followed by 2 Britons, 1 bandit etc.
While the Josephus puzzle and the Canny Skipper share Figure 2: An example of Canny Skipper - eliminat-
many of the same characteristics, the Josephus version of ing the first four participants: 9, 18, 27, 6
the problem is interested in the last one or two survivors,
whereas the Canny Skipper counts out half the group.
4. KINESTHETIC LEARNING
3. PEDAGOGY
Kinesthetic, or tactile learning is a process in which stu-
Recursion is the bugbear of problem solving techniques. dents learn by actively carrying out physical activities rather
Part of the problem is based on the fact that recursion is than by passively listening to lectures [13]. Humans gener-
often introduced in a second level data structures course, ally learn best by doing something first, then abstracting to
after a brief but all too often limited primer in introductory more general concepts [16]. In this situation, students can
courses. It is therefore not unusual for students to complete either play with the puzzle in the manner of a board game,
such a course believing that recursion as a problem solving or perform an activity such as having 30 students stand in a
tool has limited scope. This is regardless of the fact that horseshoe and re-enact the puzzle by “counting-out” aloud.
it is vital for certain operations on data structures, such as Kinesthetic activities offer a way to enhance the understand-
tree traversal. Considering the use of early examples such as ing of the problem so that students can derive an algorithmic
Towers of Hanoi (TOH), it is not inconceivable that students solution to the puzzle. Such kinesthetic approaches to intro-
1 ducing recursion have been used before with problems like
Copyright (c) 2014 Hordern-Dalgety Collection. puzzlemu-
seum.com the Towers of Hanoi [6].
5. USING DATA STRUCTURES a data structure, and reengineering it to be implemented
Approaching the Canny Skipper using data structures al- using recursion, the student learns how the two are related.
lows the student to leverage the existing knowledge obtained The most appropriate recursive algorithm is a simple exten-
during the kinesthetic activities. There are a number of sion of the algorithm using the list. When one node of the
methods of solving Canny Skipper in which the structures list of n nodes is removed, the problem essentially becomes
used mimic the physical notion of people standing in a circle a subproblem with (n − 1) nodes. Here is the code using
(used to approximate the horseshoe). The first is a circular recursion in Python. Variables are the same, the loop has
array, the second is a linked list, and by extension a circular been replaced with a recursive call to the function itself,
linked list (CLL). A circular array can be used by creating canny skipperREC.
an array of length 30, and setting every element to 1. By def canny_skipperREC(lst, k, p):
cycling through the array in a circular fashion, every 9th if len(lst) == 15:
element that has a value of 1 is set to 0, skipping any zero print ’surviving crew: ’, lst[0:15]
elements, until there are only fifteen elements left set to 1. else:
The code to perform this in Python is shown in Appendix lst.pop(p)
A.1. p = (p + k) % len(lst)
With the circular linked list, a CLL is created with 30 canny_skipperREC(lst,k,p)
nodes, each having a numeric label from 1 to 30. Starting
at the root node, every 9th node is deleted in a circular The recursive function can be called in a similar fashion
fashion. This continues until only 15 nodes remain, in a to the iterative list-based method:
similar fashion to how the puzzle is expressed. Both these
methods work nicely because there is a direct abstraction boat = range(1,31)
from the puzzle solved by hand to the data structure. A canny_skipperREC(boat,8,8)
more generic list-based algorithm works by calculating the The code can be inserted into Online Python Tutor’s vi-
next position, (pn ) in the list (lst) to eliminate, based on sualizer to observe the operation of the stack frames as the
the current position (po ) and the step size (k) using the function runs.
equation:
pn = (po + k) mod (length(lst)) 7. COMPARISON TO JOSEPHUS
The recursive solution for Canny Skipper differs from that
The idea of using a list can be easily simulated in a lan-
of the Josephus Puzzle - it is not possible to use the same
guage such as Python. Here is the Python function associ-
recursive algorithm to provide the positions of the fifteen
ated with a list-based algorithm:
bandits. That’s largely due to the recursive algorithms in-
def canny_skipperLIST(lst, k, p): ner mechanism. The recurrence relation for the generalized
while (len(lst) > 15): version of the Josephus puzzle is shown below[15] with n rep-
lst.pop(p) resenting the number of participants, and k the step size:
p = (p + k) % len(lst)
print ’surviving crew: ’, lst[0:15] f (1, k) = 0
where lst is the list, k is the step size, and p is the current f (n, k) = (f (n − 1, k) + k) mod n
position. This function is called in the following manner: Note that this works with a starting position of 0. An
algorithm coded in Python to calculate the position of the
boat = range(1,31) final participant with starting position 1 is implemented as
canny_skipperLIST(boat,8,8) follows:
The first line of code creates a list of length 30, with def josephus(n, k):
the containing the values 1 to 30. The upper limit of the if (n == 1):
list is always one more than the value required, hence 31 return 1;
as opposed to 30. The second line invokes the function else:
canny skipperLIST. The initial position to be eliminated is 8 return (josephus(n-1, k) + k - 1) % n + 1
because Python’s lists are indexed from 0. This is the same
For example, with 5 participants, and a step of size 3, the
reason that the step size is 8 (as opposed to 9) as one node
recursive solution produces the following sequence of calls:
is eliminated from the list each time. Solving the puzzle
using data structures prior to exploring a recursive solution josephus(5,3) -> (josephus(4,3)+3-1)%5+1 -> 4
allows students to familiarize themselves with a possible so- josephus(4,3) -> (josephus(3,3)+3-1)%4+1 -> 1
lution, and also places the recursive solution into the proper josephus(3,3) -> (josephus(2,3)+3-1)%3+1 -> 2
context. Many papers discuss the importance of learning to josephus(2,3) -> (josephus(1,3)+3-1)%2+1 -> 2
solve problems by means of iteration prior to introducing josephus(1,3) -> 1
recursion [2].
This produces the correct value - participant 4, however
there is no trace of the participants removed which is ef-
6. EXTENSION TO RECURSION fectively: 3, 1, 5, 2 → 4. In reality, there is no necessity
The natural extension of using the Canny Skipper in the to create a complex algorithm to solve the Canny Skipper
context of data structures is to extend its influence to recur- recursively. The recursive solution works by using a list
sion. By taking a problem which can be easily solved using structure, analogous to the real world problem.
8. CONCLUSION a programming technique. ACM SIGCSE Bulletin,
Illustrating data structures, and making abstract concepts 20(1):275–278, 1988.
such as recursion easier to understand involves finding ex- [19] E. Wingate, J. Dodson, and A. Plain. A Plain and
amples which are inspiring for students. This paper has de- Familiar Method for Attaining the Knowledge and
scribed the use of a physical puzzle, “The Canny Skipper”, Practice of Common Arithmetic. Hitch, Hawes and
to increase problem solving prowess, and build exploratory Baldwin, London, 1751.
behavior. First used in the context of tactile learning to
engage students, it is then used to illustrate various data APPENDIX
structures, and build upon a recursive solution.
A. PYTHON CODE
9. REFERENCES A.1 Canny Skipper using a circular array
The following Python code implements the Canny Skip-
[1] J. Anderson, R. Farrell, and R. Sauers. Learning to per using a simulated circular array (in Python a static list
program in lisp. Cognitive Science, 8:87–129, 1984. structure). The array skipper is created and filled with
[2] Y. Anzai and Y. Uesato. Is recursive computation 1’s. Then the array is cycled through continuously and ev-
difficult to learn? In Report: Psychology Dept. ery 9th element is set to zero, until the number of elements
Carnegie-Mellon University, 1982. eliminated (elm) equals 15. The elements remaining with
[3] W. R. Ball. Mathematical Recreations and Essays. the value 1 represent the non-eliminated participants in the
Macmillan and Co., London, 1905. puzzle.
[4] K. Becker. Teaching with games: The minesweeper
and asteroids experience. J. Computing Small def canny_skipper(n,k):
Colleges, 17(2):23–33, 2001. skipper = [1] * n
elm = 1
[5] N. Falkner, R. Sooriamuri, and Z. Michalewicz.
j = 0
Teaching puzzle-based learning: Development of basic
while (elm <= 15): #(count(skipper) > 15):
concepts. Teaching Mathematics and Computer
pos = 0
Science, 10(1):183–204, 2012.
while (pos < k):
[6] K. Gunion, T. Milford, and U. Stege. Curing recursion if (j >= n):
aversion. ACM SIGCSE Bulletin, 41(3):124–128, 2009. j = 0
[7] B. Haberman and H. Averbuch. The case of base if (skipper[j] == 1):
cases: Why are they so difficult to recognize? student pos = pos + 1
difficulties with recursion. ACM SIGCSE Bulletin, j = j + 1
34(3):84–88, 2002. skipper[j-1] = 0
[8] H. Kahney. What do novice programmers know about elm = elm + 1
recursion. In SIGCHI Conf. on Human Factors in print skipper
Computing Systems, pages 235–239, 1983.
[9] D. McCracken. Ruminations on computer science print "survivors =",
curricula. Communications of the ACM, 30(1):3–4, for i in range(0,30):
1987. if (skipper[i] == 1):
[10] G. Murphy. The puzzle of the thirty counters. print i+1,
Bealoideas, 12:3–28, 1942.
[11] P. Pirollo and J. Anderson. The role of learning from canny_skipper(30,9)
examples in the acquisition of recursive programming
Here is a sample of the visual output from the program:
skills. Canadian Journal of Psychology, 39(2):240–272,
1985. 1 - [111111110111111111111111111111]
[12] R. Robinson. Recursion and double recursion. Bull. 2 - [111111110111111110111111111111]
Amer. Math. Soc., 54:987–993, 1948. 3 - [111111110111111110111111110111]
[13] P. Sivilotti and S. Pike. The suitability of kinesthetic 4 - [111110110111111110111111110111]
learning activities for teaching distributed algorithms. 5 - [111110110111111010111111110111]
ACM SIGCSE Bulletin, 39(1):362–366, 2007. 6 - [111110110111111010111111100111]
[14] R. Sooriamurthi. Problems in comprehending 7 - [111110010111111010111111100111]
recursion and suggested solutions. ACM SIGCSE 8 - [111110010111111010011111100111]
Bulletin, 33(3):25–28, 2001. 9 - [111110010111111010011111100110]
[15] P. Tait. On the generalization of the josephus problem. 10- [111110010110111010011111100110]
Proc. Royal Society of Edinburgh, 22:165–168, 1898. 11- [111110010110111010011110100110]
[16] J. Tranquillo. Kinesthetic learning in the classroom. 12- [111110000110111010011110100110]
http://http://www.facstaff.bucknell.edu/ 13- [111110000110111010011010100110]
jvt002/Docs/ASEE-2008b.pdf, 2008. Accessed: 14- [111100000110111010011010100110]
2013-01-06. 15- [111100000110111010011000100110]
[17] M. Troy and G. Early. Unraveling recursion, part i
survivors=
and ii. The Computing Teacher, March-April, 1992.
1 2 3 4 10 11 13 14 15 17 20 21 25 28 29
[18] S. Wiedenbeck. Learning recursion as a concept and as

You might also like