Edexcel GCSE Computer Science sample Paper 2, Question 6: the login search

Pearson Edexcel 1CP2/02 sample assessment material, Question 6: write a Python program that validates a four-digit passcode, then does a linear search of a two-dimensional list sorted by login name, stopping when the record's place is passed. A model answer and the fifteen marks.

Past paper questionPearson 1CP2/02Sample Paper 215 marksWrite a program

Question 6 is the last question of Pearson's sample assessment material for Edexcel GCSE Computer Science Paper 2 (1CP2/02): 15 marks for a program written from requirements. It is a linear search of a sorted two-dimensional list, with validation in front of it and an early stop.

We do not copy the paper or Pearson's code files here. Open the paper beside this page: Edexcel 1CP2/02 sample assessment materials (PDF). The mark scheme is in the same document.

The question in short

Users are stored in a two-dimensional list. Each record holds a user number, last name, first name, login name and passcode. The list is sorted by login name. Passcodes are integers from 1000 to 9999.

  • Inputs: a login name (no validation) and a four-digit passcode, which must be from 1000 to 9999 inclusive.
  • Process: a linear search for the record with the right login name and passcode, working for any number of users, and stopping when the place where the record should be has been passed. If you are looking for Jam118 and reach Joy116, it is not there.
  • Outputs: a welcome message with the user's first and last names, or an invalid input message.

A model answer

loginName = input("Login name: ")
passcode = int(input("Passcode: "))

while passcode < 1000 or passcode > 9999:
    passcode = int(input("Passcode must be 1000 to 9999: "))

# search the sorted list until found, or until past where it would be
found = False
passed = False
index = 0
while index < len(users) and not found and not passed:
    if users[index][3] == loginName and users[index][4] == passcode:
        found = True
    elif users[index][3] > loginName:
        passed = True
    else:
        index = index + 1

if found:
    print("Welcome", users[index][2], users[index][1])
else:
    print("Invalid login name or passcode")

Where the fifteen marks are

Six single marks: accepting and responding to the input; a range check with >= 1000 and <= 9999; len() used so that it works for any number of users; a Boolean to stop the loop when found or passed; two-dimensional indexing into the list; and appropriate messages. Then up to 3 each for solution design, good programming practices and functionality.

Where the marks are lost

  • Searching to the end every time. The list is sorted, and the requirement says stop once the place has been passed. A for loop over the whole list misses that.
  • Comparing the passcode as a string. The list holds integers. Convert the input.
  • The wrong columns. Login name is at index 3 and passcode at 4, with first name at 2 and last name at 1. Count from 0.
  • "Welcome" with no name. The message must include the first and last names.
  • Hard-coding 19. len(users).

Run it

Four users stand in for the paper's nineteen. Try a real login, a wrong passcode, and a name that is past where it would be.

Jam118 with 4321 is welcomed as James Miller. Jam118 with 1111 is invalid. Jaz200 stops at Joy116 without reaching the end.
The program
from bugbot import *
connect()

# user number, last name, first name, login name, passcode. Sorted by login name
users = [[1, "Ahmed", "Ada", "Ada101", 2468],
         [2, "Miller", "James", "Jam118", 4321],
         [3, "Okoro", "Joy", "Joy116", 9876],
         [4, "Zhang", "Zoe", "Zoe140", 1357]]

loginName = input("Login name: ")
passcode = int(input("Passcode: "))

while passcode < 1000 or passcode > 9999:
    passcode = int(input("Passcode must be 1000 to 9999: "))

found = False
passed = False
index = 0
while index < len(users) and not found and not passed:
    print("  checking", users[index][3])
    if users[index][3] == loginName and users[index][4] == passcode:
        found = True
    elif users[index][3] > loginName:
        passed = True
    else:
        index = index + 1

if found:
    led("green")
    print("Welcome", users[index][2], users[index][1])
else:
    led("red")
    print("Invalid login name or passcode")
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

What is the answer to Edexcel sample Paper 2 Question 6?

Input the login name and passcode, and loop until the passcode is from 1000 to 9999. Search the list with a while loop that stops when the record matches, when a login name greater than the one entered is reached, or at the end. Print a welcome with the names, or an invalid message.

How can a search stop early in a sorted list?

When the current item is greater than the target, the target cannot come later, so the search ends without checking the rest.

What is two-dimensional indexing?

Using two index numbers to reach a value in a list of lists: users[index][3] is field 3 of record number index.

More from this paper

Every Edexcel 1CP2 question we have worked · Guide: Linear search and binary search explained

Learn it step by step

  1. F5.5 Linear search Algorithms
  2. F3.5 Two-dimensional arrays Strings, lists and records
  3. F6.2 Authentication Robust programs
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.