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" astrue. - Leave out the quote mark: the value of
'(A B)is(A B).
- Write a number as a whole number when it is whole (
- Programming: write a complete program that reads the input with
input()and prints exactly the output described. Save it as a.pyfile and run it from a terminal, typing the sample input or feeding it a file. Build new lists with loops andappend, 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
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
Exercise 2: Symbols and Decimals
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
Exercise 3: Powers and Negatives
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
Exercise 4: One-Call Calculator
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:
ADDor+, andMULTor*, with 2 to 10 argumentsSUBor-, andDIVor/, with exactly 2 argumentsSQUARE, with 1 argumentEXP, with 2 argumentsa n, givingato the powern
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,nis between 0 and 10, and the value is between -1000000000 and 1000000000. - For
DIVand/, 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.
Exercise 5: Equal or Not?
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.
Exercise 6: Is It Positive?
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.
Exercise 7: Is It Negative?
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.
Exercise 8: Moon and Stars
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)`.
Exercise 9: One Left Over
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)`.
Exercise 10: The Third Item
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)`.
Exercise 11: Atom or List?
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.
Exercise 12: Build, Then Turn Around
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)`.
Exercise 13: REVERSE in Python
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:
- the value of
(REVERSE L), whereLis the list - 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.
Exercise 14: CONS in Python
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:
- the value of
(CONS X L) - the value of
(CONS (CAR L) (CONS X (CDR L)))
Print each list the LISP way, with round brackets and single spaces.
Constraints
Xis a word of capital letters or a whole number, with no brackets inside it.Lhas between 1 and 20 atoms, each of the same form asX.- 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.
Exercise 15: Stored, Then Worked Out
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
Exercise 16: A Function Calls a Function
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
Exercise 17: Resolved Twice
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
Exercise 18: Rotate Twice
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)`.
Exercise 19: Four Shortcuts
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
Exercise 20: Shortcuts in Python
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
Lhas 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
Astep 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.
Exercise 21: Sum of Squares, Halved
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