Data representation · A level · OCR H446 1.3.1, AQA 7517 4.5.6.9 · about 40 min
Run length and dictionary coding, the Caesar and Vernam ciphers, symmetric and asymmetric encryption, and hashing.
[1 mark]A program's source code is compressed to send it by email. Which kind of compression must be used, and why?
[1 mark]What does this program print?
row = "AAAABBBCCD"
out = ""
i = 0
while i < len(row):
j = i
while j < len(row) and row[j] == row[i]:
j = j + 1
out = out + str(j - i) + row[i]
i = j
print(out)4A3B2C1D
Run length encoding replaces each run with its length and the repeated value.
[1 mark]Which conditions must a Vernam cipher key meet for perfect security?
Tick every answer that is true.
[1 mark]What does it mean for a cipher to be computationally secure?
[1 mark]Why are passwords stored as hashes rather than encrypted?
[1 mark]Alice wants to send Bob a message using asymmetric encryption. Which key does she encrypt it with?
route is a string of words separated by single spaces: FWD <cm> and LEFT <degrees> commands.
1. Write encode(text). The parameter text is a string of words separated by single spaces. It returns a pair: a list of the different words in the order they first appear (the dictionary), and a string of each word's index in that list, separated by single spaces.
2. Write decode(dictionary, encoded), which takes those two values and returns the original string.
3. Print dictionary: <the words separated by spaces>, then encoded: <the encoded string>, then original: <n> characters, encoded: <m> characters using len of route and of the encoded string, then decoded matches: <True or False> comparing the decoded string with route.
4. Drive the decoded route: FWD n is forward(50, distance=n) and LEFT n is turn_left(angle=n), where n is the number after the word.
Build the encoded string with your function; do not type it in.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
route = "FWD 20 LEFT 90 FWD 20 LEFT 90 FWD 20 LEFT 90 FWD 20 LEFT 90"
def encode(text):
dictionary = []
codes = []
return dictionary, " ".join(codes)
def decode(dictionary, encoded):
return ""The hint students can ask for: Walk the words in order. The first time you meet a word, add it to the end of the dictionary; every time, write down its position in the dictionary. Decoding looks each number up again. Drive from the decoded words, not the original string.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
route = "FWD 20 LEFT 90 FWD 20 LEFT 90 FWD 20 LEFT 90 FWD 20 LEFT 90"
def encode(text):
dictionary = []
codes = []
for word in text.split(" "):
if word not in dictionary:
dictionary.append(word)
codes.append(str(dictionary.index(word)))
return dictionary, " ".join(codes)
def decode(dictionary, encoded):
return " ".join(dictionary[int(code)] for code in encoded.split(" "))
dictionary, encoded = encode(route)
print("dictionary:", " ".join(dictionary))
print("encoded:", encoded)
print("original:", len(route), "characters, encoded:", len(encoded), "characters")
plain = decode(dictionary, encoded)
print("decoded matches:", plain == route)
words = plain.split(" ")
for i in range(0, len(words), 2):
if words[i] == "FWD":
forward(50, distance=int(words[i + 1]))
elif words[i] == "LEFT":
turn_left(angle=int(words[i + 1]))
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.