Programming techniques and object-oriented programming · A level · AQA 7517 4.1.1.3, Eduqas A500QS 1.4 · about 20 min
Integer division and MOD with negatives, rounding and truncation, XOR, string and date conversions, and pseudo-random numbers.
[1 mark]What does this program print?
print(-17 // 5, -17 % 5)
-4 3
Python's integer division rounds down to -4, and the remainder takes the divisor's sign: 5 * -4 + 3 is -17.
[1 mark]What does this program print?
import math x = -2.6 print(round(x), int(x), math.floor(x))
-3 -2 -3
Rounding gives the nearest whole number, -3; truncation removes the fraction, -2; floor rounds down, -3.
[1 mark]What is True XOR True?
[1 mark]What does this program print?
s = "R135,F20"
comma = s.find(",")
print(comma, int(s[1:comma]) + 1, s[comma + 1:] + "!")4 136 F20!
The comma is at index 4, the substring from index 1 to before 4 is "135", and the rest of the string is "F20".
[1 mark]What does this program print?
heading = 30
for turn in [-90, -45, 200]:
heading = (heading + turn) % 360
print(heading)300 255 95
30 - 90 is -60, which MOD 360 gives 300; then 255; then 455 MOD 360 is 95.
[1 mark]Why does setting the same seed before generating numbers give the same sequence each time?
A route arrives as one string of commands separated by commas: COMMANDS = "F20,L90,F15,R135,B5". Each command is a letter and a whole number: F drives forward that many cm, B drives backward that many cm, R turns right that many degrees and L turns left that many degrees.
Go through the string and carry out each command on the robot. Keep two totals as you go: the distance driven in cm (forward and backward both add) and the heading in degrees, starting at 0, turning right adding and turning left subtracting, always kept from 0 to 359 with MOD 360. Convert each amount with int. At the end print driven <cm> cm, heading <degrees>, for this string driven 40 cm, heading 45. Work both totals out from the string; do not type them.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
COMMANDS = "F20,L90,F15,R135,B5"
for command in COMMANDS.split(","):
print(command)The hint students can ask for: Each command is one letter followed by a number. Separate the commands, then take the first character as the letter and everything after it as the amount. Distances add up whichever way you drive; turns change the heading, and MOD keeps it in range.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
COMMANDS = "F20,L90,F15,R135,B5"
driven = 0
bearing = 0
for command in COMMANDS.split(","):
letter = command[0]
amount = int(command[1:])
if letter == "F":
forward(50, distance=amount)
driven = driven + amount
elif letter == "B":
backward(50, distance=amount)
driven = driven + amount
elif letter == "R":
turn_right(30, angle=amount)
bearing = (bearing + amount) % 360
elif letter == "L":
turn_left(30, angle=amount)
bearing = (bearing - amount) % 360
print(f"driven {driven} cm, heading {bearing}")
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.