answers - tcsocc.uktcsocc.uk/files/tcsocc-2017-answers.pdf · the tcs oxford computing challenge is...

39
2017 Answers The TCS Oxford Compu0ng Challenge Pilot Year

Upload: nguyenphuc

Post on 25-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

2017Answers

TheTCSOxfordCompu0ngChallenge

PilotYear

2

Working Group for the TCS Oxford Computing Challenge 2017:

Daphne Blokhuis Stuart Golodetz Peter Millican Dave Oostendorp Chris Roffey Eljakim Schrijvers Sue Sentance

The TCS Oxford Computing Challenge could not have been run this year without the generous support of Tata Consultancy Services and our National Organising Body, Oxford University.

I also want to thank Dave Oostendorp who worked so hard on developing the Blockly question types we have used in this Challenge.

Finally it is important to recognise all the School Coordinators who have to organise the time for the students to take part. In a busy school, finding an hour for a select set of students to sit quietly in front of a computer and be supervised is not an easy task.

Copyright 2017 TCS Oxford Computing Challenge – Licence: CC-BY-NC-SA 4.0

The TCS Oxford Computing Challenge is in its pilot year. It is an online challenge that asks invited students in the UK and English-speaking International Schools around the World to solve tasks using Computational Thinking skills and then provide coded solutions. It has come about because, through the Bebras Challenge, we have become aware of the incredible talent many of our students in the UK have and we felt we had to provide something to help encourage them to develop that talent further.

We hope students will prepare carefully throughout the year by using resources pointed to on tcsocc.uk and the archived challenges that are made freely available for all to use.

It is not a trivial task creating auto-marking computing tasks and a lot of this year has been spent developing these. We have introduced four new types of tasks that can assess a variety of solutions rather than pushing students into one supposedly correct answer. Please note that although example answers are provided in the rest of the booklet, when it comes to programming there are usually many ways to crack a nut!

On the following pages you will find the 21 tasks used in the TCS Oxford Computing Challenge 2017 pilot. Above each task is noted which age groups and at what level the tasks were used.

After each task there is an example answer, an explanation of how the answer could be obtained plus a section on how the tasks are related to Computational Thinking. Example answers have been provided in either blocky or Python, and occasionally both. Unfortunately, it is not possible to provide answers in all programming languages. Python has been chosen because it is currently the most common text-based language being taught in UK schools. We have also mapped each task to up to three Computational Thinking Skills plus a Computer Science domain. This system is outlined below and is thanks to the work of Valentina Dagienė, Sue Sentance and Gabrielė Stupurienė:

Each task is assigned up to 3 Computational Thinking Skills from

Abstraction (AB) Algorithmic Thinking (AL)Decomposition (DE)Evaluation (EV) Generalisation (GE)

Each task is assigned one Computer Science Domain from:

Algorithms and programmingData, data structures and representations Computer processes and hardwareCommunication and networkingInteractions, systems and society

Up to three Keyword Tags may also be added.

Introduction

4

England and Wales Scotland Northern Ireland

Elite: Years 12 & 13 Seniors: Years 10 & 11 Intermediate: Years 8 & 9 Juniors: Years 6 & 7

Elite Years S5 & S6 Seniors: Years S3 & S4Intermediate: S1 & S2 Juniors: Years P6 & P7

Elite: Years 13 & 14 Seniors: Years 11 & 12Intermediate: Years 9 & 10Juniors: Years 7 & 8

It is our sincere hope that this Computational Thinking information provided will enhance the usefulness of this booklet for Computer Science teachers and their students. It is for this reason that this booklet is being distributed to as many schools as possible and being made available as a PDF with a Creative Commons licence allowing it to be shared and freely as long as it is for non-commercial purposes.

The 2017 challenge was conducted in four age groups:

Invited students had to have achieved in the top 10% in their age group in the previous UK Bebras Challenge.

The challenge is completed online in schools under the supervision of teachers.

There were 15 tasks to be attempted in 1 hour.

Students were expected to attempt the first 10 tasks from Sections A and B and then try to solve one or two tasks from Section C.

The organisers wish to pass on a special thanks to all the teachers who have made it possible for their students to enter into this competition by taking on a huge amount of the necessary administration for us.

Keep informed by visiting tcsocc.uk

Introduction

5

Contents

Simple Maze page 6

Not a Pentagon page 7

Temperature Converter page 8

Second Counter (a) page 9

Second Counter (b) page 10

Spiral page 11

Cross Roads page 12

Snake page 13

Logo Maker page 14

Four Squares page 15

OR gate page 16

Wrong Tree page 18

SCSV to CSV page 20

Mean Problem page 21

Crazy Sequence page 23

DNA page 25

An Island Landing page 27

Adding Factors page 28

Palindrome Hunter page 29

Odd Parity Bits page 31

How Many Routes? page 33

6

Juniors: Intermediates:

Seniors: Elite:

A

A robot has to travel through a maze from the red square to the green square.

Task:Write a program to solve the maze.

Answer:One possible solution

Explanation:The solution provided for this maze requires the programmer to realise that the robot has only to move forward or left to reach its objective.

As there is no limit to the number of blocks allowed, an alternative solution would be to provide a long list of steps that takes the robot to its objective.

It’s Computer Science:

Simple Maze

CT Skills - Algorithmic Thinking (AL) CS Domain - Algorithms and programming Tags - Blockly, Loops, Mazes

In mobile robotics, navigation is a common problem. Maze solving is not so common but requires similar Computer Thinking skills to be employed. To solve this problem, an autonomous robot is used. Mazes can be of different kinds: having loops, without any loops, grid systems or without a grid system. In this short loop maze algorithm, the robot is instructed to prioritise turning left, where possible, and otherwise to simply move forwards.

7

Explanation:

It’s Computer Science:CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV)CS Domain - Algorithms and programming Tags - Blockly, Debugging, Turtle

Humans get through life by making assumptions and filtering out noise. Currently, computers are not good at this. When we write programs, every detail has to be correct and everything thought of. Even with carefully designed systems this is not always possible. This makes debugging code that does unexpected things an important skill.

Types of Programming Errors: http://www.bbc.co.uk/education/guides/zgmpr82/revision/5 Debugging: http://www.bbc.co.uk/education/guides/zgmpr82/revision/6

A young programmer has tried to write his first program, but he cannot understand why he gets a small triangle instead of a large pentagon. "But I have looped five times, so surely this should not make a triangle!" he says.

Task: 1. Run the program without changing anything to see what is happening. 2. Fix the program so it works properly.

The turtle turns too far causing the triangle.To create a pentagon the turtle needs to turn 360o / 5 = 72o

The side length also needs fixing.

Answer:

Juniors: Intermediates:

Seniors: Elite:

Not a Pentagon A A

8

Answer:

It’s Computer Science:CT Skills - Evaluation (EV)CS Domain - Algorithms and programming Tags - Blockly, Functions

Converting mathematical or scientific formulae into programming code is very common. Thankfully it is one of the easiest things in programming.

Monty Thrower is a keen gardener but only has a thermometer that gives the temperature in Fahrenheit. Monty tells you this is the formula he uses:

Temperature (in oC) = (Temperature (in oF) - 32) x (5 ÷ 9)

He asks you to write him a program to save him lots of tapping on his calculator

Task:

Complete the function provided so that it outputs 20.Do this by providing the code to perform the calculation above. Do not alter any of the blocks provided.

One possible solution is:

Temperature Converter Juniors: Intermediates:

Seniors: Elite:

A A

Explanation:This is a simple implementation of a mathematical formula. It can be done in one step or more.

9

Mrs. Precise will only consider times in seconds.She needs a program that will convert hours and minutes to seconds.

Task:

Complete the function below so that it correctly calculates seconds from hours and minutes. (Test data: 1hour and 5minutes = 3900 seconds.)

Answer:

Explanation:A mathematical algorithm needs to be worked out. This can be done in one or more steps. Using more steps often makes the logic easier for someone reading your code.

It’s Computer Science:CT Skills - Algorithmic Thinking (AL) CS Domain - Algorithms and programming Tags - Blockly, Functions

Solving problems involving times or lengths of time is a very common task in our connected world.

Juniors: Intermediates:

Seniors: Elite:

Second counter (a) A A

One possible solution is:

10

Juniors: Intermediates:

Seniors: Elite:

Second Counter (b) B B

Mrs. Precise will only consider times in seconds.She needs a program that will convert hours and minutes to seconds.

Task:

Complete the function below so that it correctly calculates seconds from days, hours and minutes. (Test data: 1 day, 1hour and 5minutes = 90,300 seconds.)

Explanation:A mathematical algorithm needs to be worked out. This can be done in one or more steps. More steps often makes the logic easier for someone reading your code.

It’s Computer Science:CT Skills - Algorithmic Thinking (AL) CS Domain - Algorithms and programming Tags - Blockly, Functions

Solving problems with times or lengths of time is a very common task in our connected world.

Answer:One possible solution is:

11

Answer: Explanation:

It’s Computer Science:CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV)CS Domain - Algorithms and programming Tags - Blockly, Turtle, Debugging

Debugging code that does unexpected things an important skill.

Types of Programming Errors: http://www.bbc.co.uk/education/guides/zgmpr82/revision/5 Debugging: http://www.bbc.co.uk/education/guides/zgmpr82/revision/6

A program is designed to draw the spiral shown. Unfortunately it is not quite correct yet. You can Run the provided code to see what actually happens. (It is a good idea to click the Fast button before running the code!)

Task:Amend the code to draw the required crazy spiral. (Hint: The programmer has simply mixed up some numbers.)

The signs of the last two integers in the program are correct but the 5 and 2 need swopping around.

Juniors: Intermediates:

Seniors: Elite:

Spiral A A

One possible solution is:

12

Answer: Explanation:

It’s Computer Science:

CT Skills - Algorithmic Thinking (AL) CS Domain - Algorithms and programming Tags - Blockly, Loops, Mazes

In mobile robotics, navigation is a common problem. Maze solving is not so common, but requires similar Computer Thinking skills to be employed. To solve this problem, an autonomous robot is used. The wall following algorithm is famous with maze enthusiasts and can be used to solve many mazes.

A Robot has to find its own way from the Red Square to the Green Square. Write a program, to solve the maze, in the workspace below. (Watch out, there is a limit to the number of blocks you are allowed to use in your program!)

Try out your solution with the Run button. Save and submit your solution with the Run and submit button.

Juniors: Intermediates:

Seniors: Elite:

Cross Roads B A

A A

One possible solution is: The solution provided provided here is produced by realising that the maze can be solved with a wall following algorithm. It does not matter which wall is followed but the right wall provides the quickest solution.

The restricted number of blocks allowed prevents this task from being solved with a simple sequence of steps.

13

Help the Robot escape the maze.

Task:Write a program, to solve the maze shown below.(Note that there is a limited amount of blocks allowed so you will need at least one loop.)

Answer: Explanation:

It’s Computational Thinking:

A loop must be used because there was a limited number of blocks allowed to solve this task.

Juniors: Intermediates:

Seniors: Elite:

Snake B B

A A

CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV)CS Domain - Algorithms and programming Tags - Blockly, Loops, Mazes

In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is met, such as whether a counter has reached a prescribed number.

If the condition is not met, the program moves to the first instruction and repeats the sequence. If the condition is met, the loop is exited and the program continues with whatever instruction is next after the loop.

A loop is a fundamental programming idea that is commonly used in writing programs.

One possible solution:

14

A new country has been founded from five small, friendly countries. A program is required that can be used to create its new logo.

Task: Write a program to produce the pattern shown. (Note you will need to use at least one loop because you are limited to 10 code blocks.)

Juniors: Intermediates:

Seniors: Elite:

Logo Maker B B

A A

Answer:One possible solution:

Explanation:

It’s Computer Science:

Two loops must be used because there was a limited number of blocks allowed to solve this task.

CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV), Generalisation (GE)CS Domain - Algorithms and programming Tags - Blockly, Loops, Turtle

This task requires a short solution because lots of copy-pasted blocks will result in you running out of blocks.

It is good to identify patterns in problems when programming and pull out repetitive code into either functions or loops. One reason for this is because, if something needs fixing at testing time, it only has to be done in one place.

15

CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV), Generalisation (GE)CS Domain - Algorithms and programming Tags - Blockly, Loops, Turtle

This task requires a short solution because lots of copy-pasted blocks will result in you running out of blocks.

It is good to identify patterns in problems when programming and pull out repetitive code into either functions or loops. One reason for this is because it is easier for someone reading the code to understand what is going on.

Answer: Explanation:This task limited students to 20 blocks. The reason for this was to try to encourage students to solve this task elegantly rather than by writing lots of code.

It’s Computer Science:

One possible solution:

Juniors: Intermediates:

Seniors: Elite:

Four Squares B B

B

Task: Write a program to draw the four squares shown. (Note you will need to use at least one loop because you are limited to 20 code blocks.)

16

OR gates return True or False when two variables are compared. In a logic circuit found in computers the inputs and outputs are high or low voltage but we can represent these as 1 for high and 0 for low. An OR gate returns 1 if one or both of the variables input are high (1). An OR gate returns 0 if both of the variables input are low (0).

Task: Complete the function supplied so that it works like an OR gate, using the blocks provided.

Juniors: Intermediates:

Seniors: Elite:

OR gate B B

A A

17

Juniors: Intermediates:

Seniors: Elite:

OR gate B B

A A

CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV)CS Domain - Algorithms and programming Tags - Blockly, Functions, Logic gates

Logic statements are usually available to a programmer, but not in this problem! Here you were asked to program one just using if statements. When a programmer uses an OR, NOT or AND keyword in their program they are asking the computer to run a logic circuit (called a logic gate in electronics).

Boolean Logic and Logic Gates: http://www.bbc.co.uk/education/guides/zc4bb9q/revision

Answer: Explanation:There are various ways to program this. Essentially, using a series of if clauses, the programmer has to ensure that the output required for each input combination is satisfied.

It’s Computer Science:

One possible solution:

18

Juniors: Intermediates:

Seniors: Elite:

Wrong Tree C C

A A

Adrian has requested a program that will draw the exact tree shown below. Unfortunately, the first attempt is not quite correct yet. For example, the branch width is too thick: it should be only 3 pixels wide rather than 5.

You can Run the provided code to see what actually happens. (It is a good idea to click the Fast button before running the code!)

Task: Amend the code to draw the required tree.

19

CT Skills - Algorithmic Thinking (AL), Evaluation (EV) CS Domain - Algorithms and programming Tags - Blockly, Debugging, Turtle

Programming solutions often require testing and tweaking of parameters to get something just right. This is especially important when they involve graphic elements.

It is also important to be able to read other people's code, although it is not always necessary to understand exactly how it works. If programs are well-commented and algorithmic details are abstracted into functions or classes, it is much easier to read and understand a program.

In the program given in this task, it turns out that the complicated algorithm for producing the tree does not need to be fully understood, just that it can be adjusted by changing the parameters. In terms of programming style, it would have been better to have all the parameters passed to the function rather than have some of them as global variables and some as required arguments!

It is worth remembering that if you come back to your own program after a few months, it can seem like someone else has written it!

Answer:

One possible solution:Explanation:

This task involves understanding how the program has been written, realising that all the important parameters can be adjusted outside of the function and then systematically finding their effect and adjusting them one at a time.

The width, detail, angle and length all need changing, but to do all at once will simply create confusion.

It’s Computer Science:

Juniors: Intermediates:

Seniors: Elite:

Wrong Tree C C

A A

20

Computer scientists do not always get their data in the form they require.

Here the data is presented as a semi-colon separated set of values (scsv) but the programmer wants the data as comma separated values (csv).

Task: Complete the function provided so that it converts any supplied scsv input to csv output. (Do not alter any of the blocks provided.)

Answer:

Explanation:

It’s Computer Science:CT Skills - Algorithmic Thinking (AL), Evaluation (EV) CS Domain - Algorithms and programming Tags - Blockly, Lists, Functions

This task tried to mimic the situation, which developers often face, of having to produce solutions in unfamiliar environments. This could be in an unfamiliar programming language or when using a library that they have not used before. If this was critical, of course, a developer would usually consult the documentation!

It was felt likely that students would not be familiar with either lists, or at least lists in Blockly. This task was there to challenge students to explore the unfamiliar and find a very useful block that is available in the Lists menu. It tests flexibility in new situations.

Juniors: Intermediates:

Seniors: Elite:

SCSV to CSV C C

B B

One possible solution:

21

Here is a large number: 5820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128

Task: Write a program (using any programming language) that does the following: - finds the mean (average) of all of the digits in this number.

Enter the mean of the digits below and press the Save button.

Juniors: Intermediates:

Seniors: Elite:

Mean Problem C C

C B

4.45Answer:

Explanation:

Here is a possible solution using Python 3:

This isn't a difficult task algorithmically but it has some niggling difficulties when programming it. First the programmer has to get access to the digits in the string and then find some way of iterating through them one at a time. The Python solution takes advantage of the ability to treat a string as an iterable data-type (which is common in many program languages), but the characters still need to be cast to integers before the maths can be done. This is done with int(char) in Python. This is equivalent to using the atoi() function from stdlib.h in C or C++.

On the next page is a possible Blockly solution (using our Blockly Standard Playground). This solution takes another tack: The string is separated into a list to make it iterable. Still the handling of the fact that the numbers are stored as strings has to be dealt with. In most other programming languages this would simply be handled by casting but with Blockly, some sort of mapping was required.

22

Juniors: Intermediates:

Seniors: Elite:

Mean Problem C C

C B

It’s Computer Science:CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV)CS Domain - Algorithms and programming Tags - Iteration

This task requires the programmer to be very familiar with their chosen programming language, especially in terms of how to manipulate and iterate through different data types.

Here is a possible Blockly solution (using our Blockly Standard Playground):

23

Answer:

Explanation:

340010386766614455385653 or 3.4001038676661446e+23

Here is a Python 3 solution: Here is a Blockly solution:

Juniors: Intermediates:

Seniors: Elite:

Crazy Sequence C C

C C

Georgina loves making up weird sequences. Here is her latest: 1 2 4 8 10 20 22 44 46 92 94 188 190 380 ...

Task: Write a program (using any programming language) that: - finds the sum of the first 150 numbers in Georgina's series.

Enter the answer below as an integer and press the Save button. (1450456 is in the correct form; 1,450,456 is not.)

24

It’s Computer Science:

CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV)CS Domain - Data, data structures and representations Tags - Iteration, IF conditions

This task is similar to Mean Problem in terms of requiring the programmer to be familiar with how to manipulate and cast the standard data-types such as strings and integers; however, it also adds pattern identification into the mix. In most languages careful choice of the number data-type used will be necessary to store the final answer as it is a large integer. In Java a BigInteger or in Visual Basic a Decimal would be suitable.

Before it is worth trying to code anything, programmers have to be able to solve the problem or at least see how they could solve it. In this task, there is no point trying to code a solution unless you can see that Georgina's sequence starts with 1 and then alternately doubles and adds two to the previous number in the series.

Experienced programmers often get pen and paper out before they touch their keyboard!

It is worth knowing that sequences are often more easily solved with maths and a calculator than programmatically, but only after you have studied sequences and finding the nth term.

Juniors: Intermediates:

Seniors: Elite:

Crazy Sequence C C

C C

25

Answer:GCGGTCTTCATGAGCAGGCGCCCACCCATCAATTGTATTATTCTGGAAAAGTAAAGAAAGCTAATTTAAAAAGACCTTCTGTGATCTATTGGTGTAGATCCTACTAGATAATTTACGATCGGTGACGAATGTAGTCCTGCAGCATGAGAAACATCTAGTAAAGCTTCACTCCTTCTTTCTTCCCTCGCACGTCCCAACCGACTATTCTCGTACATCGCCGAGTAGTCGAGAATCGCTGATTTGAAGGGTGTACCAAGCTCTTAGCGTCGTCTAATAGGAATTGCCGAAAACTTCCGGAGGCCGTGGTCCAGTGCCTCGATTACACCTGCTGGAAGGAAAAGACAGGAGTAATCCTCGAGGAGTAAGACCTGCTTTTAGTCACCGAGGATAGACCTGAGAACCGGTGAGCTTCCACGTGCTAAGTTTGTGACCTTTGTACCTGGTTATATT

Juniors: Intermediates:

Seniors: Elite:

DNA C C C

The Theory The double helix in DNA is composed of a sequence of four different bases, which can be represented by the letters A, C, G and T.

The two separate strands in the double helix attach to each other because the bases exist as complementary pairs:

A attaches to T C attaches to G

Thus the second strand in the helix is formed from the 'complementary substitution' of each base. e.g. CCCCTACTA becomes GGGGATGAT

Task: Write a program (using any programming language) that finds the complementary sequence of the strand of DNA shown below. Then, paste the output (containing all capitals, no spaces and no line returns) into the answer field below and press the Save button.

The Strand: CGCCAGAAGTACTCGTCCGCGGGTGGGTAGTTAACATAATAAGACCTTTTCATTTCTTTC GATTAAATTTTTCTGGAAGACACTAGATAACCACATCTAGGATGATCTATTAAATGCTAG CCACTGCTTACATCAGGACGTCGTACTCTTTGTAGATCATTTCGAAGTGAGGAAGAAAGA AGGGAGCGTGCAGGGTTGGCTGATAAGAGCATGTAGCGGCTCATCAGCTCTTAGCGACTA AACTTCCCACATGGTTCGAGAATCGCAGCAGATTATCCTTAACGGCTTTTGAAGGCCTCC GGCACCAGGTCACGGAGCTAATGTGGACGACCTTCCTTTTCTGTCCTCATTAGGAGCTCC TCATTCTGGACGAAAATCAGTGGCTCCTATCTGGACTCTTGGCCACTCGAAGGTGCACGA TTCAAACACTGGAAACATGGACCAATATAA

26

Juniors: Intermediates:

Seniors: Elite:

DNA C C C

Explanation:

This task cannot easily be solved with the Blockly tools available. It is important when copying in the original, large string to handle the line endings. One way is to simply delete them, which is what was done below. This produces a very long string that has had to be truncated to fit below.

Here is a Python solution:

It’s Computer Science:CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV)CS Domain - Algorithms and programming Tags - Iteration, IF conditions

Computer Science is important in almost every area of life. It is especially useful in Science where it is used to solve complicated problems quickly and produce models of systems such as the solar system or the environment to help scientists understand them. The context here is Biology and the Human genome.

Computer Scientists have to have a good general education and be quick learners if they are to help experts in other disciplines.

More DNA programming challenges: http://rosalind.info/

27

A robotic boat starts at the red square and wants to land at an island jetty. The jetty is indicated by the green square.

Task:Write a program to guide the robotic boat to its destination. (Note that there is a limited amount of blocks allowed.)

Try out your solution with the Run button. Save and submit your solution with the Run and Save button.

Answer:Explanation:This task cannot be solved by a wall-following algorithm, as the boat will keep avoiding the Island. There are also not enough blocks available to simply guide the boat to the green square with something like this:

It’s Computer Science:CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV)CS Domain - Algorithms and programming Tags - Blockly, Loops, Mazes

Many problems in Computer Science have well researched algorithms that should be the first port of call. Sometimes they are not suitable to a given situation. This is when the fun starts, as we have to get creative.

One possible solution:

Juniors: Intermediates:

Seniors: Elite:

An Island LandingB

B B

This was not intended to be this difficult! The solution on the left was intended to be available but "someone" set the block limit too low. Sorry.

Note from task author:

28

Task: Write a program (using any programming language) that does the following: - finds all of the factors of 4800 including 1 and 4800, - finds the sum of all of the factors of 4800 found.

Input the result of this computation as an integer below. Don't forget to press the Save button.

Answer:

Explanation:

CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV)CS Domain - Algorithms and programming Tags - Blockly, Iteration, IF conditions

Maths often provides very easy algorithms for programmers to use. However, whenever loops are involved, it is very important to check the boundary data carefully.

Getting these sort of problems correct is all about being careful with the boundary data when looping. It is a good idea to print out what is going on as well as the answer so that you can check it fulfils all the requirements.

Here are possible solutions in Python and Blockly:

It’s Computer Science:

Juniors: Intermediates:

Seniors: Elite:

Adding Factors B B

15748

29

Answer:

15 and 16 were allowed answers to allow for those students who did or did not include III as a palindrome.

Explanation:

Task: Write a program (using any programming language) that does the following: - imports the UTF-8 text file below - searches for and counts the number of times any palindromic words with 3 or more letters occur in the text. - outputs the number of palindromic words it finds.

Enter the number of palindromes as an integer and press the Save button.

File: alice_in_wonderland.txt

Alice loves palindromes. These are words that read the same way forwards and backwards, such as 'kayak'.

Alice decides it would be great to have a program that searches through a text file and counts the number of palindromic words it contains. The more palindromes in a book, Alice thinks, the better the book!

Juniors: Intermediates:

Seniors: Elite:

Palindrome HunterC

C C

Here again is a situation that shows how important it is to include code that checks what is being caught by your algorithm as well as just calculating the answer. If the palindromes are printed out, it is easy to check for anomalies such as III. In this instance, III (the chapter number) was allowed to be included or excluded.

On the next page is a possible Python solution.

30

It’s Computer Science:

CT Skills - Abstraction(AB), Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV), Generalisation (GE) CS Domain - Algorithms and programming Tags - File handling, Iteration, If conditions

Manipulating files and strings in programs is very important in our world. A lot of times it is easier to use an application that is made for this – counting words, for example, is most easily performed by a word processor. It is, however, worth remembering that, even in these cases, it was a team of Computer Scientists who programmed the facilities in the word processor in the first place.

Juniors: Intermediates:

Seniors: Elite:

Palindrome HunterC

C C

A possible Python solution:

31

What is an odd parity check? When data is sent over a computer network, it is sent as bytes of 8 bits. A bit is a '0' or a '1'.

The information being sent is often encoded in the first seven bits of each byte.

As a check that all the data has arrived safely, the bytes have an extra bit added to complete the byte. The extra bit is chosen such that the sum of the bits in the byte add up to an odd number:

e.g. 0110100 becomes 01101000

1110111 becomes 11101111

If the data arrives safely, the sum of all the bits in every byte will be odd. This indicates that the bytes have arrived safely.

You have been asked to write a program (using any programming language) that does the following:

• takes the groups of seven bits provided below and creates the bytes formed by adding a 0 or 1 to the end so that an odd parity bit check can be performed,

• counts the number of times a 1 is required rather than a 0.

Juniors: Intermediates:

Seniors: Elite:

Odd Parity Bits C C

0101010 0101100 1111100 0011011 0111000 1000101 0000100 0011111 1110110 0000011 0101010 0011111 0110010 1000111 0100011 0100011 1101101 1100101 1110001 0011001 1110000 1111010 1001001 1110011 1111101 0101011 0011110 1000011 0111101 0101001 0111000 0011011 0010010 1010100 1110111 0101000 0011000 0011110 1111001 1010110 1000011 1000000 0010011 1100011 1110010 1101100 0101101 0101000 0110010 1111010 0101110 0001111 1011100 1111011 0111111 1011111 1100010 0001100 0110111 0111101 1001001 0010100 0000110 0000101 0000100 1000100 0101111 0010000 1111110 1011010 1110000 1110110 1001111 0001101 1110101 1011100 0011111 0010011 0110011 1101110 1100001 0001110 0000101 0111110 1011101 0000101 0110110 1011010 0110010 0000000 1010100 0101011 1011011 1011101 0000001 0010000 1001111 1010110 0010111 1011010 0000101 1111110 1011111 1110010 1100000 1010101 0001110 0100001 0100011 0111001 1101110 1001011 0000100 1100110 1000100 0101111 0100000 0101010 0011001 1001001 1101001 0110010 1101110 0010000 1011110 0100001 1010010 0111000 1101000 0010111 0111011 1010001 0000001 1001000 0000101 0100111 1011100 1110100 0100000 0000101 1101100 0001001 0000100 0110111 1011001 0110101 0011001 0100110 1110101 1000001 1101000 1111101 1110111 1101010 0100101 0100000 1010011 1110011 0101000 0010011 0011011 0111110 1011000 0111111 0100000 1010111 1010110 0000000 1001010 0001010 0001100 1010010 0100000 1010100 1001011 0010001 1001001 0111001 0111111 1101011 0111111 0100111 1011001 1111101 1001111 0010111 1001000 0101101 1011000 1000010 0100000 1001011 0100001 1101011 0100101 0011101 1100101 1001110 1011001 1101010

Task:Find the number of times a 1 is required. Enter this number as an integer below.

32

99

Answer:

Explanation:

Although the program spec asks for a full set of bytes to be output, the task only requires an integer to be output. This task is therefore slightly easier than would first appear.

Here is a possible solution in Python:

It’s Computer Science:CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV)CS Domain - Data, data structures and representations Tags - Iteration, IF conditions

The Computer Science context here is of extreme importance. Without these sort of checks and systems we would not be able to safely pass files and data around the internet, or within our own computers and devices.

This task requires the programmer to be very familiar with their chosen programming language, especially in terms of how to manipulate and iterate through different data types. Again, we have numbers stored as text. This has to be separated in some way into an iterable container data type such as a list or array. After this, it is easy.

Juniors: Intermediates:

Seniors: Elite:

Odd Parity Bits C C

33

A simple network of nodes is shown below where there are no loops and all the paths between the nodes point in one direction.

There are 3 possible routes from node a to node g.

Nodes are represented by letters.

Links are represented by pairs of nodes, where the first node is where the link starts from and the second is where it goes to.

The whole network can therefore be described by these two data structures:

Enter the number of possible routes below and press the Save button.

Nodes: (a,b,c,d,e,f,g) Paths: [(a,b),(a,c),(b,e),(c,d),(d,e),(d,f),(e,g),(f,g)]

Juniors: Intermediates:

Seniors: Elite:

How Many Routes?C

Nodes: (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) Paths: [(a,b),(a,h),(a,u),(b,c),(b,o),(c,j),(c,p),(d,k),(d,l),(d,x), (e,f),(e,r),(f,m),(f,s),(g,z),(h,b),(i,c),(i,j),(j,d),(k,e), (l,f),(l,m),(l,y),(m,g),(n,h),(n,o),(o,i),(p,q),(p,w),(q,k), (q,r),(r,l),(s,t),(t,z),(u,p),(v,p),(w,q),(x,s),(y,s)]

Task:Write a program (using any programming language) that finds the number of routes from beginning to end in any network of the same form. You might find it easiest to start by solving the example above, where you already know the answer.

Find the possible number of routes from a to z for the network described below.

34

Answer:

Explanation:

The keen computer scientist will recognise that the problem deals with what appears to be a directed acyclic graph and asks the programmer to list the number of routes between two 'Nodes' of the graph. In Graph theory, we refer to 'Nodes' as vertices and 'Links' as edges. For the rest of this explanation, we will do the same.

The question explicitly tells the programmer that the graph is acyclic (although not using this exact phrase).

Although there are many solutions to this problem, most will be split into the following stages:

1) convert the vertices and edges given as text in the question into a data structure representing the graph

2) perform a search of the graph for all routes from the start vertex to the end vertex — whilst keeping track of the number of routes found

3) output the results

In most programming languages a 'graph' is not a common data structure and so the time-limited participant will probably instead represent the graph as a combination of arrays, lists or hashes (in Python, tuples, lists or dictionaries, in Java, Arrays, Lists or Maps), although they may choose to define a custom data type instead.

We provide two solutions in Python; neither are optimal, as the intention in a challenge like this is to get to the answer as quickly as possible, but they solve the problem. Both start by reading the vertices and edges into a tuple and a list of tuples as implied by the format given in the question, and continue to perform a depth-first search of the tree of edges beneath the start vertex. After they have read in the vertices and edges, they continue as follows.

Solution 1:

Read the edges into a dictionary of sets

The program then defines a function to expand the route:

INPUT: 'inputRoute' -> a string representing the route so far,

'resultNode' —> the string representation of the vertex at which to end

PROCESS: get the last vertex in the current route

lookup all vertices that this letter has edges to that have not already been in the route and if it is the last vertex add the complete route to the result list,

otherwise recursively call this method with the new longer route and add any routes that the method returns

Finally the program calls this function with a starting vertex of 'a' and an end vertex of 'z' and prints the resulting list and it's length.

[Code on next page]

166

Juniors: Intermediates:

Seniors: Elite:

How Many Routes?C

35

Solution 1:

Juniors: Intermediates:

Seniors: Elite:

How Many Routes?C

36

Juniors: Intermediates:

Seniors: Elite:

How Many Routes?C

Solution 2 (a simplified version using only the list of tuple pairs given and iterating over them at each stage of the search):

The program first defines a function to find the number of routes:

INPUTS: 'paths' —> the list of tuple pairs given, 'start' —> a string representing the start node, 'route' —> a list of vertices that make up the route so far, 'end' —> the string representation of the node at which to end.

PROCESS: iterate over all of the paths and, if they start with the start node, recurse with the new longer route and the next start node

and,

if they start with the start node and end with the result node, increment the result count

return the result count

The program then calls the function providing the paths, the start, a starting route (a list containing just the start node), and the end node.

It then prints the result count.

Solution 2:

37

It’s Computer Science:CT Skills - Abstraction(AB), Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV), Generalisation (GE) CS Domain - Algorithms and programming Tags - Graph, Depth-first search

This question is to do with algorithms and data structures. The task presents a directed acyclic graph (DAG) problem. In most programming languages, a graph is not a common data structure, and so the graph needs to be represented as a combination of arrays, lists or hashes (in Python, tuples, lists or dictionaries, in Java, Arrays, Lists or Maps).

Graphs, however, are required to model very common problems met by Computer Scientists in, for example, communication or computing networks or transport networks, where finding the shortest route is a common task. A DAG is a specialised form of graph. Not all networks are directed and many have loops that have to be accommodated. In a directed acyclic graph, it should be possible to do a topological sort and many algorithms would add this as an initial step to the algorithms presented above. This might be done first because it checks whether the graph is indeed a DAG and secondly because the time the topological sort algorithm takes to process might be compensated for by the time saved navigating the sorted graph to find the solution required.

Websites: https://en.wikipedia.org/wiki/Directed_acyclic_graph https://en.wikipedia.org/wiki/Graph_theory https://en.wikipedia.org/wiki/Depth-first_search

Juniors: Intermediates:

Seniors: Elite:

How Many Routes?C

38

Sponsors

UK Bebras is free to enter thanks to the support and sponsorship of the following organisations.

Oxford University is the Organising Body of the competition. To find out more about Computer Science and courses involving Computer Science please visit their website.

www.cs.ox.ac.uk/

It has been a pleasure to work with TCS this year as our very enthusiastic sponsor. Tata Consultancy Services is an IT services, consulting and business solutions organisation that delivers real results to global business, ensuring a level of certainty no other firm can match. With a workforce of more than 11,000 people in the UK, TCS is one of the country’s leading digital employers. The company has offices in more than 30 UK locations, including London, Edinburgh, Manchester, Leeds, Ipswich, Norwich, Peterborough and Liverpool. These sites help to deliver digital projects for more than 150 TCS customers in the UK. The company has been ranked number one for customer satisfaction in the UK IT Services sector by analyst firm Whitelane for three consecutive years and has been recognised as one of the UK’s top employers by the Top Employers Institute. TCS was also one of the UK’s leading employers for women in The Times Top 50 Employers for Women and was named Company of the Year at the 2015 Employee Engagement Awards. www.tcs.com

39

Copyright 2017 TCS Oxford Computing Challenge – Licence: CC-BY-NC-SA 4.0