-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW1_SeungwooKim.rkt
230 lines (185 loc) · 7.3 KB
/
HW1_SeungwooKim.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
#lang plai
; Problem 1:
; Solved by myself: Y
; Time taken: about a minute
; [contract] dollar->won: number -> number
; [purpose] To convert dollar to won
; [tests] (test (dollar->won 1) 1200)
; (test (dollar->won 2) 2400)
; (test (dollar->won 5) 6000)
(define (dollar->won x) (* x 1200))
(test (dollar->won 1) 1200)
(test (dollar->won 2) 2400)
(test (dollar->won 5) 6000)
; Problem 2:
; Solved by myself: Y
; Time taken: about 20 minutes
; [contract] first_digit: number -> number
; [purpose] to get the first digit of three digit number
; [tests] (test (first_digit 100) 1)
; (test (first_digit 587) 5)
; (test (first_digit 964) 9)
(define (first_digit x) (/ (- x (modulo x 100)) 100))
(test (first_digit 100) 1)
(test (first_digit 587) 5)
(test (first_digit 964) 9)
; [contract] second_digit: number -> number
; [purpose] to get the second digit of three digit number
; [tests] (test (second_digit 100) 0)
; (test (second_digit 587) 8)
; (test (second_digit 964) 6)
(define (second_digit x) (/ (- (modulo x 100) (modulo x 10)) 10))
(test (second_digit 100) 0)
(test (second_digit 587) 8)
(test (second_digit 964) 6)
; [contract] thrid_digit: number -> number
; [purpose] to get the third digit of three digit number
; [tests] (test (third_digit 100) 0)
; (test (third_digit 587) 7)
; (test (third_digit 964) 4)
(define (third_digit x) (modulo x 10))
(test (third_digit 100) 0)
(test (third_digit 587) 7)
(test (third_digit 964) 4)
; [contract] digit_sum: number -> number
; [purpose] to sum all digits of three integer number
; [tests] (test (digit_sum 100) 1)
; (test (digit_sum 587) 20)
; (test (digit_sum 964) 19)
(define (digit_sum x)
(cond
[(= x 0) 0]
[else (+ (first_digit x) (second_digit x) (third_digit x))]))
(test (digit_sum 100) 1)
(test (digit_sum 587) 20)
(test (digit_sum 964) 19)
; Problem 3:
; Solved by myself: Y
; Time taken: about 5 minutes
; [contract] volume-sphere: number -> number
; [purpose] to calculate the volumne of sphere
; [tests] (test (volume-sphere 1) 4.18666666)
; (test (volume-sphere 2) 33.4933333)
(define (volume-sphere x) (* (/ 4 3) 3.14 x x x))
(test (volume-sphere 1) 4.18666666)
(test (volume-sphere 2) 33.4933333)
; Problem 4:
; Solved by myself: Y
; Time taken: about 5 minutes
; [contract] is-even?: number -> boolean
; [purpose] to determine whether x is an even number or not
; [tests] (test (is-even? 0) true)
; (test (is-even? 5) false)
; (test (is-even? 100) true)
(define (is-even? x) (= (modulo x 2) 0))
(test (is-even? 0) true)
(test (is-even? 5) false)
(test (is-even? 100) true)
; Problem 5:
; Solved by myself: Y
; Time taken: about 5 minutes
; [contract] factorial: number -> number
; [purpose] to calculate the factorial of x
; [tests] (test (factorial 0) 1)
; (test (factorial 2) 2)
; (test (factorial 5) 120)
(define (factorial x)
(cond
[(= x 0) 1]
[else (* x (factorial (- x 1)))]))
(test (factorial 0) 1)
(test (factorial 2) 2)
(test (factorial 5) 120)
; [contract] combination: number -> number
; [purpose] to calculate the combination of n and k
; [tests] (test (combination 3 2) 3)
; (test (combination 10 3) 120)
; (test (combination 10 5) 252)
(define (combination n k) (/ (factorial n) (* (factorial k) (factorial (- n k)))))
(test (combination 3 2) 3)
(test (combination 10 3) 120)
(test (combination 10 5) 252)
; Problem 6:
; Solved by myself: Y
; Time taken: about an hour
; [purpose] to define the type PERSON which has Professor, UndergraduateStudent, and GraduateStudent
; [tests] (test (GraduateStudent? graduate1) true)
; (test (Professor professor1) true)
; (test (GraduateStudent? undergraduate1) false)
(define-type PERSON
(Professor (courses number?)
(projects number?))
(UndergraduateStudent (courses number?))
(GraduateStudent (courses number?)
(papers number?)))
(define graduate1(GraduateStudent 4 2))
(define professor1(Professor 3 6))
(define undergraduate1(UndergraduateStudent 6))
(test (GraduateStudent? graduate1) true)
(test (Professor? professor1) true)
(test (GraduateStudent? undergraduate1) false)
; [contract] have-courses: PERSON -> number
; [purpose] to get the number of courses either students taken or professor taught
; [tests] (test (have-courses graduate1) 4)
; (test (have-courses professor1) 3)
; (test (have-courses undergraduate1) 6)
(define (have-courses p)
(type-case PERSON p
[Professor (c prog) c]
[UndergraduateStudent (c) c]
[GraduateStudent (c ppr) c]))
(test (have-courses graduate1) 4)
(test (have-courses professor1) 3)
(test (have-courses undergraduate1) 6)
; [contract] ready-to-graduate: PERSON -> boolean
; [purpose] to check whether graudate student can graduate or not
; [tests] (test (ready-to-graduate graduate1) false)
; (test (ready-to-graduate graduate2) true)
; (test (ready-to-graduate graduate3) true)
(define (ready-to-graduate p)
(type-case PERSON p
[Professor (c proj) false]
[UndergraduateStudent (c) false]
[GraduateStudent (c ppr)
(cond
[(>= ppr 3) true]
[else false]
)]))
(define graduate2(GraduateStudent 3 3))
(define graduate3(GraduateStudent 2 5))
(test (ready-to-graduate graduate1) false)
(test (ready-to-graduate graduate2) true)
(test (ready-to-graduate graduate3) true)
; Problem 7:
; Solved by myself: N (JC helped me on how to get the character from the list)
; Time taken: about an hour
; [contract] name-alphabet: list -> list
; [purpose] to change the specific characters with spedicif strings
; [tests] (test (name-alphabet '(a b c)) '(alice unnamed cherry))
; (test (name-alphabet '(a j k e)) '(alice jc kate unnamed))
; (test (name-alphabet '(b d l o)) '(unnamed unnamed unnamed unnamed))
(define (name-alphabet lst)
(cond
[(empty? lst) (append empty)]
[(symbol=? (first lst) 'a) (append (list 'alice) (name-alphabet(rest lst)))]
[(symbol=? (first lst) 'c) (append (list 'cherry) (name-alphabet(rest lst)))]
[(symbol=? (first lst) 'j) (append (list 'jc) (name-alphabet(rest lst)))]
[(symbol=? (first lst) 'k) (append (list 'kate) (name-alphabet(rest lst)))]
[else (append (list 'unnamed) (name-alphabet(rest lst)))]))
(test (name-alphabet '(a b c)) '(alice unnamed cherry))
(test (name-alphabet '(a j k e)) '(alice jc kate unnamed))
(test (name-alphabet '(b d l o)) '(unnamed unnamed unnamed unnamed))
; Problem 8:
; Solved by myself: Y
; Time taken: about an hour
; [contract] update-namme: symbol, symbol, list of symbols -> list of symbols
; [purpose] to update the old symbol with the new one
; [tests] (test (update-name 'cherry 'claire (cons 'jc (cons 'cherry (cons 'kate empty)))) '(jc claire kate))
; (test (update-name 'difficult 'exciting (cons 'pl (cons 'is (cons 'difficult empty)))) '(pl is exciting))
(define (update-name old new lst)
(cond
[(empty? lst) (append empty)]
[(symbol=? (first lst) old) (append (list new) (update-name old new (rest lst)))]
[else (append (list (first lst)) (update-name old new (rest lst)))]))
(test (update-name 'cherry 'claire (cons 'jc (cons 'cherry (cons 'kate empty)))) '(jc claire kate))
(test (update-name 'difficult 'exciting (cons 'pl (cons 'is (cons 'difficult empty)))) '(pl is exciting))