-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathE12.rkt
116 lines (98 loc) · 3.75 KB
/
E12.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
;; 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-reader.ss" "lang")((modname E12) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
;;
;; ***************************************************
;; James Ah Yong
;; CS 135 Fall 2020
;; Module 12 Exercises
;; ***************************************************
;;
;;
;; Exercise 1
;;
;; (check-msg-length to from body min-len max-len) checks if a message
;; payload of to, from, and body is outside the bounds of min-len and max-len
;; Examples:
(check-expect (check-msg-length "Ed" "Santa" "Xmas List" 3 14) 'too-long)
(check-expect (check-msg-length "Ed" "Santa" "Xmas List" 3 140) 16)
(check-expect (check-msg-length "Charlie" "Santa" "No presents for Ed!"
140 280) 'too-short)
;; check-msg-length: Str Str Str Nat Nat -> (anyof Nat Sym)
(define (check-msg-length to from body min-len max-len)
(local [(define len (string-length (string-append to from body)))]
(cond [(> len max-len) 'too-long]
[(< len min-len) 'too-short]
[else len])))
;; Unoptimal solution
#|(define (check-msg-length to from body min-len max-len)
(cond [(> (string-length (string-append to from body)) max-len)
'too-long]
[(< (string-length (string-append to from body)) min-len)
'too-short]
[else (string-length (string-append to from body))]))|#
;;
;; Exercise 2
;;
;; (normalize lst) divides each value in lst by the sum of all values
;; Examples:
(check-expect (normalize empty) empty)
(check-expect (normalize '(4 2 14)) '(0.2 0.1 0.7))
;; normalize: (listof Num) -> (listof Num)
(define (normalize lst)
(local [(define (sum lst)
(cond [(empty? lst) 0]
[else (+ (first lst) (sum (rest lst)))]))
(define (list/ lst k)
(cond [(empty? lst) empty]
[else (cons (/ (first lst) k) (list/ (rest lst) k))]))]
(list/ lst (sum lst))))
;;
;; Exercise 3
;;
;; (list-squares n) produces the squares of the first n natural numbers
;; Example:
(check-expect (list-squares 4) '(0 1 4 9))
;; list-squares: Nat -> (listof Nat)
(define (list-squares n)
(local [(define (squares k)
(cond [(>= k n) empty]
[else (cons (sqr k) (squares (add1 k)))]))]
(squares 0)))
;;
;; Exercise 4
;;
;; (mult-table3 nr nc) produces a multiplication table with nr rows and nc cols
;; Example:
(check-expect (mult-table3 3 4)
(list (list 0 0 0 0)
(list 0 1 2 3)
(list 0 2 4 6)))
;; mult-table3: Nat Nat -> (listof (listof Nat))
(define (mult-table3 nr nc)
(local [(define (rows-to r)
(local [(define (cols-to c)
(cond [(>= c nc) empty]
[else (cons (* r c) (cols-to (add1 c)))]))]
(cond [(>= r nr) empty]
[else (cons (cols-to 0) (rows-to (add1 r)))])))]
(rows-to 0)))
;;
;; Exercise 5
;;
;; (table-ccy nr nc) produces a (nr)x(nc) table where each element is c^2*r
;; Example:
(check-expect (table-ccr 4 5)
(list (list 0 0 0 0 0)
(list 0 1 4 9 16)
(list 0 2 8 18 32)
(list 0 3 12 27 48)))
;; table-ccy: Nat Nat -> (listof (listof Nat))
(define (table-ccr nr nc)
(local [(define (rows-to r)
(local [(define (cols-to c)
(cond [(>= c nc) empty]
[else (cons (* r c c) (cols-to (add1 c)))]))]
(cond [(>= r nr) empty]
[else (cons (cols-to 0) (rows-to (add1 r)))])))]
(rows-to 0)))