Edexcel GCSE Computer Science June 2022 Paper 2, Question 4: binary to denary, lines mixed up
Pearson Edexcel 1CP2/02 June 2022, Question 4: put the mixed-up lines of a Python program in order so that it converts a binary pattern to denary with a doubling multiplier, looping until an empty input. The fifteen marks explained, and the program to run.
Question 4 of the Pearson Edexcel GCSE Computer Science Paper 2 from the June 2022 series (1CP2/02) is a 15 mark puzzle. Every line of the program is given to you, already indented, in the wrong order. You rearrange them until it works.
We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2022 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.
The question in short
The user types a binary pattern. The program works through it from right to left with a multiplier that doubles each time (1, 2, 4, 8 ...). Where there is a 1, the place value is added to a running total. The program keeps asking for patterns until the user enters nothing. So 1100 gives 12 and 10101010 gives 170.
How to order mixed-up lines
Use the indentation. It has been kept, so it tells you which lines are inside the subprogram, which are inside a loop, and which are in the main program.
Then use these rules, which are what the mark scheme rewards:
- In a subprogram, the local variables are initialised first, all together.
- The loop comes after them.
- Inside the loop, a value must be made before it is used: get the
digit, work out itsvalue, add it to thetotal, then double themultiplier. returnis the last line of the subprogram.- In the main program: input first, then the
whileloop, then the call, then the output, then the input again as the last line inside the loop.
A finished program
def binaryLoop(pattern):
multiplier = 1
total = 0
digit = ""
value = 0
for index in range(len(pattern) - 1, -1, -1):
digit = pattern[index]
value = int(digit) * multiplier
total = total + value
multiplier = multiplier * 2
return total
binary = input("Enter a binary pattern: ")
while binary != "":
result = binaryLoop(binary)
print(result)
binary = input("Enter a binary pattern: ")
Where the fifteen marks are
Eight are for the subprogram: two for the initialisations coming first, one for the loop, one each for digit, value, total and multiplier in that order, and one for return last.
Five are for the main program: input first; the while loop after it; the call inside the loop; the output after the call; the second input last.
One is for working with the test data, and one is for keeping the indentation accurate. Marks are for sequence only, and none are given for a section where no lines were moved.
Where the marks are lost
- Doubling the multiplier before using it. Then the right-hand digit is worth 2, and every answer is doubled.
- The second input outside the loop. The pattern never changes and the loop runs for ever.
- Changing the indentation. It was right when you got it. Moving lines with copy and paste keeps it. Retyping does not.
- Not running it. This is an on-screen paper. Run it with 1100 after every few moves.
Run it
It prints each step, so you can watch the multiplier double. Enter an empty line to stop.
The program
from bugbot import *
connect()
def binaryLoop(pattern):
multiplier = 1
total = 0
digit = ""
value = 0
for index in range(len(pattern) - 1, -1, -1):
digit = pattern[index]
value = int(digit) * multiplier
total = total + value
print(" digit", digit, "x", multiplier, "-> total", total)
multiplier = multiplier * 2
return total
binary = input("Enter a binary pattern: ")
while binary != "":
result = binaryLoop(binary)
print(result)
binary = input("Enter a binary pattern: ")
Questions
How do I answer a "rearrange the lines" question?
Group the lines by their indentation. Put initialisations first, loops after them, and inside each loop make every value before the line that uses it. Put return last in a subprogram. Run the program after each change.
How do you convert binary to denary in Python?
Work from the right-hand digit with a multiplier that starts at 1 and doubles each time. Add the multiplier to a total wherever the digit is 1. Or, outside the exam, use int(pattern, 2).
Why does the program ask for input twice?
Once before the loop, so that the while condition has something to test, and once at the end of the loop, to get the next pattern. It is the standard shape for a loop that ends on an empty input.
More from this paper
- Question 1: Turn a number from 5 to 30 into a letter with chr(), with a range check 10 marks
- Question 2: Fix ten errors in a turtle graphics program 10 marks
- Question 3: The area of card left when a circle is cut from a square 10 marks
- Question 5: Write a list to a CSV file, seven values to a line 15 marks
- Question 6: A linear search of a sorted 2D list that stops early and suggests a word 15 marks
Every Edexcel 1CP2 question we have worked · Guide: Two's complement explained
Learn it step by step
- F8.2 Binary and denary Data representation
- F4.2 Parameters and return values Functions and structured code
- F2.6 Condition-controlled loops: while Decisions and loops
This is our own explanation of a published exam question. It is not written or endorsed by Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.