-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathex-195-to-198.rkt
293 lines (233 loc) · 6.87 KB
/
ex-195-to-198.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#lang htdp/bsl+
(require 2htdp/batch-io)
(require racket/string)
(require test-engine/racket-tests)
; ==================== Exercise 195 ====================
; ### Constants
; NOTE: import a bigger dictionary if you wish
(define DICT (read-lines "short-dictionary"))
; ### Data Definitions
; A Dictionary is one of:
; - '()
; - (const String Dictionary)
; A Letter is one of the following 1Strings:
; – "a"
; – ...
; – "z"
; ### Functions
; Letter Dictionary -> Number
; Counts how many words start with letter in the dict
(check-expect (starts-with# "a" (list "arrow" "available" "pig" "donkey")) 2)
(check-expect (starts-with# "z" (list "arrow" "pig" "donkey")) 0)
(define (starts-with# letter dict)
(cond
[(empty? dict) 0]
[else
(+
(if (starts-with? (first dict) letter) 1 0)
(starts-with# letter (rest dict))
)]))
; String 1-String -> Boolean
; Checks whether str strings with first
(check-expect (starts-with? "lol" "l") #true)
(check-expect (starts-with? "lol" "z") #false)
(define (starts-with? str first)
(string=?
(string-ith str 0)
first
))
(write-file
'stdout
(format
"There are ~a words starting with the letter 'a'\n"
(starts-with# "a" DICT)
))
(write-file
'stdout
(format
"There are ~a words starting with the letter 'd'\n"
(starts-with# "d" DICT)
))
; =================== End of exercise ==================
; ==================== Exercise 196 ====================
; ### Constants
(define LETTERS (explode "abcdefghijklmnopqrstuvwxyz"))
; A Letter-Count is a structure
; (make-lc letter count)
(define-struct lc [letter count])
; ### Functions
; Dictionary List-of-letters -> List-of-Letter-Count
; Given a dictionary, it returs a list of Letter-Count with the frequencies
; of words by first letter
(check-expect (count-by-letter '() '()) '())
(check-expect
(count-by-letter (list "arrow" "available" "pig" "donkey") (list "a" "b" "p" "d"))
(list (make-lc "a" 2) (make-lc "b" 0) (make-lc "p" 1) (make-lc "d" 1))
)
(define (count-by-letter dict lol)
(cond
[(empty? lol) '()]
; NOTE: this is O(n^2), but I think we don't have the tools to make it better so far
[else
(cons
(make-lc
(first lol)
(starts-with# (first lol) dict)
)
(count-by-letter dict (rest lol))
)]))
; Returns the first letter of a string
(define (first-letter str)
(string-ith str 0)
)
(define (format-lolc lolc)
(cond
[(empty? lolc) '()]
[else
(cons
(format "~s: ~a" (lc-letter (first lolc)) (lc-count (first lolc)))
(format-lolc (rest lolc))
)]))
(write-file
'stdout
(string-append
"Frequencies of first letters in dict:\n"
(string-join
(format-lolc (count-by-letter DICT LETTERS))
"\n"
)
"\n"
))
; =================== End of exercise ==================
; ==================== Exercise 197 ====================
; ### Functions
; Dictionary -> Letter
; Returns the most frequent first word letter from a non-empty dictionary
(check-expect (most-frequent (list "lol" "troll" "tunnel" "cod")) "t")
(define (most-frequent dict)
(lc-letter
(max-freq-from-lolc
(count-by-letter dict LETTERS)
)))
; List-of-letter-count -> Letter-Count
; Returns the letter-count of the list with the greatest frequency
;
; NOTE: the exercise suggests either picking the maximum freq. or sorting
; the list and then getting the first/last value. The former is simpler
; to implement and O(n), whereas the latter is more messy and can't be
; done in O(n) ... if I knew how I would be rich lol
(check-expect
(max-freq-from-lolc (list (make-lc "a" 10) (make-lc "b" 3)))
(make-lc "a" 10)
)
(define (max-freq-from-lolc lolc)
(cond
[(empty? (rest lolc)) (first lolc)]
[else
(max-freq-lc
(first lolc)
(max-freq-from-lolc (rest lolc))
)]))
; Letter-Count Letter-Count -> Letter-Count
; Given two Letter-Counts, it returns the one with the greatest frequency
(check-expect
(max-freq-lc (make-lc "a" 10) (make-lc "b" 3))
(make-lc "a" 10)
)
(define (max-freq-lc a b)
(if
(>= (lc-count a) (lc-count b))
a
b
))
(write-file
'stdout
(format "The most frequent first letter is: ~a\n\n" (most-frequent DICT))
)
; =================== End of exercise ==================
; ==================== Exercise 198 ====================
; ### Functions
; Dictionary -> List-of-dictionaries
; Groups words by first letter. If a letter is not used to start any word, there won't be a list
; for words starting with that letter
(check-expect (words-by-first-letter '()) '())
(check-expect
(words-by-first-letter (list "asterisk" "asteroid" "beer" "burrito" "zealot"))
(list
(list "asterisk" "asteroid")
(list "beer" "burrito")
(list "zealot")
))
(define (words-by-first-letter dict)
(filter-out-empty-lists (group-by-first-letter dict LETTERS))
)
; Dictionary -> List-of-dictionaries
; Groups words by first letter. If a letter is not used to start any word, there will be an
; empty list for words starting with that letter
(define (group-by-first-letter dict letters)
(cond
[(empty? letters) '()]
[else
(cons
(filter-starting-with dict (first letters))
(group-by-first-letter dict (rest letters))
)]))
; List-of-lists-of-anything -> List-of-lists-of-anything
; Filters out inner empty lists
(check-expect
(filter-out-empty-lists (list (list "a") '()))
(list (list "a"))
)
(define (filter-out-empty-lists lol)
(cond
[(empty? lol) '()]
[(empty? (first lol)) (filter-out-empty-lists (rest lol))]
[else
(cons
(first lol)
(filter-out-empty-lists (rest lol))
)]))
; Dictionary Letter -> Dictionary
; Filters out words not starting by letter
(check-expect
(filter-starting-with (list "hola" "hello" "ciao") "c")
(list "ciao")
)
(define (filter-starting-with dict letter)
(cond
[(empty? dict) '()]
[(starts-with? (first dict) letter)
(cons
(first dict)
(filter-starting-with (rest dict) letter)
)]
[else (filter-starting-with (rest dict) letter)]
))
; Returns the most frequent first word letter from a non-empty dictionary
(check-expect (most-frequent.v2 (list "lol" "troll" "tunnel" "cod")) "t")
(define (most-frequent.v2 dict)
(first-letter
(first
(longest
(words-by-first-letter dict)
))))
(check-expect
(most-frequent DICT)
(most-frequent.v2 DICT)
)
; List-of-list-of-anything -> List-of-anything
; Returns the longest list
(check-expect (longest (list (list 1 2) (list 1) '())) (list 1 2))
(define (longest lol)
(cond
[(empty? (rest lol)) (first lol)]
[(>
(length (first lol))
(length (longest (rest lol)))
)
(first lol)
]
[else (longest (rest lol))]
))
; =================== End of exercise ==================
(test)