Contest Python: ACSL › Unit 7 — LISP

Exercises: Unit 7 — LISP

There are two kinds of exercises here, just as on an ACSL contest.

  • Short-answer: work out the value with pencil and paper and write one exact answer.
    • Write a number as a whole number when it is whole (-3, not -3.0) and as a decimal otherwise (7.5).
    • Write a list with single spaces and round brackets, in the same capital letters as the question: (A (B C) D).
    • Write the empty list, and "no", as NIL; write "yes" as true.
    • Leave out the quote mark: the value of '(A B) is (A B).
  • Programming: write a complete program that reads the input with input() and prints exactly the output described. Save it as a .py file and run it from a terminal, typing the sample input or feeding it a file. Build new lists with loops and append, as in the lesson.

LISP is on the Intermediate and Senior papers only, so each exercise is marked Intermediate or Senior: the lowest division it belongs to. Do every exercise at or below your division. Exercises marked Challenge are extra practice for when you want more.

Exercise 1: Arithmetic Warm-Up

  • Division: Intermediate

Evaluate the following expression.

(ADD (MULT 3 4) (SUB 10 7) (SQUARE 2))

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Exercise 2: Symbols and Decimals

  • Division: Intermediate

Evaluate the following expression.

(/ (+ 9 (* 2 6) 6) (- 10 2))

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Exercise 3: Powers and Negatives

  • Division: Intermediate

Evaluate the following expression.

(MULT (SUB 3 (EXP 2 3)) (DIV 12 (SUB 1 5)) 2)

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Exercise 4: One-Call Calculator

  • Division: Intermediate

The input is one line holding a single LISP statement whose arguments are all whole numbers, such as (DIV 54 4). The function is one of these:

  • ADD or +, and MULT or *, with 2 to 10 arguments
  • SUB or -, and DIV or /, with exactly 2 arguments
  • SQUARE, with 1 argument
  • EXP, with 2 arguments a n, giving a to the power n

Print the value of the statement. Print a whole number without a decimal point; print any other value as a decimal.

Constraints

  • The line starts with (, ends with ), and has exactly one space between items.
  • Every argument is between -1000 and 1000.
  • For EXP, n is between 0 and 10, and the value is between -1000000000 and 1000000000.
  • For DIV and /, the second argument is not 0, and the quotient is either a whole number or a decimal with at most 3 digits after the point.

Sample Input

(DIV 54 4)

Sample Output

13.5

The input (- 6 9) prints -3, and (DIV 12 -4) prints -3 as well.

How this is checked: Checked by running your program on 13 test inputs (1 sample, 12 hidden) and comparing its output line by line.

For parents and teachers: report a problem

Exercise 5: Equal or Not?

  • Division: Intermediate

Evaluate the following expression.

(EQ (ADD 4 7) (SUB 9 -2))

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: `true` or `NIL`, spelled as this book writes them: `true` in small letters, `NIL` in capitals.

For parents and teachers: report a problem

Exercise 6: Is It Positive?

  • Division: Intermediate

Evaluate the following expression.

(POS (DIV (SUB 3 8) (MULT -2 5)))

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: `true` or `NIL`, spelled as this book writes them: `true` in small letters, `NIL` in capitals.

For parents and teachers: report a problem

Exercise 7: Is It Negative?

  • Division: Intermediate

Evaluate the following expression.

(NEG (SUB (SQUARE 4) 15))

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: `true` or `NIL`, spelled as this book writes them: `true` in small letters, `NIL` in capitals.

For parents and teachers: report a problem

Exercise 8: Moon and Stars

  • Division: Intermediate

Evaluate the following expression.

(CAR (CDR '(SUN (MOON STAR) SKY)))

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A LISP list: round brackets, single spaces between items, the same capital letters as the question, and no quote mark, as in `(A (B C) D)`.

For parents and teachers: report a problem

Exercise 9: One Left Over

  • Division: Intermediate

Evaluate the following expression.

(CONS 'X (CDR (CAR '((LEFT) RIGHT))))

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A LISP list: round brackets, single spaces between items, the same capital letters as the question, and no quote mark, as in `(A (B C) D)`.

For parents and teachers: report a problem

Exercise 10: The Third Item

  • Division: Intermediate

Evaluate the following expression.

(CADDR '(RED (GREEN) (BLUE PINK) GOLD))

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A LISP list: round brackets, single spaces between items, the same capital letters as the question, and no quote mark, as in `(A (B C) D)`.

For parents and teachers: report a problem

Exercise 11: Atom or List?

  • Division: Intermediate

Evaluate the following expression.

(ATOM (CAR (CDDR '(A (B) (C D)))))

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: `true` or `NIL`, spelled as this book writes them: `true` in small letters, `NIL` in capitals.

For parents and teachers: report a problem

Exercise 12: Build, Then Turn Around

  • Division: Intermediate

Evaluate the following expression.

(REVERSE (CONS '(1 2) (CDR '(3 4 5))))

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A LISP list: round brackets, single spaces between items, the same capital letters as the question, and no quote mark, as in `(A (B C) D)`.

For parents and teachers: report a problem

Exercise 13: REVERSE in Python

  • Division: Intermediate

The input is one line holding a LISP list of atoms, such as (A B C D). Store its atoms in a Python list. Print two lines:

  1. the value of (REVERSE L), where L is the list
  2. the value of (REVERSE (CDR L))

Print each list the LISP way, with round brackets and single spaces, and print an empty list as NIL.

Constraints

  • The list has between 1 and 20 atoms.
  • Each atom is a word of capital letters or a whole number, with no brackets inside it.
  • The line starts with (, ends with ), and has exactly one space between atoms.

Sample Input

(A B C D)

Sample Output

(D C B A)
(D C B)

For the input (SOLO), the output is (SOLO) and then NIL.

How this is checked: Checked by running your program on 4 test inputs (1 sample, 3 hidden) and comparing its output line by line.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 14: CONS in Python

  • Division: Intermediate

The first line of input holds one atom X. The second line holds a LISP list of atoms, L, such as (B C). Store the atoms of L in a Python list. Print two lines:

  1. the value of (CONS X L)
  2. the value of (CONS (CAR L) (CONS X (CDR L)))

Print each list the LISP way, with round brackets and single spaces.

Constraints

  • X is a word of capital letters or a whole number, with no brackets inside it.
  • L has between 1 and 20 atoms, each of the same form as X.
  • The second line starts with (, ends with ), and has exactly one space between atoms.

Sample Input

Z
(B C)

Sample Output

(Z B C)
(B Z C)

How this is checked: Checked by running your program on 3 test inputs (1 sample, 2 hidden) and comparing its output line by line.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 15: Stored, Then Worked Out

  • Division: Intermediate

Consider the following program.

(SETQ A '(SUB 20 (MULT 3 4)))
(SETQ B (EVAL A))
(ADD B (EVAL A) (SQUARE B))

What is the value of the final statement?

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Exercise 16: A Function Calls a Function

  • Division: Intermediate

Consider the following program.

(DEF TWICE (N) (MULT 2 N))
(DEF F (A B) (SUB (TWICE A) B))
(F (F 5 3) 4)

What is the value of the final statement?

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Exercise 17: Resolved Twice

  • Division: Senior

Consider the following program.

(SET 'X 5)
(SETQ Y '(X 7))
(SET 'Z (CAR Y))
(MULT (EVAL Z) (CADR Y) 2)

What is the value of the final statement?

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Exercise 18: Rotate Twice

  • Division: Senior

Consider the following program.

(DEFUN ROT (L) (CONS (CAR (REVERSE L)) (REVERSE (CDR (REVERSE L)))))
(ROT (ROT '(1 2 3 4)))

What is the value of the final statement?

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A LISP list: round brackets, single spaces between items, the same capital letters as the question, and no quote mark, as in `(A (B C) D)`.

For parents and teachers: report a problem

Exercise 19: Four Shortcuts

  • Division: Senior

Consider the following program.

(SETQ M '((1 2 3) (4 5) 6))
(ADD (CAAR M) (CADR (CADR M)) (CAR (CDDAR M)) (CAR (CDAR M)))

What is the value of the final statement?

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Exercise 20: Shortcuts in Python

  • Challenge
  • Division: Senior

The first line of input holds the name of one of these functions: CAR, CDR, CADR, CDDR or CADDR. The second line holds a LISP list of atoms, L, such as (P Q R S). Print the value of the function applied to L.

The value is either an atom, which you print as it is, or a list, which you print the LISP way, with round brackets and single spaces. Print an empty list as NIL.

Work through the letters between the C and the R from right to left, as LISP does, rather than writing a separate case for each name.

Constraints

  • L has between 1 and 20 atoms.
  • Each atom is a word of capital letters or a whole number, with no brackets inside it.
  • The second line starts with (, ends with ), and has exactly one space between atoms.
  • The list is always long enough: every A step is taken on a list with at least one atom.

Sample Input

CDDR
(P Q R S)

Sample Output

(R S)

For the same list, CADDR prints R, and for the list (P Q), CDDR prints NIL.

How this is checked: Checked by running your program on 7 test inputs (1 sample, 6 hidden) and comparing its output line by line.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 21: Sum of Squares, Halved

  • Challenge
  • Division: Senior

Consider the following program.

(DEFUN SUMSQ (A B) (+ (SQUARE A) (SQUARE B)))
(SETQ P (REVERSE '(2 (1 3) 4)))
(DIV (SUMSQ (CAR P) (CADR (CADR P))) (CADDR P))

What is the value of the final statement?

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Back to Unit 7 — LISP