30.08.2014 Views

Assignment 02 questions (PDF) - Student.cs.uwaterloo.ca

Assignment 02 questions (PDF) - Student.cs.uwaterloo.ca

Assignment 02 questions (PDF) - Student.cs.uwaterloo.ca

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CS 116 Winter 2014!<br />

<strong>Assignment</strong> <strong>02</strong>!<br />

Due at 11:00am on Tuesday, January 28!<br />

• This assignment consists mostly of material from Module <strong>02</strong>, on functional<br />

abstraction and functions as values.<br />

• You must use abstract list functions (map, filter, foldr, build-list)<br />

where appropriate. You may use the function length.<br />

• <br />

For full marks, each question must use abstract list functions in a nontrivial<br />

way. You may NOT use explicit/structural recursion in any of your<br />

solutions. <br />

• For this assignment, unless otherwise noted, you <strong>ca</strong>nnot use named helper<br />

functions. This includes those that are defined outside of a function and those<br />

defined within a lo<strong>ca</strong>l. In essence you should only use lo<strong>ca</strong>l to define<br />

constants and you should use lambda for any functions.<br />

• You are expected to use the design recipe when writing functions from scratch.<br />

For lambda functions embedded in larger functions you do not need comments<br />

(i.e. contracts or purpose).<br />

• Do not copy the purpose directly from the assignment description. The purpose<br />

should be written in your own words and reference the parameter names of<br />

your functions.<br />

• The solutions you submit must be entirely your own work. Do not look up either<br />

full or partial solutions on the Internet or in printed sources.<br />

• Download the interface file from the course Web page.<br />

• Read the course Web page for more information on assignment policies and how<br />

to organize and submit your work. Follow the instructions in the style guide.<br />

Specifi<strong>ca</strong>lly your solutions should be placed in files a<strong>02</strong>qY.rkt where Y is a value<br />

from 1 to 4.<br />

• Read each <strong>questions</strong> <strong>ca</strong>refully for restrictions. Test data for all <strong>questions</strong> will<br />

always meet the stated assumptions for consumed values.<br />

• <strong>Assignment</strong>s will not be accepted through email. Course staff will not debug<br />

code emailed to them.<br />

• Read the assignment <strong>ca</strong>refully before asking any <strong>questions</strong> on Piazza.<br />

• You may want to review the string and characters resource on the course<br />

website.<br />

!<br />

Coverage: Module <strong>02</strong><br />

Language Level: Intermediate <strong>Student</strong> Scheme with lambda<br />

!<br />

1. A triangular number is one which represents the number of “balls” required to make an<br />

equilateral triangle. For example the numbers 1, 3 and 6 are the first three triangular<br />

numbers (note how they correspond to the images below).<br />

!<br />

!<br />

!<br />

k = 1; b = 1 k = 2; b = 3 k = 3; b = 6


CS 116 Winter 2014!<br />

<strong>Assignment</strong> <strong>02</strong>!<br />

Due at 11:00am on Tuesday, January 28!<br />

!<br />

!<br />

!<br />

!<br />

In general the number of balls b required to make a triangle with k rows is given by:<br />

(a)<br />

b = 1 + 2 + 3 + 4 + … + k = k(k+1)/2<br />

Use the formula above, k(k+1)/2, to write a Scheme function first-n-triangular<br />

that consumes a positive natural number n and produces a list of the first n triangular<br />

numbers.<br />

For example,<br />

(first-n-triangular 1) => (list 1)<br />

(first-n-triangular 4) => (list 1 3 6 10)<br />

!<br />

(b)<br />

!<br />

Write a Scheme function first-n-triangular—v2 that consumes a positive natural<br />

number n and produces a list of the first n triangular numbers. In this function you<br />

<strong>ca</strong>nnot use the formula k(k+1)/2.<br />

!<br />

2. (a) Write a Scheme function all-letters-sorted that consumes a list of lower<strong>ca</strong>se<br />

alphabetic strings los and produces a single string containing all of the letters in los<br />

ordered alphabeti<strong>ca</strong>lly.<br />

!<br />

For example,<br />

(all-letters-sorted (list "bob" "mob" "cob" "dave" "rob")) =><br />

"abbbbbcdemoooorv"<br />

!<br />

!<br />

!<br />

!<br />

Use the function sort in your solution. From the Racket reference:<br />

(sort lst less-than?) <br />

Returns a list sorted according to the less-than? procedure, which takes two elements of lst and returns a<br />

true value if the first is less (i.e., should be sorted earlier) than the second. <br />

!<br />

Examples:!<br />

> (sort '(1 3 4 2)


CS 116 Winter 2014!<br />

<strong>Assignment</strong> <strong>02</strong>!<br />

Due at 11:00am on Tuesday, January 28!<br />

!<br />

For example,<br />

(unique-letters-sorted<br />

(list "bob" "mob" "cob" "dave" "rob"))<br />

=> "abcdemorv"<br />

!<br />

3. Consider the new structure<br />

(define-struct lottery-ticket (game value))<br />

;; A Lottery-Ticket is a structure (make-lottery-ticket g v), where<br />

;; g is one of the symbols 'Keno, 'Powerball, 'Lottario,<br />

;; v is an integer representing the “earnings” on that ticket<br />

;;(earnings could be negative)<br />

!<br />

Write a Scheme function game-summary that consumes a symbol game and a list of<br />

Lottery-ticket structures, <strong>ca</strong>lled all-tickets, and produces a function. The<br />

produced function accepts a symbol (either 'won 'lost or 'net) and produces the total<br />

amount won, lost or net (see table below) for that particular game across all tickets.<br />

!<br />

Value<br />

Meaning<br />

won<br />

lost<br />

sum of all positive earnings<br />

absolute value of the sum of all negative earnings<br />

net<br />

won - lost (i.e. total value)<br />

!<br />

For example,<br />

(define powerball-summary<br />

(game-summary 'Powerball<br />

(list (make-lottery-ticket 'Powerball 25)<br />

(make-lottery-ticket 'Keno -9)<br />

(make-lottery-ticket 'Powerball -20)<br />

(make-lottery-ticket 'Lottario 17)<br />

(make-lottery-ticket 'Powerball 10)<br />

(make-lottery-ticket 'Keno 10)<br />

(make-lottery-ticket 'Powerball -30))))<br />

!<br />

(powerball-summary 'won) => 35<br />

(powerball-summary 'net) => -15<br />

(powerball-summary 'lost) => 50<br />

!<br />

4. Write a Scheme function letter-with-position that consumes a list of strings<br />

wordlist and produces a list of lists of strings according to the following:<br />

!<br />

• For each non-empty string w in wordlist the function should produce a list of strings<br />

e a c h w i t h 3 o r m o r e c h a r a c t e r s w h e r e t h e f i r s t c h a r a c t e r i s a l e t t e r f r o m w, the second<br />

character is an underscore _ and third (and possibly 4th and 5th etc.) character(s)<br />

represent the position of that letter in w (indexed by zero).


CS 116 Winter 2014!<br />

<strong>Assignment</strong> <strong>02</strong>!<br />

Due at 11:00am on Tuesday, January 28!<br />

• For each empty string produce empty.<br />

• If wordlist is empty produce empty.<br />

!<br />

!<br />

!<br />

!<br />

!<br />

For example,<br />

(letter-with-position (list "hello" "goodbye")) =><br />

(list<br />

(list "h_0" "e_1" "l_2" "l_3" "o_4")<br />

(list "g_0" "o_1" "o_2" "d_3" "b_4" "y_5" "e_6"))<br />

(letter-with-position (list "business suits" "" "4 u")) =><br />

(list<br />

(list "b_0" "u_1" "s_2" "i_3" "n_4" "e_5" "s_6" "s_7" " _8"<br />

"s_9" "u_10" "i_11" "t_12" "s_13")<br />

empty<br />

(list "4_0" " _1" "u_2"))<br />

The order of the letters should correspond to their order in the original word.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!