2018 Answers - The TCS Oxford Compu0ng Challenge - The TCS Oxford Computing Challenge
←
→
Page content transcription
If your browser does not render page correctly, please read the page content below
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 has continued to work 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. Working Group for the TCS Oxford Computing Challenge 2018: :Daphne Blokhuis Rosanna Cretney Andrew Csizmadia Stuart Golodetz Peter Millican Dave Oostendorp Chris Roffey Eljakim Schrijvers Sue Sentance Question writing team: Ian Menezes Chirag Nimavat Debraj Pal Chris Roffey Souradip Sanyal Copyright 2018 TCS Oxford Computing Challenge – Licence: CC-BY-NC-SA 4.0 2
Introduction The TCS Oxford Computing Challenge is in its first 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 the last two years have been spent developing these. Please note that although example answers are provided in the rest of this booklet, when it comes to programming there are usually many ways to crack a nut! On the following pages you will find the 25 tasks used in the 2018 TCS Oxford Computing Challenge. 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 3. 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 programming Data, data structures and representations Computer processes and hardware Communication and networking Interactions, systems and society Up to three Keyword Tags may also be added (in addition to the question type).
Introduction It is our sincere hope that the 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 freely as long as it is for non-commercial purposes. The 2018 challenge was conducted in four age groups: England and Wales Scotland Northern Ireland Elite: Years 12 & 13 Elite Years S5 & S6 Elite: Years 13 & 14 Seniors: Years 10 & 11 Seniors: Years S3 & S4 Seniors: Years 11 & 12 Intermediate: Years 8 & 9 Intermediate: S1 & S2 Intermediate: Years 9 & 10 Juniors: Years 6 & 7 Juniors: Years P6 & P7 Juniors: Years 7 & 8 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 4
Contents Long maze short program page 6 Toy car page 7 5 Star page 8 Tricky triangles page 9 Relativity page 11 Genie in a bottle page 12 Graphics Flag page 13 Turtle Flag page 14 Maximum page 15 NOR gate page 16 Black and white page 17 Concentric circles page 19 Hidden code page 20 Big crash page 22 Low gravity page 23 Second highest page 25 Lettuce face page 27 Adding cubes page 29 Palindromic integer page 30 Binary addition page 31 Is it safe? page 32 Parity check page 34 Unusual characters page 36 Sorting numbers page 38 STV page 40 5
Long maze short program Juniors: A Seniors: A Intermediates: A Elite: A A Robot has to find its own way from the Red Square to the Green Square. It faces a long journey but can only store a program with a maximum of 4 code blocks. Task: Write a program to get the robot to its destination. Answer: Explanation: Probably the only solution: The student needs to identify a simple pattern and then write the code with only four blocks to navigate through a long maze. This task involves finding the simplest possible algorithm. It demonstrates that sometimes, when we use iteration, very few instructions can produce a useful outcome. 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 Computational 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 loop maze algorithm, the robot is instructed to prioritise using a minimum number of instructions. Similar thinking skills are required by programmers when optimising programs. 6
Toy car Juniors: A Seniors: Intermediates: Elite: A toy car has broken steering: It can only turn right. Task: Write a program, for the robot, in the workspace below. (Your program must be made from 12 blocks or less.) Answer: Explanation: This is a fairly simple navigation problem but with a command missing in the language. One way of solving this problem is to create a new block with a function to re-instate the missing block! It’s Computer Science: CT Skills - Abstraction(AB), Algorithmic Thinking (AL), Generalisation(GE) CS Domain - Algorithms and programming Tags - Blockly, Mazes The programming languages taught in schools are generally high level languages. They have a lot of useful functions and commands built in. These work because the designers of the languages have made these commands available by programming them in low level languages or by joining simple commands together to make a new one. There is a popular saying in Computer Science: "Standing on the shoulders of giants!" 7
5 Star Juniors: A Seniors: A Intermediates: Elite: Below is some information about how to construct a 5 Star: Task: Write a program to draw the 5 Star shown on the right. Answer: Explanation: One possible solution is: One tactic that can be employed to solve this task is to ignore the supplied diagram, with all its useful angles, and just try and estimate the angles and then narrow in on the correct value. Unfortunately, in this instance, the answer can look correct even when it is out by a tiny amount. However, if this tactic is employed, it is then possible to compare the angles in the nearly correct solution with those in the supplied diagram. e.g. 190 should most likely be 180 (half of 360) It’s Computer Science: CT Skills - Algorithmic Thinking (AL) , Evaluation (EV), Generalisation (GE) CS Domain - Algorithms and programming Tags - Blockly, Turtle In this task we are asked to produce an algorithm to produce a geometric shape. Solving problems in Computer Science is often best approached by sitting down with pen and paper before starting to write code, however, there is more than one way to cook an egg! 8
Tricky triangles Juniors: A Seniors: Intermediates: A Elite: Something is wrong with this program! It is supposed to produce the pattern of triangles as shown in the output space but it goes of at all sorts of angles. 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 pattern. 9
Tricky triangles Juniors: A Seniors: Intermediates: A Elite: Answer: The three "Turn right 720" blocks need to be edited so that they turn right 1200. Explanation: This is a debugging task. There is a clue in the question text: "goes off at all sorts of angles". One way of solving this is to run the given program at a slow pace and notice that the first turn is no where near sharp enough. The turn blocks provided in the toolbox for this task only have a few options so it should not be too long before 1200 is tried. When the first turn is successfully corrected it becomes evident that 72o is not a very "triangular" number so perhaps all three angles need to change form 720 to 1200 It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV) CS Domain - Algorithms and programming Tags - Blockly, Turtle, Debugging, Fractals Debugging programs written by yourself or other programmers is an important skill. There are many tactics a programmer can employ. Here, by pressing the slow button, it is possible to do a kind of stepping through analysis to spot and fix the bugs. 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 10
Relativity Juniors: A Seniors: Intermediates: A Elite: Albert Einstein's famous equation tells us how much energy (E is in joules) is equivalent to a mass of matter (m is in grams): E = mc2 c is the speed of light, which is 300000000 m/s For example, 10g of matter is equivalent to 900000000000000000 joules of energy. Task: Complete the function provided so that it outputs E (as an integer) given any positive value of m between 1 and 100. Do this by providing the code to perform the calculation above. Do not alter any of the blocks provided. Answer: One possible solution is: Explanation: A mathematical algorithm needs to be worked out. This can be done in one step. 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 to accomplish when writing programs. 11
Genie in a bottle Juniors: Seniors: A Intermediates: A Elite: The genie needs to go back in its bottle. Task: Write a program, in the workspace below, that gets the genie to the green square. (Your program must be made from 16 blocks or less.) Answer: Explanation: One possible solution is: First impressions would suggest that this task looks like it will be a very tricky one. How do we get the robot to decide to turn into the bottle across a void? However, the task featured is found in the easy set of tasks so perhaps there is an easier way: With a loop and an if-else block it is possible to drive the robot straight to the target in considerably less than 16 blocks. It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV) CS Domain - Algorithms and programming Tags - Blockly, Mazes 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 12
Graphics flag Juniors: Seniors: A Intermediates: A Elite: Eliza is trying to design a new flag for her club. Task: Write a program, using the commands available, to draw the flag shown. Hint: If you need to see below a shape that you have created, try right-clicking on its code block, select Disable Block and then re-run your program. Answer: Explanation: One possible solution is: It is necessary that the four blocks used to create this image all have different identifiers. It is important that they appear in the correct order in your program, if the lines are not to be obscured by the squares. However, when building the solution it is easiest to program the lines before the squares because otherwise the squares obscure the target image. Finally it is important to get the image perfect rather than an approximation. At this point disabling blocks of code, so that one shape can be focussed on at a time, is a helpful technique. These tasks can take a long time if approached without too much thought. They can also be done exceedingly quickly if the student has mastered the tools and takes a thought through approach. It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV) CS Domain - Algorithms and programming Tags - Blockly, Graphics, Precedence The graphics module allows students to draw images with a traditional coordinate system. They can compare this system with how easy it is to achieve the same output using a turtle system in the Turtle flag task. Most modern programming languages have a simple drawing system that draws shapes and lines to a canvas. In this task precedence of instructions is also important so that shapes processed later on appear above those processed previously. 13
Turtle flag Juniors: C Seniors: B Intermediates: B Elite: Eliza is trying to come up with a new flag design for her club like this one: Task: Write a program to draw the flag shown. (Hint: All Move, Turn and Set widths to values should all be multiples of 10) Answer: Explanation: One possible solution: The two red lines for the flag are relatively easy to produce with the tools available. Filling in the background is less easy. The ragged edges to the flag provide a hint that 6 lines are needed and this suggests that the quickest way to do this might be with a loop. It’s Computational Thinking: CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV) CS Domain - Algorithms and programming Tags - Blockly, Loops, Turtle Although turtle graphics is part of the Logo programming language developed to help young programmers learn computational thinking, there are circumstances where it is not as easy to use as more traditional programming systems. Compare this task with the Graphics flag task which asks students to draw a similar flag using shapes on a coordinate based canvas. 14
Maximum Juniors: B Seniors: Intermediates: B Elite: Task: Complete the function provided so that it outputs the largest number from any given five positive integers. Do not alter any of the blocks provided. Answer: Explanation: One possible solution: There are a variety of ways of solving this problem. One way would be to add the inputs to a list and then re-order it. Another method is provided as the example answer. When using an if statement algorithm to solve this problem it is important not to fall in to the trap of using if-else blocks. This is because all of the inputs have to be tested against the current maximum; none of them must be missed. It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV), Generalisation (GE) CS Domain - Algorithms and programming Tags - Blockly, Functions, Sorting, If conditions Container variables are an important and powerful data-type (in Blocky we use lists) and can be used to good effect in algorithms to solve these type of problems. Alternatively a good knowledge of selection techniques can be used. This task demonstrates again that there is often more than one valid way of solving the same problem. There is more than one way to peel a potato. 15
NOR gate Juniors: B Seniors: A A Intermediates: B Elite: NOR gates return True or False when 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. A NOR gate returns 1 if neither of the variables input are high (1). A NOR gate returns 0 if one or more of the variables input are high (1). Task: Complete the function supplied so that it works like a NOR gate, using the blocks provided. Answer: One possible solution: Explanation: Using an if-else clause, the programmer has to ensure that the output required for each input combination is satisfied. It’s Computer Science: 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 16
Black and white Juniors: B Seniors: Intermediates: Elite: The photo shown is a PNG. One way of creating a black and white version of this image is to set the Red, Green and Blue components of each pixel to be the average of their three values. This is how the black and white image, shown below, is made: Task: Write a program to convert the colour image into a black and white image like the one shown. 17
Black and white Juniors: B Seniors: Intermediates: Elite: Answer: One possible solution: Explanation: The for P in pixels block loops through each pixel in the supplied image in order. It starts from the top left pixel. Inside the loop, the R,G,B values of the pixel need to be found, averaged and then the average value used to replace the current value of the R,G,B values of the current pixel. As these values are now the same for the current pixel, it will now be a grey colour. By looping through all pixels, the whole image will be converted to grey scale. It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV), Generalisation (GE) CS Domain - Interactions, systems and society Tags - Blockly, Loops, RGB values, Image processing We live in a very graphical world where images are everywhere. Computers use a number of formats to store and compress images. Several programming languages provide tools for accessing the R,G,B values in images and then apply mathematical operations to them. These can produce many of the effects commonly found in Photo manipulation tools. 18
Concentric circles Juniors: B Seniors: Intermediates: Elite: Task: Write a program to create the pattern shown using shape blocks. Hint1: All variable values end in a 5 or 0. Hint2: If you need to see below a shape that you have created, try right-clicking on its code block, select Disable Block and then re-run your program. Answer: Explanation: One possible solution: It is necessary for the three blocks, used to create this image, to all have different identifiers. It is also important that they appear in the correct order. However, when building the solution it is easiest to program the upper most circles first to avoid obscuring the target image. It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Evaluation (EV) CS Domain - Algorithms and programming Tags - Blockly, Precedence, Graphics The graphics module allows students to draw images with a traditional coordinate system. You might want to compare this system with how easy it would be to achieve the same output using a turtle system. Most modern programming languages have a simple drawing library available which provides tools to draw shapes and lines to a canvas. In this task precedence of instructions is also important as shapes processed later on appear above previous ones. 19
Hidden code Juniors: B Seniors: A Intermediates: B Elite: The image shown is a PNG. Its pixels are defined by four values: Red 0 to 255 Green 0 to 255 Blue: 0 to 255 A (transparency): 0 to 255 Hidden in the Blue data of the image is a secret code. Task: Write a program to reveal the code word and then enter it as your answer below? Answer: The hidden word is salad. Explanation: An initial attempt at this task might be to change the B value of all the pixels in the image to 255. This will result in an image with a distinct blue tinge. The hidden code will not be found though. At this point, reflecting on the phrase: "Hidden in the Blue data" should lead to an understanding of why this did not work: All the blue data is now the same so the message will be lost. What about boosting the strength of all the blue pixels. Choosing any multiplier above 10 will reveal the answer required. 20
Hidden code Juniors: B Seniors: A Intermediates: B Elite: It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Evaluation (EV), Generalisation (GE) CS Domain - Interactions, systems and society Tags - Blockly, Loops, RGB values, Image processing We live in a very graphical world where images are everywhere. Computers use a number of formats to store and compress images. Several programming languages provide tools for accessing the R,G,B values in images and then apply mathematical operations to them. These can produce many of the effects commonly found in Photo manipulation tools. In this task a code word has been hidden in the image by finding a black area of the image and writing a word with very low values for the blue component of the pixels. By boosting the blue values the word becomes magically visible! 21
Big crash Juniors: C Seniors: B Intermediates: B Elite: Press the Run button to see the shadow of a movie that shows two rocks colliding. Task: Write a program to produce this movie. Hint: All variable values, except number of sides, are multiples of 10. Answer: Explanation: One possible solution: The coded solution has to be worked out carefully through careful observation. Being able to run the target movie, pause it and then scrub through it makes it possible to study the important moments of change. It’s Computer Science: CT Skills - Abstraction(AB), Algorithmic Thinking (AL), Evaluation (EV) CS Domain - Algorithms and programming Tags - Blockly, If conditions, Movie Movies are just many frames of individual images repeated one after another. Animations, have to be stored efficiently on websites to avoid the need for users having to download large files. The system used here is one way of doing this; storing the code required to construct each frame, rather than storing pixel data for thousands of frames. 22
Low gravity Juniors: C Seniors: B A Intermediates: C Elite: Fleageuse is a small planet found in a galaxy far far away. A model of how a purple ball, thrown horizontally, would act in Fleageuse's dense atmosphere is required. Press the Run button to see how it should work. The distance the ball falls can be calculted using this equation: distance fallen = 0.5 x g x t2 where t is time and g is the acceleration of gravity on Fleageuse (which is unfortunately not currently known). Task: Write a program that produces the movie required, to illustrate how gravity acts on this planet. 23
Low gravity Juniors: C Seniors: B A Intermediates: C Elite: Answer: One possible solution: Explanation: The coded solution has to be worked out carefully through careful observation. It is clear that the velocity of the purple ball is constant in the x-axis and that it takes the full 101 frames to cross the canvas. This can be added to the program and then the y-axis movement can be programmed separately. To do this we are using the equation provided. We can simplify 0.5 x g into one variable because we don't know what the value for g is anyway. If we assume g is 1 and use 0.5 we will find, on testing, that the ball falls far to fast. Systematic trial and error should enable us to quickly find the value required to multiply by t2 so that the ball follows the one in the shadow movie. It’s Computer Science: CT Skills - Abstraction (AB), Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV), Generalisation (GE) CS Domain - Algorithms and programming Tags - Blockly, Modelling, Movie Movies are just many frames of individual images repeated one after another. Animations, have to be stored efficiently on websites to avoid the need for users having to download large files. The system used here is one way of doing this: storing the code required to construct each frame, rather than storing pixel data for thousands of frames. Very often programming requires us to encode mathematical or scientific formulae. The good thing is that, when provided with these formulae, writing them into our programs is usually very simple. 24
Second highest Juniors: C Seniors: B B Intermediates: C Elite: Task: Complete the function provided so that it outputs the second highest value from any five given integers. (Do not alter any of the blocks provided.) Example: If the list of numbers sent to the function is 0,6,6,3,5 then the function should return 5 (not 6). Answer: Here is one possible answer: Explanation: 25
Second highest Juniors: C Seniors: B B Intermediates: C Elite: Explanation: The first impression of this task might be that it appears to be quite an easy task. Just sort the list and choose the second number in the sorted list. Careful reading of the question and examination of the example provided shows that this is not quite all that is required. It is the second highest value that needs to be output which is, in some cases, not the same as the second item in a sorted list. Once this is realised (or programmed and marked wrong!) then a new algorithm must be developed. Sorting the list could still be used to find the highest value of the 5 numbers but the example answer instead takes advantage of the fact that there are only five numbers and uses the same method to find the highest and the second highest valued numbers in the list. It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Evaluation (EV) CS Domain - Algorithms and programming Tags - Blockly, Functions, If conditions Many professionally produced programs work well at first and then, over time, an unforeseen set of inputs causes something to go wrong. Indeed, even in Bebras and the TCS Oxford Computing Challenge, we have limited facilities to test tasks and then let them loose on hundreds of thousands of students. Yes we have occasionally fallen foul of this problem too! This task mirrors this circumstance: Our first impression is a good one but a little more thought is required to make sure that our function does exactly what is required. 26
Lettuce face Juniors: C Seniors: B B Intermediates: C Elite: The photo provided is a PNG. If the right hand side pixels in the image are replaced with a mirror image of the left hand side pixels it creates an image like the one shown on the right below: Task: Write a program to convert the original image into the one shown. Answer: One possible solution: 27
Lettuce face Juniors: C Seniors: B B Intermediates: C Elite: Explanation: This is our hardest Blockly task and it has a sting in the tail. The first problem is to collect pixel data and then copy it to the correct pixel locations at the other side of the image. It is not too difficult to realise that the pixels with x coordinates that are half the width of the image need to be copied and then their values need to be set to the pixels in the other half of the image. However, it is quite possible to get to a point where the image looks perfect but the task marker is telling you it is incorrect. The problem could be caused by the programmer's image being out by one pixel. If all the pixels are shifted just one place then this will not be visible to the eye but is correctly marked as wrong. The programmer needs to remember that the pixel data is counting from (0,0) in the top left corner (Tutorial on this: http:// tcsocc.uk/prepare.html#6) and the arrays of pixels that are being iterated through have indexes starting from 0. Hence the program needs to subtract 1 to account for the pixel arrays counting from zero. Look back at the answer provided on the previous page to see how this works. It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV) CS Domain - Interactions, systems and society Tags - Iteration, RGB values, Image processing We live in a very graphical world where images are everywhere. Computers use a number of formats to store and compress images. Several programming languages provide tools for accessing the R,G,B values in images and then apply mathematical operations to them. These can produce many of the effects commonly found in Photo manipulation tools. In this task a reflection is asked for. However, as is often the case in programming and Computer Science, we have to be very careful about the boundary data. 28
Adding cubes Juniors: Seniors: B Intermediates: Elite: Here is a large number: 5820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128 Task: Write a program (using any programming language) that outputs the sum of all the digits cubed in this number. e.g. for input 123, the output would be 13 + 23 + 33 = 36 Answer: 20407 Explanation: Here is a Python 3 solution: 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++. It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV) CS Domain - Algorithms and programming Tags - Iteration, data-types, casting 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. 29
Palindromic integer Juniors: Seniors: C B Intermediates: Elite: Palindromic integers are positive integers, with two or more characters, that would be the same number if read from right to left as when read from left to right. e.g. 11, 232, 1224221 Task: If 11 is the first palindromic integer, write a program to find the 1000th palindromic integer. Enter your answer as an integer below. Answer: 91019 Explanation: Getting these sort of problems correct is all about being careful with the boundary data when looping. In the Python 3 solution provided below the algorithm is abstracted out into a function. This makes it easier to focus on the separate jobs that the program needs to solve and makes the final code easier to read. It’s Computer Science: CT Skills - Abstraction (AB) , Algorithmic Thinking (AL), Decomposition (DE) CS Domain - Algorithms and programming Tags - Iteration, IF conditions Decomposing problems into their constituent parts often leads to obvious function choices. Whenever loops are involved, it is very important to check the boundary data carefully. 30
Binary addition Juniors: Seniors: C B Intermediates: Elite: Here are six binary numbers: 100110111111100010111001010110110011100111111001 101111010011110110111100001110110110110000111110 100110100111110110011110000111001001100110111101 111100000110111111110110111110101111010111101000 111100000110111111101000111010001111010111111010 111101111111101001110001111000101110001011101101 Task: Find the sum of adding these six binary numbers and then converting the answer into base 10. (Enter your answer below as an integer.) Answer: 1350811866762947 Explanation: Although the task suggests binary addition, the task only requires an integer to be output. This task is therefore slightly easier than it would first appear. Some students in some programming languages may worry that they cannot input such large integers. However, this task is best approached by inputing the binary numbers as strings and casting. This is shown in the Python solution below. An alternative approach would be to enter the binary numbers provided as actual binary data types. In Python and Java, for example, this would be achieved by preceding each of the supplied numbers with 0b It’s Computer Science: CT Skills - Evaluation (EV) CS Domain - Data, data structures and representations Tags - Binary, Function Manipulating different data types and casting between them in programs is a very important important technique for programmers. Here we are asked to input one of the most basic data-types, binary numbers. Although a computer, at the lowest level, would add these in a series of binary operations, a programmer has the choice of either adding at a binary level or casting into more easily managed integers before doing the addition. 31
Is it safe? Juniors: Seniors: C C Intermediates: C Elite: Alexi was worried that he would forget the 4 figure numerical code for his safe. He decides to store it in plain view, in his social media profile, but in code: I'm Alexi. I love solving difficult problems. I enjoy Professor Layton games, programming, and chess. I have now turned my passion into a business: Do you have a difficult problem you cannot solve, need an algorithm for a particularly troublesome application you are writing or simply want an app made for you? Send me a direct message, detailing the job you would like me to do for you, and I will let you know, by return, if I can help. I charge for each task individually. Prices depend on how much time it will take me and how enjoyable the job sounds. Remember, I enjoy difficult problems rather than easy ones! If you are not sure what kind of problems I enjoy solving, you are already in the right place. Read my blog entries below where I discuss some of the more interesting tasks I have worked on. I look forward to hearing from you! To retrieve the safe code, all Alexi has to do is add the values of every letter in the text where: a=1, b=2, c=3, etc. Capital letters have the same values as their lowercase equivalent. All non-letter characters and spaces are ignored. Task: Write a program that finds the sum of the values of all the letters in Alexi's profile text and enter the answer below as a four digit integer. 32
Is it safe? Juniors: Seniors: C C Intermediates: C Elite: Answer: 7989 Explanation: This is a relative simple text manipulation problem that can be made simpler by taking the supplied string and converting all the letters to lowercase before working out the hidden number. One way of doing this would be to make an array of the letters of the alphabet and using their indexes to convert the letters to numbers. In Python, and in some other languages, a string can be iterated through in the same way and a string s a lot faster to type out! Here is a Python 3 solution: It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV) CS Domain - Algorithms and programming Tags - Iteration, IF conditions, Arrays Iterating through arrays, lists and strings is an important tool that programmers must at some time become comfortable doing. This problem uses the indexes of a container variable containing the letters of the alphabet as a convenient look up tool. 33
Parity check Juniors: Seniors: C C Intermediates: C Elite: What is an even 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 means of checking that all the data has arrived safely, before transmission the seven bits have an extra bit added to the original bits to complete the byte. The extra bit is chosen such that the sum of the bits in the byte add up to an even number: e.g. 0110100 becomes 10110100 1110111 becomes 01110111 If the data arrives safely, the sum of all the bits in every byte will be even. This indicates that the bytes have arrived safely. You have been asked to write a program (using any programming language) that does the following: - inputs the string below made of 201 binary bytes - counts the number of times a byte fails the even parity check. 01101111011110011111011001111000111100111110001001101111111000101110010101101100111001111110010011 11010011110110111100001110110110110000111110100110100111110110011110000111001001100110111101011110 00011110010001101001011010010110010101101001011110000110011001110010011011000111010011101110011011 11111101010110001101100110011100101111101011100111111110011111100111110011011011111111100101110001 01101100011000110110010111100001111001001110001011110011011011111110111001110010011101000110010101 11100001101111011001011111010111100111111000101111000001101111111101101111101011110101111010001111 00000110111111101000111010001111010111111010011101111111101001110001111000101110001011101101011000 11111010111110111001101100111100111110110101110001111000010111100011110011011011111111001111111010 11110101011000111110100011100001011000110110101001111000111110011110110111100010111010111110010001 10001111101011111100111110010011110000111011100110011001110001011001100110011011110110111011101110 10001110011101101001011110001111001111101011111100110110110011111010011100010110010111110011011000 11111000010110010101100011011010101110011111110011111100111110101101100011111110101110010011111001 01100011111100110111001001101001111100000111011111110110011011110110100111110011011100010110010111 11000001110001111101011110101111110011111110010110110011111010011000110111100001110001111001111110 11011111001101110111111100111111001101110100111010110110111111100010011010011111010111100010111101 10011101111111011011100111011101111111100101101010111010000110110011110110111010111111001111100001 1110110111101000111001000110011011100000 Task: Find the number of bytes that fail the even parity check. Enter this number as an integer below. 34
Parity check Juniors: Seniors: C C Intermediates: C Elite: Answer: 5 Explanation: Again, we have numbers stored as text. This string has to be iterated through and the characters could be cast as integers to perform the check. The important part here is performing a mod 2 calculation as a check for evenness. Here is a Python 3 solution: It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV) CS Domain - Communication and networking Tags - Iteration, IF conditions, Parity bit 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. 35
Unusual characters Juniors: Seniors: C Intermediates: Elite: There are many unusual characters in Alice in Wonderland such as ; " - and ! Unusual characters are those that are not uppercase or lowercase letters, full-stops, spaces or commas. Task: Write a program (using any programming language) that does the following: - imports the UTF-8 text file supplied. - searches for and counts the number of times unusual characters appear in the text. - outputs the number occurrences of unusual characters it finds. Enter the number of times unusual characters appear in the the text as an integer and press the Save button. Answer: 1367 Explanation: Once the text file is read into memory, there are many ways of solving this problem. Here is a Python 3 solution: 36
Unusual characters Juniors: Seniors: C Intermediates: Elite: It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Decomposition (DE), Evaluation (EV) CS Domain - Interactions, systems and society Tags - File handling, Iteration, If conditions Manipulating files and strings in programs is a very important skill that modern programmers need to be good at. 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 these facilities in the word processor in the first place. 37
Sorting numbers Juniors: Seniors: C Intermediates: Elite: Here is a list of comma separated integers: 276,458,304,453,481,576,854,954,803,219,167,813,560,91,342,591,519,812,888,451,434,540,29,212,609,952, 38,513,972,197,127,277,373,607,302,177,997,473,168,840,345,370,167,889,495,301,156,65,587,88,328,791, 508,87,799,587,289,45,318,258,804,153,311,794,162,904,268,766,910,862,181,159,329,813,492,74,222,415, 859,337,841,872,335,573,928,249,821,281,243,849,329,535,797,159,803,718,363,290,872,576,404,378,47,7, 461,133,348,660,764,967,12,900,478,275,107,102,227,580,953,956,964,229,789,800,82,408,551,963,532,667, 816,232,492,249,927,978,204,501,802,3,899,627,729,731,82,259,548,889,23,849 Task: Write a program to sort them in ascending order and output these numbers as a new list of comma separated integers. Copy and paste your list into the answer box and press Save. Answer: 3,7,12,23,29,38,45,47,65,74,82,82,87,88,91,102,107,127,133,153,156,159,159,162,167,167,168,177,181,197,2 04,212,219,222,227,229,232,243,249,249,258,259,268,275,276,277,281,289,290,301,302,304,311,318,328,32 9,329,335,337,342,345,348,363,370,373,378,404,408,415,434,451,453,458,461,473,478,481,492,492,495,501, 508,513,519,532,535,540,548,551,560,573,576,576,580,587,587,591,607,609,627,660,667,718,729,731,764,7 66,789,791,794,797,799,800,802,803,803,804,812,813,813,816,821,840,841,849,849,854,859,862,872,872,88 8,889,889,899,900,904,910,927,928,952,953,954,956,963,964,967,972,978,997 Explanation: Most languages have a sort function built in so using these is the quickest way to find a solution to this task. If we wanted to add a sorting algorithm of our own to our program, we have several to choose from. One is offered below: Here is a Python 3 solution that uses the built in sorted() function: 38
Sorting numbers Juniors: Seniors: C Intermediates: Elite: Explanation continued: Here is a Python 3 solution with a programmed sorting function: It’s Computer Science: CT Skills - Algorithmic Thinking (AL), Evaluation (EV) CS Domain - Algorithms and programming Tags - Sorting, Quick sort, Arrays, List In Computer Science there are few more important basic task requirements than that of sorting data. When the amount of data that needs sorting is large this can be a time consuming and memory hungry task for a computer. Large amounts of time and energy have been spent by researchers finding the most efficient sorting algorithms for different circumstances. These are then available to programmers that work in higher level languages who can simply call a built-in sort function. 39
STV Juniors: Seniors: C C Intermediates: Elite: Bebravia has introduced a single transferrable vote (STV) system for its presidential elections. This year there are four candidates: Alfred, Bertha, Cleo and Dafid Each voter ranks their top two candidates on their ballot papers. e.g. So this voter has chosen Cleo as their first choice and Alfred as their second choice. The votes are stored as an array of strings: ['2010','2001','0201','0210','0021','0021','1020','2100','1002','2010','0021','0210','1002','0201','2010','2001', '0201','0210','0021','0021','1020','2100','1002','2010','0021','0210','1002','0201','0012','0210','0201','1002', '1002','1200','0120','1020','1020','0201','0012','2100','0021','0102','0210','0012','2010','2001','2100','2100', '1020','2010','2100','0012','2100','2001','2010','2001','0201','0210','0021','0021','1020','2100','1002','2010', '0021','0210','1002','0201','2010','2001','0201','0210','0021','0021','1020','2100','1002','2010','0021','0210', '1002','0201','0012','0210','0201','1002','1002','1200','0120','1020','1020','0201','0012','2100','0021','0102', '0210','0012','2010','2001','2100','2100','1020','2010','2100','0012','2100','2001','2001','2001','0021','0120', '1020','0201','0012','2001','2100','0102','0021','1020','2100','1002','2001','0012','0201','2001','1020','0012', '0210','0201','1002','1002','1200','0120','1020','1020','0201','0012','2100','0021','0102','0210','0012','2010', '2001','2100','2100','1020','2010','2100','0012','2100','2001','2001','2001','0021','0120','1020','0201','0012', '2001','2100','0102','0021','1020','2100','1002','2001','0012','0201','2001','1020','2001','2001','0021','0120', '1020','0201','0012','2001','2100','0102','0021','1020','2100','1002','2001','0012','0201','2001','1020''0012', '0210','0201','1002','1002','1200','0120','1020','1020','0201','0012','2100','0021','0102','0210','0012','2010', '2001','2100','2100','1020','2010','2100','0012','2100','2001','2001','2001','0021','0120','1020','0201','0012', '2001','2100','0102','0021','1020','2100','1002','2001','0012','0201','2001','1020'] The STV system used in Bebravia: Round 1: All first choices are counted. The candidate with the lowest count is eliminated. Round 2: All voters who placed the eliminated candidate first have their second choices added to the first vote count. The winner is the candidate with the highest count after rounds one and two. Task: Find the total number of votes the winner scored after both rounds of counting are complete. 40
STV Juniors: Seniors: C C Intermediates: Elite: Answer: Explanation: 88 Here is a possible solution in Python 3: 41
STV Juniors: Seniors: C C Intermediates: Elite: Explanation continued: This was the most complex task in the 2018 Official Challenge. The task provides a complicated algorithm for a single transferrable voting system. The problem is how to implement it, find the winner and keep count of the votes. There are as many ways of doing this as there are ways to mow a lawn. One solution is provided, with comments and clearly named functions, on the previous page. Note how the task was decomposed into smaller functions to make it easier to program and to check. In these sort of tasks it is important to be sure that the program is doing what is expected. One way of doing this is to print out more information than is required; enough to be able to check what is going on and that the final answer is correct. It’s Computer Science: CT Skills - Abstraction (AB), Algorithmic Thinking (AL), Decomposition (DE) CS Domain - Interactions, systems and society Tags - Single Transferable Vote, Arrays, Lists Sometimes programmers have to solve simple tasks, other times they have to solve more challenging algorithms. This task involves solving several intertwined smaller algorithms. 42
Sponsors UK Bebras is free to enter thanks to the support and sponsorship of the following organisations. Oxford University is the Organising Body of this Challenge. 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. TCS offers a consulting-led, integrated portfolio of IT, BPS,infrastructure, engineering and assurance services. A part of the Tata group, India’s largest industrial conglomerate, TCS has over 389,000 of the world’s best-trained consultants in 46 countries. In the UK, TCS is one of the country’s leading digital employers with offices in more than 30 locations, including London, Edinburgh, Manchester, 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 IT Services sector by analyst firm Whitelane for four consecutive years, and has been recognised as one of the UK’s top employers by the Top Employers Institute, one of the UK’s leading employers for women in The Times Top 50 Employers for Women and Company of the Year at the Employee Engagement Awards. For more information, visit us at www.tcs.com or follow @TCS_UKI on Twitter. 43
Copyright 2018 TCS Oxford Computing Challenge – Licence: CC-BY-NC-SA 4.0 44
You can also read