Quick Reference

Use this as a reminder after you have met an idea in its unit. Short answers are worked by hand, so most of this card is ACSL's notation and its traps. Every programming answer reads its input with input() and prints exactly the answer, with nothing extra.

Reading contest input · Unit 0

parts = input().split()     # one line, split into pieces
n = int(parts[0])           # a piece is text: convert it
name = parts[1]             # keep a word as a string

values = []
for piece in input().split():   # N values on one line
    values.append(int(piece))

count = 0
value = int(input())        # read before the loop ...
while value != 0:           # ... until the 0 that ends the input
    count = count + 1
    value = int(input())    # ... and again at the end of the loop

A tuple keeps two values together: best = (a, b), and a, b = best unpacks it. To try every pair of positions once, start the inner loop at i + 1.

Number bases · Unit 1

Base Digits Place values, from the right
2 0 1 1, 2, 4, 8, 16, 32, …
8 0 to 7 1, 8, 64, 512, …
16 0 to 9, A to F 1, 16, 256, 4096, …

Recursive functions · Unit 2

f(x) = { f(x - 3) + x      if x > 10
       { 2 * x - 5         if x <= 10

ACSL pseudocode · Unit 3

ACSL Python Watch out
/ / real division: 7 / 2 is 3.5
int(x) math.floor(x) rounds down: int(-3.7) is -4
int(a / b) a // b for whole numbers a and b
^ or ↑ ** 2 ^ 5 is 32
sqrt(x) math.sqrt(x) gives a float
ELSE IF elif only the first true branch runs
A(3), A(r, c) A[3], A[r][c] round brackets for arrays

ACSL's !, && and || are Python's not, and and or; write not (…) with brackets. ACSL's order: !, then ^, then *, / and %, then + and -, then the comparisons, then &&, and || last. Python puts not after the comparisons, so add brackets when you translate.

FOR includes its end value; range stops one step before its end:

ACSL Python Values
FOR k = 1 TO 9 STEP 2 range(1, 10, 2) 1, 3, 5, 7, 9
FOR k = 20 TO 2 STEP -6 range(20, 1, -6) 20, 14, 8, 2

Substrings, with positions from 0, for S = "ELEPHANT":

ACSL Means Gives Python
S[:n] the first n characters S[:2] is EL S[:n]
S[n:] the last n characters S[3:] is ANT S[len(S) - n:]
S[a:b] positions a through b S[1:3] is LEP S[a:b + 1]

When a question shows Python, use Python's rules; when it shows ACSL pseudocode, use ACSL's.

Prefix, infix and postfix · Unit 4

Notation 3 + 4 is written
infix 3 + 4
prefix + 3 4
postfix 3 4 +

Bit-string flicking · Unit 5

Operator What it does
NOT (~) flips every bit
AND (&) 1 where both bits are 1
OR 1 where at least one bit is 1
XOR (⊕) 1 where the bits differ
LSHIFT-x, RSHIFT-x move x places; the bits that fall off are lost, and zeros come in
LCIRC-x, RCIRC-x move x places; the bits that fall off come back in at the other end

Order, first to last: brackets, NOT, then the shifts and circulates, then AND, then XOR, then OR. Two-string operators of the same level go left to right; one-string operators go right to left, the one nearest the string first.

Loops in pseudocode · Unit 6

LISP · Unit 7

Statement Value
(ADD 1 4 2), (MULT 2 3 4) 7, 24 (any number of arguments)
(SUB 3 10), (DIV 30 4) -7, 7.5
(SQUARE 3), (EXP 2 4) 9, 16
(EQ 2 2), (POS 0), (NEG -7) true, NIL, true
(CAR '(A B C)) A, the first item
(CDR '(A B C)) (B C), the list without its first item
(CONS 'A '(B C)) (A B C)
(REVERSE '((1 2) 3)) (3 (1 2))
(ATOM 'A), (ATOM '(A)) true, NIL
(SETQ X 5), (SET 'X 5) 5, and X now holds 5
(EVAL '(ADD 3 4)) 7
(DEF F (L) (CAR L)) defines F; DEFUN is the same

Boolean algebra · Unit 8

This book ACSL paper Meaning
~A A‾\overline{A} NOT
A * B ABAB AND
A + B A+BA + B OR
A ⊕ B A⊕BA \oplus B XOR: 1 when different
A ⊙ B A⊙BA \odot B XNOR: 1 when the same

Order, first to last: brackets, ~, *, then ⊕ and ⊙, then +; the same level goes left to right. A long bar is a bracket: AB‾\overline{AB} is ~(A * B), but A‾ B‾\overline{A}\,\overline{B} is ~A * ~B.

Data structures · Unit 9

Structure PUSH(x) X = POP()
stack puts x on the top takes the top item (the newest)
queue puts x at the back takes the front item (the oldest)

POP() on an empty stack or queue gives NIL.

Arrays · Unit 10

FSAs and regular expressions · Unit 11

Symbol Matches
x* zero or more x
x+ one or more x
x? zero or one x
. any one character
[abc], [a-z] one character listed, or in the range
[^abc] one character not listed

One string that fits one expression and not the other shows that two expressions are not equivalent.

Graph theory · Unit 12

Digital electronics · Unit 13

Gate Output is 1 when Expression
BUFFER the input is 1 A
NOT the input is 0 ~A
AND both inputs are 1 A * B
NAND not both are 1 ~(A * B)
OR at least one is 1 A + B
NOR both are 0 ~(A + B)
XOR the inputs differ A ⊕ B
XNOR the inputs are the same A ⊙ B
INPUTS A B C
p = NAND(A, B)
out = OR(p, C)

Strings in pseudocode · Unit 14

Assembly language · Unit 15

Opcode What it does
DC the label names a location holding LOC
LOAD, STORE copy LOC into ACC; copy ACC into LOC
ADD, SUB, MULT ACC becomes ACC + LOC, − LOC, × LOC
DIV ACC ÷ LOC, rounded toward zero
BE, BG, BL jump to the label LOC if ACC = 0, > 0, < 0
BU always jump
READ, PRINT read a number into LOC; print LOC
END stop