Created by Tim Bell, Ian H. Witten and Mike Fellows
Adapted for classroom use by Robyn Adams and Jane McKenzie Created by Tim Bell, Ian H. Witten and Mike Fellows
Adapted for classroom use by Robyn Adams and Jane McKenzie
Illustrations by Matt Powell
2015 Revision by Sam Jarman
An enrichment and extension programme for primary-aged students
Introduction
Computers are everywhere. We all need to learn how to use them, and many of us use them every day. But how do they work? How do they think? And how can people write software that is fast and easy to use? Computer science is a fascinating subject that explores these very questions. The easy and fun activities in this book, designed for studentren of all ages, introduce you to some of the building blocks of how computers work—without using a computer at all!
This book can be effectively used in enrichment and extension programmes, or even in the regular classroom. You don’t have to be a computer expert to enjoy learning these principles with your students. The book contains a range of activities, with background information explained simply. Answers to all problems are provided, and each activity ends with a ‘what’s it all about?’ section that explains the relevance of the activities.
Many of the activities are mathematically based, e.g. exploring binary numbers, mapping and graphs, patterns and sorting problems, and cryptography. Others link in well with the technology curriculum, and the knowledge and understanding of how computers work. The studentren are actively involved in communication, problem solving, creativity, and thinking skills in a meaningful context. The activities also provide a very engaging way to explore “computational thinking”, which is gaining traction in school curricula.
In addition to this book, the “Unplugged” project has as lot of free, online resources including videos, pictures and extra material at csunplugged.org. As part of the 2015 revision of this book, we have also released a brand new website, with more resources, better access to the open source material, and stronger curriculum links to match the appearance of computer science and computational thinking in school curricula.
This book was written by three computer science lecturers and two school teachers, and is based on our experience in classrooms as well as feedback from hundreds of educators over two decades. We have found that many important concepts can be taught without using a computer—in fact, sometimes the computer is just a distraction from learning. Often computer science is taught using programming first, but not every student finds this motivating, and it can be a significant barrier to getting into the really interesting ideas in computer science. So unplug your computer, and get ready to learn what computer science is really about!
Licensed under Creative Commons i © 2015 Computer Science Unplugged (csunplugged.org)
This book is available as a free download thanks to a generous grant by Google, Inc. It is distributed under a Creative Commons Attribution- NonCommercial-ShareAlike licence, which means that you are free to share (copy, distribute, and transmit) the book. It also allows you to remix the book. These are only available under the following conditions: you include attribution to the authors, you do not use this book for commercial purposes, and if you alter, transform or build upon this work, you share under the same or similar license. More details of this license can be found online by searching: CC BY-NC-SA 3.0.
We encourage the use of this material in educational settings, and you are welcome to print your own copy of the book and distribute worksheets from it to students. We welcome enquiries and suggestions, which should be directed to the authors (see csunplugged.org).
This book has been translated into many languages. Please check the web site for information about the availability of translations.
ii Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Acknowledgements
Many children and teachers have helped us to refine our ideas. The children and teachers at South Park School (Victoria, BC), Shirley Primary School, Ilam Primary School and Westburn Primary School (Christchurch, New Zealand) were guinea pigs for many activities. We are particularly grateful to Linda Picciotto, Karen Able, Bryon Porteous, Paul Cathro, Tracy Harrold, Simone Tanoa, Lorraine Woodfield, and Lynn Atkinson for welcoming us into their classrooms and making helpful suggestions for refinements to the activities. Gwenda Bensemann has trialed several of the activities for us and suggested modifications. Richard Lynders and Sumant Murugesh have helped with classroom trials. Parts of the cryptography activities were developed by Ken Noblitz. Some of the activities were run under the umbrella of the Victoria “Mathmania” group, with help from Kathy Beveridge. Earlier versions of the illustrations were done by Malcolm Robinson and Gail Williams, and we have also benefited from advice from Hans Knutson. Matt Powell has also provided valuable assistance during the development of the “Unplugged” project. We are grateful to the Brian Mason Scientific and Technical Trust for generous sponsorship in the early stages of the development of this book.
Special thanks go to Paul and Ruth Ellen Howard, who tested many of the activities and provided a number of helpful suggestions. Peter Henderson, Bruce McKenzie, Joan Mitchell, Nancy Walker-Mitchell, Gwen Stark, Tony Smith, Tim A. H. Bell1, Mike Hallett, and Harold Thimbleby also provided numerous helpful comments.
We owe a huge debt to our families: Bruce, Fran, Grant, Judith, and Pam for their support, and Andrew, Anna, Hannah, Max, Michael, and Nikki who inspired much of this work,2 and were often the first children to test an activity.
We are particularly grateful to Google Inc. for sponsoring the Unplugged project, and enabling us to make this edition available as a free download.
We welcome comments and suggestions about the activities. The authors can be contacted via csunplugged.org.
1 No relation to the first author. 2 In fact, the text compression activity was invented by Michael.
Licensed under Creative Commons iii © 2015 Computer Science Unplugged (csunplugged.org)
Contents
Introduction i
Acknowledgements iii
Data: the raw material—Representing information 1
Count the Dots—Binary Numbers 3
Colour by Numbers—Image Representation 16
You Can Say That Again! —Text Compression 26
Card Flip Magic—Error Detection & Correction 35
Twenty Guesses—Information Theory 43
Putting Computers to Work—Algorithms 51
Battleships—Searching Algorithms 53
Lightest and Heaviest—Sorting Algorithms 72
Beat the Clock—Sorting Networks 80
The Muddy City—Minimal Spanning Trees 87
The Orange Game—Routing and Deadlock in Networks 93
Tablets of Stone—Network Communication Protocols 97
Telling Computers What To Do—Representing Procedures 105
Treasure Hunt—Finite-State Automata 107
Marching Orders—Programming Languages 123
Really hard problems—Intractability 129
The poor cartographer—Graph coloring 132
Tourist town—Dominating sets 146
Ice roads —Steiner trees 155
Sharing secrets and fighting crime-Cryptography 167
iv Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Sharing secrets—Information hiding protocols 172
The Peruvian coin flip—Cryptographic protocols 176
Kid Krypto—Public-key encryption 188
The human face of computing-Interacting with computers 201
The chocolate factory—Human interface design 205
Conversations with computers—The Turing test 220
Licensed under Creative Commons v © 2015 Computer Science Unplugged (csunplugged.org)
vi Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Part I
Data: the raw material— Representing information
Data: The Raw Material
How can we store information in computers?
The word computer comes from the Latin computare, which means to calculate or add together, but computers today are more than just giant calculators. They can be a library, help us to write, find information for us, play music and even show movies. So how do they store all this information? Believe it or not, the computer uses only two things: zero and one!
What is the difference between data and information?
Data is the raw material, the numbers that computers work with. A computer converts its data into information (words, numbers and pictures) that you and I can understand.
How can numbers, letters, words and pictures be converted into zeros and ones?
In this section we will learn about binary numbers, how computers draw pictures, how fax machines work, what is the most efficient way to store lots of data, how we can prevent errors from happening and how we measure the amount of information we are trying to store.
2 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org) © 2015 Computer Science Unplugged (csunplugged.org)
Activity 1
Count the Dots—Binary Numbers
Summary
Data in computers is stored and transmitted as a series of zeros and ones. How can we represent words and numbers using just these two symbols?
Curriculum Links
✓ Mathematics: Number – Exploring numbers in other bases. Representing
numbers in base two. ✓ Mathematics: Algebra – Continue a sequential pattern, and describe a rule for this pattern. Patterns and relationships in powers of two.
Skills
✓ Counting ✓ Matching ✓ Sequencing
Ages
✓ 6 and up
Materials
✓ You will need to make a set of five binary cards (see page 7) for the
demonstration. A4 cards with smiley face sticker dots work well.
Each student will need: ✓ A set of five cards.
Copy Photocopy Master: Binary numbers (page 7) onto card and cut out. ✓ Worksheet Activity: Binary numbers (page 6)
There are optional extension activities, for which each student will need: ✓ Worksheet Activity: Working with binary (page 8) ✓ Worksheet Activity: Sending secret messages (page 9) ✓ Worksheet Activity: Email and modems (page 10) ✓ Worksheet Activity: Counting higher than 31 (page 11) ✓ Worksheet Activity: More on binary numbers (page 12)
Licensed under Creative Commons 3 © 2015 Computer Science Unplugged (csunplugged.org)
Binary Numbers
Introduction
Before giving out the worksheet on page 6, it can be helpful to demonstrate the principles to the whole group.
For this activity, you will need a set of five cards, as shown below, with dots on one side and nothing on the other. Choose five students to hold the demonstration cards at the front of the class. The cards should be in the following order:
Discussion
As you give out the cards (from right to left), see if the students can guess how many dots are on the next card. What do you notice about the number of dots on the cards? (Each card has twice as many as the card to its right.)
How many dots would the next card have if we carried on to the left? (32) The next...? (64)
We can use these cards to make numbers by turning some of them face down and adding up the dots that are showing. Ask the students to show 6 dots (4-dot and 2-dot cards), then 15 (8-, 4-, 2- and 1-dot cards), then 21 (16, 4 and 1)... The only rule is that a card has to be completely visible, or completely hidden.
What is the smallest number of dots possible? (They may answer one, but it’s zero).
Now try counting from zero onwards.
The rest of the class needs to look closely at how the cards change to see if they can see a pattern in how the cards flip (each card flips half as often as the one to its right). You may like to try this with more than one group.
When a binary number card is not showing, it is represented by a zero. When it is showing, it is represented by a one. This is the binary number system.
4 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org) © 2015 Computer Science Unplugged (csunplugged.org)
Ask the students to make 01001. What number is this in decimal? (9) What would 17 be in binary? (10001)
Try a few more until they understand the concept.
There are five optional follow-up extension activities, to be used for reinforcement. The students should do as many of them as they can.
Licensed under Creative Commons 5 © 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: Binary Numbers
Learning how to count
So, you thought you knew how to count? Well, here is a new way to do it!
Did you know that computers use only zero and one? Everything that you see or hear on the computer—words, pictures, numbers, movies and even sound is stored using just those two numbers! These activities will teach you how to send secret messages to your friends using exactly the same method as a computer.
Instructions
Cut out the cards on your sheet and lay them out with the 16-dot card on the left as shown here:
Make sure the cards are placed in exactly the same order.
Now flip the cards so exactly 5 dots show—keep your cards in the same order!
Find out how to get 3, 12, 19. Is there more than one way to get any number? What is the biggest number you can make? What is the smallest? Is there any number you can’t make between the smallest and biggest numbers?
Extra for Experts: Try making the numbers 1, 2, 3, 4 in order. Can you work out a logical and reliable method of flipping the cards to increase any number by one?
6 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org) © 2015 Computer Science Unplugged (csunplugged.org)
Photocopy Master: Binary Numbers
Licensed under Creative Commons 7 © 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: Working With Binary
The binary system uses zero and one to represent whether a card is face up or not. 0 shows that a card is hidden, and 1 means that you can see the dots. For example:
Can you work out what 10101 is? What about 11111?
What day of the month were you born? Write it in binary. Find out what your friend’s birthdays are in binary.
Try to work out these coded numbers:
Extra for Experts: Using a set of rods of length 1, 2, 4, 8 and 16 units show how you can make any length up to 31 units. Or you could surprise an adult and show them how they only need a balance scale and a few weights to be able to weigh those heavy things like suitcases or boxes!
8 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org) © 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: Sending Secret Messages
Tom is trapped on the top floor of a department store. It’s just before Christmas and he wants to get home with his presents. What can he do? He has tried calling, even yelling, but there is no one around. Across the street he can see some computer person still working away late into the night. How could he attract her attention? Tom looks around to see what he could use. Then he has a brilliant idea—he can use the Christmas tree lights to send her a message! He finds all the lights and plugs them in so he can turn them on and off. He uses a simple binary code, which he knows the woman across the street is sure to understand. Can you work it out?
1 2 3 4 5 6 7 8 9 10 11 12 13 a b c d e f g h i j k l m
14 15 16 17 18 19 20 21 22 23 24 25 26 n o p q r s t u v w x y z
Licensed under Creative Commons 9 © 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: E-mail and Modems
Computers connected to the internet through a modem also use the binary system to send messages. The only difference is that they use beeps. A high- pitched beep can be used for a one and a low-pitched beep for a zero. These tones go very fast—so fast, in fact, that all we can hear is a horrible continuous screeching sound. If you have never heard it, listen to a modem connecting to the Internet, or try calling a fax machine—fax machines also use modems to send information.
Using the same code that Tom used in the department store, try sending an e-mail message to your friend. Make it easy for yourself and your friend though—you don’t have to be as fast as a real modem!
10 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: Counting higher than 31
Look at the binary cards again. If you were going to make the next card in the sequence, how many dots would it have? What about the next card after that? What is the rule that you are following to make your new cards? As you can see, only a few cards are needed to count up to very big numbers.
If you look at the sequence carefully, you can find a very interesting relationship:
1, 2, 4, 8, 16...
Try adding: 1 + 2 + 4 = ? What does it come to?
Now try 1 + 2 + 4 + 8 = ?
What happens if you add all the numbers up from the beginning?
Have you ever heard of “let your fingers do the walking”? Well now you can let your fingers do the counting, but you can get much higher than ten—no, you don’t have to be an alien! If you use the binary system and let each finger on one hand represent one of the cards with dots you can count from 0–31. That’s 32 numbers. (Don’t forget that zero is a number too!)
Try counting in order using your fingers. If a finger is up it is a one, and if it is down it is a zero.
You can actually get from 0–1023 if you use both hands! That’s 1024 numbers!
If you had really bendy toes (now you would have to be an alien) you could get even higher. If one hand can be used to count 32 numbers, and two hands can count to 32 × 32 = 1024 numbers, what is the biggest number Miss Flexi-Toes can reach?
Licensed under Creative Commons 11 © 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: More on Binary Numbers
1. Another interesting property of binary numbers is what happens when a zero
is put on the right hand side of the number. If we are working in base 10 (decimal), when you put a zero on the right hand side of the number, it is multiplied by 10. For example, 9 becomes 90, 30 becomes 300.
But what happens when you put a 0 on the right of a binary number? Try this:
1001 → 10010
(9) (?)
Make up some others to test your hypothesis. What is the rule? Why do you think this happens?
2. Each of the cards we have used so far represents a ‘bit’ on the computer (‘bit’
is short for ‘binary digit’). So our alphabet code we have used so far can be represented using just five cards, or ‘bits’. However a computer has to know whether letters are capitals or not, and also recognise digits, punctuation and special symbols such as $ or ~.
Go and look at a keyboard and work out how many characters a computer has to represent. So how many bits does a computer need to store all the characters?
Most computers today use a representation called ASCII (American Standard Code for Information Interchange), which is based on using this number of bits per character, but some non-English speaking countries have to use longer codes.
12 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
What’s it all about?
Computers today use the binary system to represent information. It is called binary because only two different digits are used. It is also known as base two (humans normally use base 10). Each zero or one is called a bit (binary digit). A bit is usually represented in a computer’s main memory by a transistor that is switched on or off, or a capacitor that is charged or discharged.
When data must be transmitted over a telephone line or radio link, high and low-pitched tones are used for the ones and zeros. On magnetic disks (hard disks and floppy disks) and tapes, bits are represented by the direction of a magnetic field on a coated surface, either North-South or South-North.
Audio CDs, CD-ROMs and DVDs store bits optically—the part of the surface corresponding to a bit either does or does not reflect light.
The reason that computers only use two different values is that it’s much easier to build devices that do it this way. We could have had CDs that have 10 levels of reflection so that we could represent all the digits from 0 to 9, but you have to build very expensive and precise devices to make it work. The other thing you may have noticed is that although we say that computers only store zeroes and ones, the actually don’t have zeroes and ones inside them – just high and low voltages, or north/south magnetism, and so on. But it’s quicker to write “0” and “1” than things like “shiny” and “not shiny”. Everything on computers is represented using these bits – documents, pictures, songs, videos, numbers, and even the programs and apps that we use are just a whole lot of binary digits.
One bit on its own can’t represent much, so they are usually grouped together in groups of eight, which can represent numbers from 0 to 255. A group of eight bits is called a byte.
The speed of a computer depends on the number of bits it can process at once. For example, a 32-bit computer can process 32-bit numbers in one operation, while a 16-bit computer must break 32-bit numbers down into smaller pieces, making it slower (but cheaper!)
In some of the later activities we will see how other kinds of information can be represented on a computer using binary digits.
14 Licensed under Creative Commons
Licensed under Creative Commons 13 © 2015 Computer Science Unplugged (csunplugged.org)
© 2015 Computer Science Unplugged (csunplugged.org)
Solutions and hints
Binary Numbers (page 6)
3 requires cards 2 and 1 12 requires cards 8 and 4 19 requires cards 16, 2 and 1
There is only one way to make any number.
The biggest number you can make is 31. The smallest is 0. You can make every number in between, and each has a unique representation.
Experts: To increase any number by one, flip all the cards from right to left until you turn one face up.
Working with binary (page 8)
10101 = 21, 11111 = 31
Sending Secret Messages (page 9)
Coded message: HELP IM TRAPPED
Counting higher than 31 (page 11)
If you add the numbers up from the beginning the sum will always be one less than the next number in the sequence.
Miss Flexi-toes can count 1024 × 1024 = 1,048,576 numbers—from 0 to 1,048,575!
More on Binary Numbers (page 12)
When you put a zero on the right hand side of a binary number the number doubles.
All of the places containing a one are now worth twice their previous value, and so the total number doubles. (In base 10 adding a zero to the right multiplies it by 10.)
A computer needs 7 bits to store all the characters. This allows for up to 128 characters. Usually the 7 bits are stored in an 8-bit byte, with one bit wasted.
Licensed under Creative Commons 15 © 2015 Computer Science Unplugged (csunplugged.org)
Activity 2
Colour by Numbers—Image Representation
Summary
Computers store drawings, photographs and other pictures using only numbers. The following activity demonstrates how they can do this.
Curriculum Links
✓ Mathematics: Geometry – Shapes and Spaces ✓ Technology: using whole numbers to represent other kinds of data ✓ Technology: reducing the space used by repetitive data
Skills
✓ Counting ✓ Graphing
Ages
✓ 7 and up
Materials
✓ Slide for presenting: Colour by numbers (page 19)
Each student will need: ✓ Worksheet Activity: Kid Fax (page 20) ✓ Worksheet Activity: Make your own picture (page 21)
16 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Colour by Numbers
Introduction
Discussion Questions
1. What do facsimile (fax) machines do?
2. In what situations would computers need to store pictures? (A drawing
program, a game with graphics, or a multi-media system.)
3. How can computers store pictures when they can only use numbers?
(You may like to arrange for the students to send and/or receive faxes as a preparation for this activity)
Demonstration using projection
Computer screens are divided up into a grid of small dots called pixels (picture elements).
In a black and white picture, each pixel is either black or white.
The letter “a” has been magnified above to show the pixels. When a computer stores a picture, all that it needs to store is which dots are black and which are white.
1, 3, 1 4, 1 1, 4 0, 1, 3, 1 0, 1, 3, 1 1, 4
The picture above shows us how a picture can be represented by numbers. The first line consists of one white pixel, then three black, then one white. Thus the first line is represented as 1, 3, 1.
The first number always relates to the number of white pixels. If the first pixel is black the line will begin with a zero.
The worksheet on page 20 gives some pictures that the students can decode using the method just demonstrated.
18 Licensed under Creative Commons
Licensed under Creative Commons 17 © 2015 Computer Science Unplugged (csunplugged.org)
© 2015 Computer Science Unplugged (csunplugged.org)
Colour by numbers
A letter “a” from a computer screen and a magnified view showing the pixels that make up the image
1, 3, 1 4, 1 1, 4 0, 1, 3, 1 0, 1, 3, 1 1, 4
The same image coded using numbers
Blank grid (for teaching purposes)
Licensed under Creative Commons 19 © 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: Kid Fax
The first picture is the easiest and the last one is the most complex. It is easy to make mistakes and therefore a good idea to use a pencil to colour with and have a rubber handy!
4, 11 4, 9, 2, 1 4, 9, 2, 1 4, 11 4, 9 4, 9 5, 7 0, 17 1, 15
6, 5, 2, 3 4, 2, 5, 2, 3, 1 3, 1, 9, 1, 2, 1 3, 1, 9, 1, 1, 1 2, 1, 11, 1 2, 1, 10, 2 2, 1, 9, 1, 1, 1 2, 1, 8, 1, 2, 1 2, 1, 7, 1, 3, 1 1, 1, 1, 1, 4, 2, 3, 1 0, 1, 2, 1, 2, 2, 5, 1 0, 1, 3, 2, 5, 2 1, 3, 2, 5
6, 2, 2, 2 5, 1, 2, 2, 2, 1 6, 6 4, 2, 6, 2 3, 1, 10, 1 2, 1, 12, 1 2, 1, 3, 1, 4, 1, 3, 1 1, 2, 12, 2 0, 1, 16, 1 0, 1, 6, 1, 2, 1, 6, 1 0, 1, 7, 2, 7, 1 1, 1, 14, 1 2, 1, 12, 1 2, 1, 5, 2, 5, 1 3, 1, 10, 1 4, 2, 6, 2 6, 6
20 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: Make Your Own Picture
Now that you know how numbers can represent pictures, why not try making your own coded picture for a friend? Draw your picture on the top grid, and when you’ve finished, write the code numbers beside the bottom grid. Cut along the dotted line and give the bottom grid to a friend to colour in. (Note: you don’t have to use the whole grid if you don’t want to—just leave some blank lines at the bottom if your picture doesn’t take up the whole grid.)
✁
Licensed under Creative Commons 21 © 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: Make Your Own Picture
Extra for Experts: If you want to produce coloured images you can use a number to represent the colour (e.g. 0 is black, 1 is red, 2 is green etc.) Two numbers are now used to represent a run of pixels: the first gives the length of the run as before, and the second specifies the colour. Try making a coloured picture for a friend. Don’t forget to let your friend know which number stands for which colour!
✁
22 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Variations and Extensions
1. Try drawing with a sheet of tracing paper on top of the grid, so that the
final image can be viewed without the grid. The image will be clearer.
2. Instead of colouring the grid the students could use squares of sticky
paper, or place objects, on a larger grid.
Discussion Point
There is usually a limit to the length of a run of pixels because the length is being represented as a binary number. How would you represent a run of twelve black pixels if you could only use numbers up to seven? (A good way is to code a run of seven black pixels, followed by a run of zero white, then a run of five black.)
Licensed under Creative Commons 23 © 2015 Computer Science Unplugged (csunplugged.org)
What’s it all about?
A fax machine is really just a simple computer that scans a black and white page into about 1000 × 2000 pixels, which are sent using a modem to another fax machine, which prints the pixels out on a page. Often fax images have large blocks of white (e.g. margins) or black pixels (e.g. a horizontal line). Colour pictures also have a lot of repetition in them. To save on the amount of storage space needed to keep such images programmers can use a variety of compression techniques. The method used in this activity is called ‘run- length coding’, and is an effective way to compress images. If we didn't compress images it would take much longer to transmit pictures and require much more storage space. This would make it infeasible to send faxes or put photos on a web page. For example, fax images are generally compressed to about a seventh of their original size. Without compression they would take seven times as long to transmit!
Photographs and pictures are often compressed to a tenth or even a hundredth of their original size (using a related techniques such as JPEG, GIF and PNG). This allows many more images to be stored on a disk, and it means that viewing them over the web will take a fraction of the time.
A programmer can choose which compression technique best suits the images he or she is transmitting.
24 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Solutions and hints
Answers to Kid Fax Worksheet
Licensed under Creative Commons 25 © 2015 Computer Science Unplugged (csunplugged.org)
Activity 3
You Can Say That Again! —Text Compression
Summary
Since computers only have a limited amount of space to hold information, they need to represent information as efficiently as possible. This is called compression. By coding data before it is stored, and decoding it when it is retrieved, the computer can store more data, or send it faster through the Internet.
Curriculum Links
✓ English: Recognising patterns in words and text. ✓ Technology: reducing the space used by repetitive data
Skills
✓ Copying written text
Ages
✓ 9 and up
Materials
✓ Presentation Slide: You can say that again! (page 28)
Each student will need: ✓ Worksheet Activity: You can say that again! (page 29) ✓ Worksheet Activity: Extras for experts (page 30) ✓ Worksheet Activity: Short and sweet (page 31) ✓ Worksheet Activity: Extras for real experts (page 33)
26 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
You can say that again!
Introduction
Computers have to store and transmit a lot of data. So that they don’t have to use up too much storage space, or take too long to send information through a network connection, they compress the text a bit like this.
Demonstration and Discussion
Show “The Rain” slide (page 28). Look for the patterns of letters in this poem. Can you find groups of 2 or more letters that are repeated, or even whole words or phrases? (Replace these with boxes as shown in the diagram below.)
Licensed under Creative Commons 27 © 2015 Computer Science Unplugged (csunplugged.org)
You Can Say That Again!
The Rain
Pitter patter
Pitter patter
Listen to the rain
Pitter patter
Pitter patter
On the window pane
28 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: You can say that again!
Many of the words and letters are missing in this poem. Can you fill in the missing letters and words to complete it correctly? You will find these in the box that the arrow is pointing to.
Now choose a simple poem or nursery rhyme and design your own puzzle. Make sure your arrows always point to an earlier part of the text. Your poem should be able to be decoded from left to right and from top to bottom in the same way we read.
Challenge: See how few of the original words you need to keep!
Here are some suggestions: Three Blind Mice, Mary Mary Quite Contrary, Hickory Dickory Dock—or try some Dr Seuss books!
Hint: Try to avoid overcrowding of arrows. Leave a lot of space around letters and words as you write them so that you have room for the boxes within boxes and the arrows pointing to them.
It is easier to design the puzzle if you write out the poem first and then decide where the boxes need to be.
Licensed under Creative Commons 29 © 2015 Computer Science Unplugged (csunplugged.org)
30 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: Extra for Experts
How would you solve this puzzle? Ban---
Sometimes missing text points to part of itself. In this case it can be decoded correctly if the letters are copied from left to right. Then each letter is available to be copied before it is needed. This is useful in computers if there is a long run of a particular character or pattern.
Try drawing some of your own.
On computers the boxes and arrows are represented by numbers. For example,
Banana
can be written as Ban(2,3). “2” means count back two characters to find the starting point for copying,
Ban---
and “3” means copy three consecutive characters:
Bana-- Banan- Banana- As two numbers are used to code these words, usually only groups of two or more letters are worth compressing, otherwise there is no saving of space. In fact the size of the file could go up if two numbers are used to code one letter.
Make up some words of your own written in the way a computer would if they were compressed. Can your friends decode them?
Worksheet Activity: Short and Sweet
How many words do you really need here?
Pretend you are a computer trying to fit as much into your disk as possible. Cross out all the groups of two or more letters that have already occurred. These are no longer needed as they could be replaced by a pointer. Your goal is to get as many letters crossed out as possible.
Licensed under Creative Commons 31 © 2015 Computer Science Unplugged (csunplugged.org)
32 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: Extra for Real Experts
Ready for some really tough compression?
The following story was run through a computer program, which found that there are at least 1,633 letters that can be crossed out. How many can you find? Remember, only groups of two or more repeated characters can be eliminated. Good luck! O
nce little upon pig wasn’t a time, very long, clever, long and ago, decided three little to build pigs set his out house to make out of their straw, fortunes. because The it was
first
cheap. The second little pig wasn’t very clever either, and decided to build his house out of sticks, for the “natural” look that was so very much in fashion, even in those days. The third little pig was much smarter than his two brothers, and bought a load of bricks in a nearby town, with which to construct a sturdy but comfortable country home.
Not long after his housewarming party, the first little pig was curled up in a chair reading a book, when there came a knock at the door. It was the big bad wolf, naturally.
“Little pig, little pig, let me come in!” cried the wolf.
“Not by the hair on my chinny-chin-chin!” squealed the first little pig.
“Then I’ll huff, and I’ll puff, and I’ll blow your house down!” roared the wolf, and he did huff, and he did puff, and the house soon collapsed. The first little pig ran as fast as he could to the house of sticks, and was soon safe inside. But it wasn’t long before the wolf came calling again.
“Little pig, little pig, let me come in!” cried the wolf.
“Not by the hair on my chinny-chin-chin!” squealed the second little pig.
“Then I’ll huff, and I’ll puff, and I’ll blow your house down!” roared the wolf, and he did huff, and he did puff, and the house was soon so much firewood. The two terrified little pigs ran all the way to their brother’s brick house, but the wolf was hot on their heels, and soon he was on the doorstep.
“Little pig, little pig, let me come in!” cried the wolf.
“Not by the hair on my chinny-chin-chin!” squealed the third little pig.
“Then I’ll huff, and I’ll puff, and I’ll blow your house down!” roared the wolf, and he huffed, and he puffed, and he huffed some more, but of course, the house was built of brick, and the wolf was soon out of breath. Then he had an idea. The chimney! He clambered up a handy oak tree onto the roof, only to find that there was no chimney, because the third little pig, being conscious of the environment, had installed electric heating. In his frustration, the wolf slipped and fell off the roof, breaking his left leg, and severely injuring his pride. As he limped away, the pigs laughed, and remarked how much more sensible it was to live in the city, where the only wolves were in the zoo. And so that is what they did, and of course they all lived happily ever after.
Licensed under Creative Commons 33 © 2015 Computer Science Unplugged (csunplugged.org)
What’s it all about?
The storage capacity of computers is growing at an unbelievable rate—in the last 25 years, the amount of storage provided on a typical computer has grown about a millionfold—but we still find more to put into our computers. Computers can store whole books or even libraries, and now music and movies too, if only they have the room. Large files are also a problem on the Internet, because they take a long time to download. We also try to make computers smaller—even a cellphone or wristwatch can be expected to store lots of information!
There is a solution to this problem, however. Instead of buying more storage space, or a faster network connection, we can compress the data so that it takes up less space. This process of compressing and decompressing the data is normally done automatically by the computer. All we might notice is that the disk holds more, or that web pages display faster, but the computer is actually doing more processing.
Many methods of compression have been invented. The method used in this activity, with the principle of pointing to earlier occurrences of chunks of text, is often referred to as ‘Ziv-Lempel coding,’ or ‘LZ coding’, invented by two Israeli professors in the 1970s. It can be used for any language and can easily halve the size of the data being compressed. It is sometimes referred to as ‘zip’ on personal computers, and is also used for ‘GIF’ and ‘PNG’ images, and has been used in high-speed modems. In the case of modems, it reduces the amount of data that needs to be transmitted over the phone line, so it goes much faster.
Some other methods are based on the idea that letters that are used more often should have shorter codes than the others. Morse code used this idea.
Solutions and hints
You can say that again! (page 29)
Pease porridge hot, Pease porridge cold, Pease porridge in the pot, Nine days old.
Some like it hot, Some like it cold, Some like it in the pot, Nine days old.
34 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Activity 4
Card Flip Magic—Error Detection & Correction
Summary
When data is stored on a disk or transmitted from one computer to another, we usually assume that it doesn’t get changed in the process. But sometimes things go wrong and the data is changed accidentally. This activity uses a magic trick to show how to detect when data has been corrupted, and to correct it.
Curriculum Links
✓ Mathematics: Number – Exploring computation and estimation. ✓ Mathematics: Algebra – Exploring patterns and relationships, solving for a
missing value. ✓ Mathematics: Rows and columns, coordinates ✓ Technology: Validating data
Skills
✓ Counting ✓ Recognition of odd and even numbers
Ages
✓ 7 years and up
Materials
✓ A set of 36 “fridge magnet” cards, coloured on one side only ✓ A metal board (a whiteboard works well) for the demonstration.
Each pair of students will need: ✓ 36 identical cards, coloured on one side only.
Licensed under Creative Commons 35 © 2015 Computer Science Unplugged (csunplugged.org)
The “Magic Trick”
Demonstration
Here’s your chance to be a magician!
You will need a pile of identical, two-sided cards. (To make your own cut up a large sheet of card that is coloured on one side only). For the demonstration it is easiest to use flat magnetic cards that have a different colour on each side—fridge magnets are ideal, but make sure they are magnetic on both sides (many are single sided, in which case you can glue them face to face, and put a white dot on one side).
1. Choose a student to lay out the cards in a 5 × 5 square, with a
random mixture of sides showing.
Casually add another row and column, “just to make it a bit harder”.
These cards are the key to the trick. You must choose the extra cards to ensure that there is an even number of coloured cards in each row and column.
2. Get a student to flip over one card only while you cover your eyes.
The row and column containing the changed card will now have an odd number of coloured cards, and this will identify the changed card. Can the students guess how the trick is done?
36 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Teach the trick to the students:
1. Working in pairs, the students lay out their cards 5 × 5.
2. How many coloured cards are there in each row and column? Is it an odd or
even number? Remember, 0 is an even number.
3. Now add a sixth card to each row, making sure the number of
coloured cards is always even. This extra card is called a “parity” card.
4. Add a sixth row of cards along the bottom, to make the number of
cards in each column an even number.
5. Now flip a card. What do you notice about the row and column? (
They will have an odd number of coloured cards.) Parity cards are used to show you when a mistake has been made.
6. Now take turns to perform the ‘trick’.
Extension Activities:
1. Try using other objects. Anything that has two ‘states’ is suitable. For example,
you could use playing cards, coins (heads or tails) or cards with 0 or 1 printed on them (to relate to the binary system).
2. What happens if two, or more, cards are flipped? (It is not always possible to know exactly which two cards were flipped, although it is possible to tell that something has been changed. You can usually narrow it down to one of two pairs of cards. With 4 flips it is possible that all the parity bits will be correct afterwards, and so the error could go undetected.)
3. Try this with a much larger layout e.g. 9 × 9 cards, with the extra row and
column expanding it to 10 × 10. (It will work for any size layout, and doesn’t have to be square).
4. Another interesting exercise is to consider the lower right-hand card. If you choose it to be the correct one for the column above, then will it be correct for the row to its left? (The answer is yes, always, if you use even parity.)
5. In this card exercise we have used even parity—using an even number of
coloured cards. Can we do it with odd parity? (This is possible, but the lower right-hand card only works out the same for its row and column if the numbers of rows and columns are both even or both odd. For example, a 5 × 9 layout will work fine, or a 4 × 6, but a 3 × 4 layout won’t.)
Licensed under Creative Commons 37 © 2015 Computer Science Unplugged (csunplugged.org)
A Real-Life Example for Experts!
This same checking technique is used with book codes and bar codes. Published books have a ten- or 13-digit code usually found on the back cover. The last digit is a check digit, just like the parity bits in the exercise.
This means that if you order a book using its ISBN (International Standard Book Number), the website can check that you haven’t made a mistake. They simply look at the checksum. That way you don’t end up waiting for the wrong book!
Here’s how to work out the checksum for a 10-digit book code:
Multiply the first digit by ten, the second by nine, the third by eight, and so on, down to the ninth digit multiplied by two. Each of these values is then added together.
For example, the ISBN 0-13-911991-4 gives a value
(0 × 10) + (1 × 9) + (3 × 8) + (9 × 7) + (1 × 6) + (1 × 5) + (9 × 4) + (9 × 3) + (1 × 2) = 172
Then divide your answer by eleven. What is the remainder?
172 ÷ 11 = 15 remainder 7
If the remainder is zero, then the checksum is zero, otherwise subtract the remainder from 11 to get the checksum.
11 – 7 = 4
Look back. Is this the last digit of the ISBN? Yes!
If the last digit of the ISBN wasn’t a four, then we would know that a mistake had been made.
It is possible to come up with a checksum of the value of 10, which would require more than one digit. When this happens, the character X is used.
A barcode (UPC) from a box of Weet-BixTM
38 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Another example of the use of a check digit is the bar codes on grocery items. This uses a different formula (the same formula is used for 13-digit book codes). If a bar code is misread the final digit should be different from its calculated value. When this happens the scanner beeps and the checkout operator re-scans the code. Check digits are also used for bank account numbers, social security numbers, tax numbers, numbers on trains and rolling stock, and many other applications where people are copying a number and need some assurance that it has been typed in correctly.
Licensed under Creative Commons 39 © 2015 Computer Science Unplugged (csunplugged.org)
Check that book!
Detective Blockbuster
Book Tracking Service, Inc.
We find and check ISBN checksums for a small fee.
Join our agency—look in your classroom or library for real ISBN codes.
Are their checksums correct?
Sometimes errors are made.
Some of the common errors are:
✘ a digit has its value changed; ✘ two adjacent digits are swapped with each other; ✘ a digit is inserted in the number; and ✘ a digit is removed from the number
Can you find a book with the letter X for a checksum of 10? It shouldn’t be too hard to find—one in every 11 should have it.
What sort of errors might occur that wouldn’t be detected? Can you change a digit and still get the correct checksum? What if two digits are swapped (a common typing error)?
40 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Licensed under Creative Commons 41 © 2015 Computer Science Unplugged (csunplugged.org)
What’s it all about?
Imagine you are depositing $10 cash into your bank account. The teller types in the amount of the deposit, and it is sent to a central computer. But suppose some interference occurs on the line while the amount is being sent, and the code for $10 is changed to $1,000. No problem if you are the customer, but clearly a problem for the bank!
It is important to detect errors in transmitted data. So a receiving computer needs to check that the data coming to it has not been corrupted by some sort of electrical interference on the line. Sometimes the original data can be sent again when an error has been transmitted, but there are some situations when this is not feasible, for example if a disk has been corrupted by exposure to magnetic or electrical radiation, by heat or by physical damage. If data is received from a deep space probe, it would be very tedious to wait for retransmission if an error had occurred! (It takes just over half an hour to get a radio signal from Jupiter when it is at its closest to Earth!)
We need to be able to recognize when the data has been corrupted (error detection) and to be able to reconstruct the original data (error correction).
The same technique as was used in the “card flip” game is used on computers. By putting the bits into imaginary rows and columns, and adding parity bits to each row and column, we can not only detect if an error has occurred, but where it has occurred. The offending bit is changed back, and so we have performed error correction.
Of course computers often use more complex error control systems that are able to detect and correct multiple errors. The hard disk in a computer has a large amount of its space allocated to correcting errors so that it will work reliably even if parts of the disk fail. The systems used for this are closely related to the parity scheme.
And to finish, a joke that is better appreciated after doing this activity:
Q: What do you call this: “Pieces of nine, pieces of nine”?
A: A parroty error.
Solutions and hints
Errors that would not be detected by an ISBN-10 checksum are those where one digit increases and another decreases to compensate. Then the sum might still be the same. However, because of the way the calculation is done, this is unlikely to happen. In other systems (such as ISBN-13) there are other types of errors that might not be detected, such as three consecutive digits being reversed, but most of the common errors (typing one digit incorrectly, or swapping two adjacent digits) will be detected.
42 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Activity 5
Twenty Guesses—Information Theory
Summary
How much information is there in a 1000-page book? Is there more information in a 1000-page telephone book, or in a ream of 1000 sheets of blank paper, or in Tolkien’s Lord of the Rings? If we can measure this, we can estimate how much space is needed to store the information. For example, can you still read the following sentence?
Ths sntnc hs th vwls mssng.
You probably can, because there is not much ‘information’ in the vowels. This activity introduces a way of measuring information content.
Curriculum links
✓ Mathematics: Number – Exploring number: Greater than, less than,
ranges. ✓ Mathematics: Algebra – Patterns and sequences ✓ English: spelling, recognising elements of text
Skills
✓ Comparing numbers and working with ranges of numbers ✓ Deduction ✓ Asking questions
Ages
✓ 10 and up
Materials
✓ No materials are required for the first activity
There is an extension activity, for which each student will need: ✓ Worksheet Activity: Decision trees (page 47)
Licensed under Creative Commons 43 © 2015 Computer Science Unplugged (csunplugged.org)
Twenty Guesses
Discussion
1. Discuss with the students what they think information is.
2. How could we measure how much information there would be in a book? Is
the number of pages or number of words important? Can one book have more information than another? What if it is a very boring book, or a particularly interesting one? Would 400 pages of a book containing the phrase “blah, blah, blah” have more or less information than, say, the telephone directory?
3. Explain that computer scientists measure information by how surprising a
message (or book!) is. Telling you something that you know already—for example, when a friend who always walks to school says “I walked to school today”—doesn’t give you any information, because it isn’t surprising. If your friend said instead, “I got a ride to school in a helicopter today,” that would be surprising, and would therefore tell us a lot of information.
4. How can the surprise value of a message be measured?
5. One way is to see how hard it is to guess the information. If your friend says, “Guess how I got to school today,” and they had walked, you would probably guess right first time. It might take a few more guesses before you got to a helicopter, and even more if they had travelled by spaceship.
6. The amount of information that messages contain is measured by how easy
or hard they are to guess. The following game gives us some idea of this.
44 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Twenty Questions Activity
This is an adapted game of 20 questions. Students may ask questions of a chosen student, who may only answer yes or no until the answer has been guessed. Any question may be asked, provided that the answer is strictly ‘yes’ or ‘no’.
Suggestions:
I am thinking of: ✓ a number between 1 and 100 ✓ a number between 1 and 1000 ✓ a number between 1 and 1,000,000. ✓ any whole number ✓ a sequence of 6 numbers in a pattern (appropriate to the group). Guess in
order from first to last. (e.g. 2, 4, 6, 8, 10)
Count the number of questions that were asked. This is a measure of the value of the “information”.
Follow-up Discussion
What strategies did you use? Which were the best ones?
Point out that it takes just 7 guesses to find a number between 1 and 100 if you halve the range each time. For example:
Is it less than 50? Yes. Is it less than 25? No. Is it less than 37? No. Is it less than 43? Yes. Is it less than 40? No. Is it less than 41? No. It must be 42! Yes!
Interestingly if the range is increased to 1000 it doesn’t take 10 times the effort—just three more questions are needed. Every time the range doubles you just need one more question to find the answer.
A good follow up would be to let the students play Mastermind.
Extension: How much information is there in a message?
Computer scientists don’t just use guessing with numbers—they can also guess which letter is more likely to be next in a word or sentence.
Try the guessing game with a short sentence of 4–6 words. The letters must be guessed in the correct order, from first to last. Get someone to write down the letters as they are found and keep a record of how many guesses it takes
to find each letter. Any questions with a yes/no answer can be used. Examples would be, “It it a t?” “Is it a vowel?” “Does it come before m in the alphabet?” A space between words also counts as a “letter” and must be guessed. Take turns and see if you can discover which parts of messages are easiest to find out.
46 Licensed under Creative Commons
Licensed under Creative Commons 45 © 2015 Computer Science Unplugged (csunplugged.org)
© 2015 Computer Science Unplugged (csunplugged.org)
Worksheet Activity: Decision Trees
If you already know the strategy for asking the questions, you can transmit a message without having to ask anything.
Here is a chart called a ‘decision tree’ for guessing a number between 0 and 7: What are the yes/no decisions needed to ‘guess’ the number 5?
How many yes/no decisions do you need to make to work out any number?
Now look at something very fascinating. Underneath the numbers 0, 1, 2, 3... in the final row of the tree write the number in binary (see Activity 1).
Look closely at the tree. If no=0 and yes=1, what do you see?
In the number guessing game we try to choose questions so that the sequence of answers works out to represent the number in exactly this way.
Design your own decision tree for guessing numbers between 0 and 15.
Extra for experts: What kind of tree would you use to guess someone’s age? What about a tree to guess which letter is next in a sentence?
Licensed under Creative Commons 47 © 2015 Computer Science Unplugged (csunplugged.org)
48 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
What’s it all about?
A celebrated American mathematician (and juggler, and unicyclist) called Claude Shannon did a lot of experiments with this game. He measured the amount of information in bits—each yes/no answer is equivalent to a 1/0 bit. He found that the amount of “information” contained in a message depends on what you already know. Sometimes we can ask a question that eliminates the need to ask a lot of other questions. In this case the information content of the message is low. For example, the information in a single toss of a coin is normally one bit: heads or tails. But if the coin happens to be a biased one that turns up heads nine times out of ten, then the information is no longer one bit—believe it or not, it’s less. How can you find out what a coin toss was with less than one yes/no question? Simple—just use questions like “are the next two coin tosses both heads?” For a sequence of tosses with the biased coin, the answer to this will be “yes” about 80%, of the time. On the 20% of occasions where the answer is “no,” you will have to ask two further questions. But on average you will be asking less than one question per coin toss!
Shannon called the information content of a message “entropy”. Entropy depends not only on the number of possible outcomes—in the case of a coin toss, two—but also on the probability of it happening. Improbable events, or surprising information, need a lot more questions to guess the message because they tell us more information we didn’t already know—just like the situation of taking a helicopter to school.
The entropy of a message is very important to computer scientists. You cannot compress a message to occupy less space than its entropy, and the best compression systems are equivalent to a guessing game. Since a computer program is making the ‘guesses’, the list of questions can be reproduced later, so as long as the answers (bits) are stored, we can reconstruct the information! The best compression systems can reduce text files to about a quarter of their original size—a big saving on storage space!
The guessing method can also be used to build a computer interface that predicts what the user is going to type next! This can be very useful for physically disabled people who find it difficult to type. The computer suggests what it thinks they are likely to type next, and they just indicate what they want. A good system needs an average of only two yes/no answers per
character, and can be of great assistance to someone who has difficulty making the fine movements needed to control a mouse or keyboard. This sort of system is also used in a different form to ‘type’ text on some cellphones.
Licensed under Creative Commons 49 © 2015 Computer Science Unplugged (csunplugged.org)
Solutions and hints
The answer to a single yes/no question corresponds to exactly one bit of information—whether it is a simple question like “Is it more than 50?” or a more complex one like “Is it between 20 and 60?”
In the number-guessing game, if the questions are chosen in a certain way, the sequence of answers is just the binary representation of the number. Three is 011 in binary and is represented by the answers “No, yes, yes” in the decision tree, which is the same if we write no for 0 and yes for 1.
A tree you would use for someone’s age might be biased towards smaller numbers.
The decision about the letters in a sentence might depend upon what the previous letter was.
50 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Part II
Putting Computers to Work— Algorithms
Putting Computers to Work
Computers operate by following a list of instructions set for them. These instructions enable them to sort, find and send information. To do these things as quickly as possible, you need good methods for finding things in large collections of data, and for sending information through networks.
An algorithm is a set of instructions for completing a task. The idea of an algorithm is central to computer science. Algorithms are how we get computers to solve problems. Some algorithms are faster than others, and many of the algorithms that have been discovered have made it possible to solve problems that previously took an infeasible length of time—for example, finding millions of digits in pi, or all pages that contain your name on the World-Wide Web, or finding out the best way to pack parcels into a container, or finding out whether or not very large (100-digit) numbers are prime.
The word “algorithm” is derived from the name of Mohammed ibn Musa Al- Khowarizmi—Mohammed, son of Moses, from Khowarizm—who joined an academic centre known as the House of Wisdom in Baghdad around 800AD. His works transmitted the Hindu art of reckoning to the Arabs, and thence to Europe. When they were translated into Latin in 1120AD, the first words were “Dixit Algorismi”—“thus said Algorismi”.
52 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Activity 6
Battleships—Searching Algorithms
Summary
Computers are often required to find information in large collections of data. They need to develop quick and efficient ways of doing this. This activity demonstrates three different search methods: linear searching, binary searching and hashing.
Curriculum Links
✓ Mathematics: Number – Exploring numbers: Greater than, less than and
equal to ✓ Mathematics: Geometry – Exploring shape and space: Co-ordinates ✓ Computing: Algorithms
Skills
✓ Logical reasoning
Ages
✓ 9 years and up
Materials
Each student will need: ✓ Copy of battleships games
▪ 1A, 1B for game 1
▪ 2A, 2B for game 2
▪ 3A, 3B for game 3
✓ You may also need a few copies of the supplementary game sheets, 1A',
1B', 2A', 2B', 3A', 3B'.
Licensed under Creative Commons 53 © 2015 Computer Science Unplugged (csunplugged.org)
Battleships
Introductory Activity
1. Choose about 15 students to line up at the front of the classroom. Give each
student a card with a number on it (in random order). Keep the numbers hidden from the rest of the class.
2. Give another student a container with four or five sweets in it. Their job is to
find a given number. They can “pay” to look at a particular card. If they find the correct number before using all their sweets, they get to keep the rest.
3. Repeat if you wish to.
4. Now shuffle the cards and give them out again. This time, have the students
sort themselves into ascending order. The searching process is repeated.
If the numbers are sorted, a sensible strategy is to use just one “payment” to eliminate half the students by having the middle student reveal their card. By repeating this process they should be able to find the number using only three sweets. The increased efficiency will be obvious.
Activity
The students can get a feel for how a computer searches by playing the battleship game. As they play the game, get them to think about the strategies they are using to locate the ships.
54 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Battleships—A Linear Searching Game
Read the following instructions to the students
1. Organise yourselves into pairs. One of you has sheet 1A, the other sheet 1B.
Don’t show your sheet to your partner!
2. Both of you circle one battleship on the top line of your game sheet and tell
your partner its number.
3. Now take turns to guess where your partner’s ship is. (You say the letter name of a ship and your partner tells you the number of the ship at that letter.)
4. How many shots does it take to locate your partner’s ship? This is your score
for the game.
(Sheets 1A' and 1B' are extras provided for students who would like to play more games or who “inadvertently” see their partner’s sheet. Sheets 2A', 2B' and 3A', 3B' are for the later games.)
Follow Up Discussion
1. What were the scores?
2. What would be the minimum and maximum scores possible? (They are 1 and 26 respectively, assuming that the students don’t shoot at the same ship twice. This method is called ‘linear search’, because it involves going through all the positions, one by one.)
Licensed under Creative Commons 55 © 2015 Computer Science Unplugged (csunplugged.org)
Battleships—A Binary Searching Game
Instructions
The instructions for this version of the game are the same as for the previous game but the numbers on the ships are now in ascending order. Explain this to the students before they start.
1. Organise yourselves into pairs. One of you has sheet 2A, the other sheet 2B.
Don’t show your sheet to your partner!
2. Both of you circle one battleship on the top line of your game sheet and tell
your partner its number.
3. Now take turns to guess where your partner’s ship is. (You say the letter name of a ship and your partner tells you the number of the ship at that letter.)
4. How many shots does it take to locate your partner’s ship? This is your score
for the game.
Follow Up Discussion
1. What were the scores?
2. What strategy did the low scorers use?
3. Which ship should you choose first? (The one in the middle tells you which
half of the line the chosen ship must be in.) Which location would you choose next? (Again the best strategy is always to choose the middle ship of the section that must have the selected ship.)
4. If this strategy is applied how many shots will it take to find a ship? (Five at
most).
This method is called ‘binary search’, because it divides the problem into two parts.
56 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Battleships—A Hashing Search Game
Instructions
1. Each take a sheet as in the previous games and tell your partner the number
of your chosen ship.
2. In this game you can find out which column (0 to 9) the ship is in. You simply
add together the digits of the ship’s number. The last digit of the sum is the column the ship is in. For example, to locate a ship numbered 2345, add the digits 2+3+4+5, giving 14. The last digit of the sum is 4, so that ship must be in column 4. Once you know the column you need to guess which of the ships in that column is the desired one. This technique is called ‘hashing’, because the digits are being squashed up (“hashed”) together.
3. Now play the game using this new searching strategy. You may like to play
more than one game using the same sheet—just choose from different columns.
(Note that, unlike the other games, the spare sheets 3A' and 3B' must be used as a pair, because the pattern of ships in columns must correspond.)
Follow Up Discussion
1. Collect and discuss scores as before.
2. Which ships are very quick to find? (The ones that are alone in their column.) Which ships may be harder to find? (The ones whose columns contain lots of other ships.)
3. Which of the three searching processes is fastest? Why?
What are the advantages of each of the three different ways of searching? (The second strategy is faster than the first, but the first one doesn’t require the ships to be sorted into order. The third strategy is usually faster than the other two, but it is possible, by chance, for it to be very slow. In the worst case, if all the ships end up in the same column, it is just as slow as the first strategy.)
Licensed under Creative Commons 57 © 2015 Computer Science Unplugged (csunplugged.org)
Extension Activities
1. Have the students make up their own games using the three formats. For the
second game they must put the numbers in ascending order. Ask how they might make the Hashing Game very hard. (The hardest game is when all the ships are in the same column.) How could you make it as easy as possible? (You should try to get the same number of ships into each column.)
2. What would happen if the ship being sought wasn’t there? (In the Linear
Search game it would take 26 shots to show this. In the Binary Search game you would need five shots to prove this. When using the Hash System it would depend on how many ships appeared in the relevant column.)
3. Using the Binary Search strategy how many shots would be required if there were a hundred locations (about six shots), a thousand locations (about nine), or a million (about nineteen)? (Notice that the number of shots increases very slowly compared to the number of ships. One extra shot is required each time the size doubles, so it is proportional to the logarithm of the number of ships.)
58 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Licensed under Creative Commons 59 © 2015 Computer Science Unplugged (csunplugged.org)
60 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Licensed under Creative Commons 61 © 2015 Computer Science Unplugged (csunplugged.org)
62 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Licensed under Creative Commons 63 © 2015 Computer Science Unplugged (csunplugged.org)
64 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Licensed under Creative Commons 65 © 2015 Computer Science Unplugged (csunplugged.org)
66 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Licensed under Creative Commons 67 © 2015 Computer Science Unplugged (csunplugged.org)
68 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
Licensed under Creative Commons 69 © 2015 Computer Science Unplugged (csunplugged.org)
70 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)
What’s it all about?
Computers store a lot of information, and they need to be able to sift through it quickly. One of the biggest search problems in the world is faced by Internet search engines, which must search billions of web pages in a fraction of a second. The data that a computer is asked to look up, such as a word, a bar code number or an author’s name, is called a search key.
Computers can process information very quickly, and you might think that to find something they should just start at the beginning of their storage and keep looking until the desired information is found. This is what we did in the Linear Searching Game. But this method is very slow—even for computers. For example, suppose a supermarket has 10,000 different products on its shelves. When a bar code is scanned at a checkout, the computer must look through up to 10,000 numbers to find the product name and price. Even if it takes only one thousandth of a second to check each code, ten seconds would be needed to go through the whole list. Imagine how long it would take to check out the groceries for a family!
A better strategy is binary search. In this method, the numbers are sorted into order. Checking the middle item of the list will identify which half the search key is in. The process is repeated until the item is found. Returning to the supermarket example, the 10,000 items can now be searched with fourteen probes, which might take two hundredths of a second—hardly noticeable.
A third strategy for finding data is called hashing. Here the search key is manipulated to indicate exactly where to find the information. For example, if the search key is a telephone number, you could add up all the digits in the number and take the remainder when divided by 11. In this respect, a hash key is a little like the check digits discussed in Activity 4—a small piece of data whose value depends on the other data being processed. Usually the computer will find what it is looking for straight away. There is a small chance that several keys end up in the same location in which case the computer will need to search through them until it finds the one it is seeking.
Computer programmers usually use some version of the hashing strategy for searching, unless it is important to keep the data in order, or unless an occasional slow response is unacceptable.
Licensed under Creative Commons 71 © 2015 Computer Science Unplugged (csunplugged.org)
Activity 7
Lightest and Heaviest—Sorting Algorithms
Summary
Computers are often used to put lists into some sort of order, for example names into alphabetical order, appointments or e-mail by date, or items in numerical order. Sorting lists helps us find things quickly, and also makes extreme values easy to see. If you sort the marks for a class test into numeric order, the lowest and highest marks become obvious.
If you use the wrong method, it can take a long time to sort a large list into order, even on a fast computer. Fortunately, several fast methods are known for sorting. In this activity students will discover different methods for sorting, and see how a clever method can perform the task much more quickly than a simple one.
Curriculum links
✓ Mathematics: Measurement – Carrying out practical weighing tasks. ✓ Computing: Algorithms
Skills
✓ Using balance scales ✓ Ordering ✓ Comparing
Ages
✓ 8 and up
Materials
Each group of students will need: ✓ Sets of 8 containers of the same size but different weights (e.g. milk
cartons or film canisters filled with sand) ✓ Balance scales ✓ Worksheet Activity: Sorting weights (page 74) ✓ Worksheet Activity: Divide and conquer (page 75)
72 Licensed under Creative Commons
© 2015 Computer Science Unplugged (csunplugged.org)