-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathE14.rkt
208 lines (171 loc) · 6.17 KB
/
E14.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname E14) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
;;
;; ***************************************************
;; James Ah Yong
;; CS 135 Fall 2020
;; Module 14 Exercises
;; ***************************************************
;;
;;
;; Exercise 1
;;
;; (keep4 lst) finds strings in a list of strings, lst, with length 4
;; Example:
(check-expect
(keep4 '("There's" "no" "fate" "but" "what" "we" "make" "for" "ourselves"))
'("fate" "what" "make"))
;; keep4: (listof Str) -> (listof Str)
(define (keep4 lst)
(filter (lambda (str) (= (string-length str) 4)) lst))
;;
;; Exercise 2
;;
;; (squash-range nums) converts a list of nums from [0,255] to [0,1]
;; Example:
(check-expect (squash-range '(0 204 255)) '(0 0.8 1))
;; squash-range: (listof Num) -> (listof Num)
;; Requires: every number is between 0 and 1, inclusive
(define (squash-range nums)
(map (lambda (n) (/ n 255)) nums))
;;
;; Exercise 3
;;
;; (greet-each names) produces a greeting for every name in a list of names
;; Example:
(check-expect (greet-each '("Ali" "Carlos" "Sai"))
'("Hi Ali!" "Hi Carlos!" "Hi Sai!"))
;; greet-each: (listof Str) -> (listof Str)
(define (greet-each names)
(map (lambda (name) (string-append "Hi " name "!")) names))
;; (neg-odd nums) negates all odd numbers in nums, leaving the rest as-is
;; Example:
(check-expect (neg-odd '(2 5 8 11 14 17)) '(2 -5 8 -11 14 -17))
;; neg-odd: (listof Int) -> (listof Int)
(define (neg-odd nums)
(map (lambda (n) (cond [(odd? n) (- n)]
[else n]))
nums))
;;
;; Exercise 4
;;
;; (count-odd nums) counts odd numbers in nums
;; Example:
(check-expect (count-odd '(2 5 8 11 14 17)) 3)
;; count-odd: (listof Nat) -> Nat
(define (count-odd nums)
(foldr (lambda (x count) (+ count (remainder x 2))) 0 nums))
;; Alternative solutions:
(check-expect (count-odd/filter '(2 5 8 11 14 17)) 3)
(define (count-odd/filter nums)
(length (filter odd? nums)))
(check-expect (count-odd/map '(2 5 8 11 14 17)) 3)
(define (count-odd/map nums)
(foldr + 0 (map (lambda (x) (remainder x 2)) nums)))
;; (prod nums) produces the product of a list of nums
;; Example:
(check-expect (prod '(2 2 3 5)) 60)
;; prod: (listof Num) -> Num
(define (prod nums) (foldr * 1 nums))
;; (total-length lst) produces the total number of elements
;; in a nested list, lst
;; Example:
(check-expect (total-length '((1 2 3) (4 5) (1 1 1))) 8)
;; total-length: (nested-listof Any) -> Nat
(define (total-length lst)
(foldr (lambda (l rror) (+ rror (length l))) 0 lst))
;; Alternative solution:
(check-expect (total-length/foldr '((1 2 3) (4 5) (1 1 1))) 8)
(define (total-length/foldr lst)
(foldr (lambda (l rror) (+ rror (foldr (lambda (x len) (add1 len)) 0 l)))
0 lst))
;;
;; Exercise 5
;;
;; (average nums) takes the geometric mean of nums
;; Examples:
(check-expect (average '(2 4 9)) 5)
(check-expect (average '(4 5 6 6)) 5.25)
;; average: (listof Num) -> Num
;; Requires: nums is not empty
(define (average nums)
(/ (foldr + 0 nums) (length nums)))
;; (times-square nums) produces the product of all perfect squares in nums
;; Example:
(check-expect (times-square '(1 25 5 4 1 17)) 100)
;; times-square: (listof Nat) -> Nat
(define (times-square nums)
(foldr * 1 (filter (lambda (x) (integer? (sqrt x))) nums)))
;;
;; Exercise 6
;;
(define (double n) (* n 2))
;; (double-each lst) doubles each element in a list
;; Example:
(check-expect (double-each '(1 2 3 4)) '(2 4 6 8))
;; double-each: (listof Num) -> (listof Num)
(define (double-each lst)
(foldr (lambda (n lst) (cons (double n) lst)) empty lst))
;; (keep-evens lst) filters out only even elements of lst
;; Example:
(check-expect (keep-evens '(1 2 3 4 5 6)) '(2 4 6))
;; keep-evens: (listof Int) -> (listof Int)
(define (keep-evens lst)
(foldr (lambda (n lst) (cond [(even? n) (cons n lst)] [else lst])) empty lst))
;; (sum-evens lst) sums all the even elements of lst
;; Example:
(check-expect (sum-evens (list 2 3 4 5)) 6)
;; sum-evens: (listof Int) -> Int
(define (sum-evens lst) (foldr + 0 (filter even? lst)))
;; Alternative solution:
(check-expect (sum-evens/no-filter (list 2 3 4 5)) 6)
(define (sum-evens/no-filter lst)
(foldr (lambda (n sum) (cond [(even? n) (+ n sum)] [else sum])) 0 lst))
;;
;; Exercise 7
;;
;; (multiply-each lst n) multiplies each element in lst by n
;; Example:
(check-expect (multiply-each (list 2 3 5) 4) (list 8 12 20))
;; multiply-each: (listof Num) Num -> (listof Num)
(define (multiply-each lst n)
(map (lambda (x) (* n x)) lst))
;; (add-total lst) adds the total sum of lst to each element of lst
;; Example:
(check-expect (add-total (list 2 3 5 10)) (list 22 23 25 30))
;; add-total: (listof Num) -> (listof Num)
(define (add-total lst)
(local [(define total (foldr + 0 lst))]
(map (lambda (n) (+ n total)) lst)))
;; (discard-bad lst lo hi) finds elements of lst between lo and hi, inclusive
;; Example:
(check-expect (discard-bad '(12 5 20 2 10 22) 10 20) '(12 20 10))
;; discard-bad: (listof Num) Num Num -> (listof Num)
(define (discard-bad lst lo hi)
(filter (lambda (n) (and (>= n lo) (<= n hi))) lst))
;;
;; Exercise 8
;;
;; (squash-bad lo hi lst) clamps elements of lst to a max of hi and min of lo
;; Example:
(check-expect (squash-bad 10 20 '(12 5 20 2 10 22)) '(12 10 20 10 10 20))
;; squash-bad: Num Num (listof Num)
(define (squash-bad lo hi lst)
(map (lambda (n) (cond [(< n lo) lo] [(> n hi) hi] [else n])) lst))
;; (above-average lst) finds values above in lst above the average of lst
;; Example:
(check-expect (above-average '(1 2 3 4 5 200 201)) '(200 201))
;; above-average: (listof Num) -> (listof Num)
(define (above-average lst)
(local [(define avg (/ (foldr + 0 lst) (length lst)))]
(filter (lambda (n) (>= n avg)) lst)))
;;
;; Exercise 10
;;
;; (triangles k) gives the first k triangular numbers
;; Example:
(check-expect (triangles 4) (list 0 1 3 6))
;; triangles: Nat -> (listof Nat)
(define (triangles n)
(build-list n (lambda (x) (/ (* x (add1 x)) 2))))