Docsity

Helper Function - Introduction to Symbolic Programming - Exams, Exams of Computer Programming

Partial preview of the text

Download Helper Function - Introduction to Symbolic Programming - Exams and more Exams Computer Programming in PDF only on Docsity!

CS 3, MIDTERM

Professor Dan Garcia

Fall Semester 2002

Test given 17 October 2002.

Exam topic: Recursion

NOTE:

  • There were two parts to this exam.
  • Part 1, worth 40 points, was done by hand.
  • Part 2, worth 10 points, was done with a computer.

CS 3, MIDTERM

Professor Dan Garcia

Fall Semester 2002

Test given 17 October 2002.

Exam topic: Recursion

THIS IS PART 1 (ONE) OF THE EXAM.

b. What pattern is last-differnece? (Circle one- 1 point.) MAPPING FINDING COUNTIN FILTERING TESTING COMBINING c. Your partner then disappears to Hawaii and takes abs-differences with her. Fill in the blanks to complete abs-differences that takes a sentence of numbers and returns the absolute value of the differences of successive elements.

  • Feel free to use second, which returns the second item from a word or sentence (similar to first. )
  • Feel free to use abs, which returns the absolute value of a number.
  • You MAY NOT define any helper functions (5 points.)

;;INPUTS: A sentence of numbers. ;;REQUIRES: The sentence has at least 2 elements. ;;SIDE-EFFECTS: None ;;RETURNS: A sentence containing the abs. value of the differences of successive elements. ;;EXAMPLE: (last-difference ‘(3 5 0 –3 10))

(2 5 3 13)

(define (abs-difference s) (if ___________________________________________________________



d. As you can guess, sometimes two different sentences produce the same last- difference. Write down two sentences that have the same last-difference of 1. What makes this challenging is that each of the six blanks below must be filled with a different single-digit number from 0 through 9. There may be multiple solutions (2 points.)

(last-difference ‘(___ ___ ___))

(last-difference ‘(___ ___ ___))

1

T ake a deep breath, you’re about one-third of the way done!

Question 3: The students +ed and +ed like rabbits… (13 points) We are going to look at the function multiply-then-add, a procedure that takes three arguments a, b, and cand multiples the first two and then adds the third. The catch is that YOU MAY NOT USE * OR / from scheme! Here are the specs:

;;INPUTS: Numbers a, b, and c ;;REQUIRES: a, b, and c are not negative. ;;SIDE-EFFECTS: none ;;RETURNS: The same thing that (+ (* a b) c) does ;;EXAMPLE: (multiply-then-add 3 4 100)

  1. (3 * 4) + 100 = 112.

a. Write a version of multiply-then-add using EMBEDDED recursion, without using * or /. You MAY NOT define any helper functions. (6 points)

b. Write a version of multiply-then-add using TAIL recursion, without using * or /. You MAY NOT define any helper functions (6 points).

c. Take a look at your EMBEDDED solution in (A) above. It might be that the restrictions we placed on the inputs in the REQUIRES section were too strict. IF you think the earlier REQUIRES section was fine, circle it above and leave the box below blank. Otherwise, cross it off and write a new REQUIRES section based on your (A) solution below. ;;REQUIRES:

(define (multiply-then-add a b c)

(define (multiple-then-add a b c)

CS 3, MIDTERM

Professor Dan Garcia

Fall Semester 2002

PART II given 22 October 2002.

Exam topic: Recursion

THIS IS PART 2 (TWO) OF THE EXAM, THE COMPUTER PART

THE QUESTIONS

We’re going to write all-good-partners by first debugging its helper function, called pair- with-others (below we have changed A SINGLE LINE) from a perfect, working version of the function). pair-with-others takes four arguments:

  • good-partners? (the predicate mentioned earlier.)
  • person (a particular student’s name as a word)
  • others (the sentence of remaining potential student partners), and
  • answer (the current answer so far) and returns a sentence of all the good partnerships involving person. For example:

(pair-with-others same-name-length? ‘bo ‘(che dan ed) ‘())

(bo-ed)

(define (make-partners student-a student-b) ;;helper function (word student-a ‘- ‘student-b))

(define (pair-with-others good-partners? person others answer) (cond ((empty? others) answers);; line ((good-partners? person (first others));; line 2 (pair-with-others good-partners? ;; line 3 person ;; line 4 (bf others) ;; line 5 se (make-partners person (first others))))) ;;line 6 (else (pair-with-others good-partners? ;; line 7 person ;; line 8 (bf others) ;; line 9 answer)))) ;; line 10 a. Why is pair-others buggy? Fill in the blanks for the bug that applies and provide the simplest yourname input that makes the statement correct and triggers only that bug. Then change only one line to fix the error. ( Simplest = fewest students in the ‘others’ input sentence; yourname = you must supply your own names for the input students. You may not use ana , bo , che , dan , or ed. Be creative.) You might find it handy to write same-name-length? for online testing. (4 points)

It rejects good partnerships (when it shouldn’t.) E.g., (pair-with-others same-name-length? _____ _____ ‘() ) returns ________________________ when it should return ______________________________

It accepts bad partnerships (when it shouldn’t.) E.g., (pair-with-others same-name-length? _____ _____ ‘() ) returns ________________________ when it should return ______________________________

It can go into an infinite loop (when it shouldn’t. E.g., (pair-with-others same-name-length? _____ _____ ‘() ) returns ________________________ when it should return ______________________________

“Replacing line _____ with __________________________________________________________________ will fix the bug so that pair-with-others works correctly on all input”.

(define (all-good-partners good-partners? students) (if (empty? students) ;;line 1 ‘() ;;line 2 (se (pair-with-others good partners? (first students) students ‘()) ;;line 3 (all-good-partners good-partners? (bf students))))) ;;line 4

b. Above you see all-good-partners. As before, we have changed only a single line from the perfect working version to introduce a bug. Complete the statement below with the simplest surname input that triggers the bug ( simplest = fewest people in the ‘students’ input sentence, yourname = you must supply your own name for the students. You may not use ana , bo , che , dan , or ed. Be creative.) If you wish, experiment with your online version of all-good-partners (4 points).

c. Let’s say your TA doesn’t care about the silly rules which restrict partnerships. Using all-good-partners, write all-partners which returns all possible partners without restrictions. For example:

(all-partners ‘(ana bo che dan ed))

(ana-bo ana-che ana-dan ana-ed bo-che bo-dan bo-ed che-dan che-ed dan-ed)

The partners can be in any order in the sentence. You may write a one-line helper function if you need it. (2 points)

(all-good-partners same-name-length? ________________________________________________________) returns _______________________________________ when it should return _________________________________________________________________. Replacing line ________ with ___________________________________________________________________________ will fix the bug so that all-good-partners works correctly on all input.

(define (all-partners students) _______________________________________________________________________________)

;;Below you may write a one-line helper function if you need…

(define (____________________) _________________________________________________________)