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.

Past paper questionPearson 1CP2/02June 2022 Paper 215 marksRearrange the lines

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:

  1. In a subprogram, the local variables are initialised first, all together.
  2. The loop comes after them.
  3. Inside the loop, a value must be made before it is used: get the digit, work out its value, add it to the total, then double the multiplier.
  4. return is the last line of the subprogram.
  5. In the main program: input first, then the while loop, 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.

1100 gives 12 and 10101010 gives 170. Press Enter on an empty line to finish.
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: ")
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

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

Every Edexcel 1CP2 question we have worked · Guide: Two's complement explained

Learn it step by step

  1. F8.2 Binary and denary Data representation
  2. F4.2 Parameters and return values Functions and structured code
  3. F2.6 Condition-controlled loops: while Decisions and loops
Open the lessons

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.