-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamplary_k1.rkt
194 lines (160 loc) · 4.49 KB
/
examplary_k1.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
#lang racket
(define (middle-digit n)
(define (get-len n len)
(if (< n 1)
len
(get-len (quotient n 10) (+ len 1))
)
)
(define (get-n-digit num digit)
(if (< num (expt 10 digit))
(remainder num 10)
(get-n-digit (quotient num 10) digit)
)
)
(let
(
(len (get-len n 0))
)
(if (= (remainder len 2) 0)
-1
(get-n-digit n (+ (quotient len 2) 1))
)
)
)
;(middle-digit 4522432)
(define (meet-twice? f g a b)
(define (meet-twice-help a b count)
(cond ((= count 2) #t)
((> a b) #f)
((= (f a) (g a)) (meet-twice-help (+ a 1) b (+ count 1)))
(else (meet-twice-help (+ a 1) b count)))
)
(meet-twice-help a b 0)
)
;(meet-twice? (lambda(x)x) (lambda(x) (- x)) -3 1)
;(meet-twice? (lambda(x)x) sqrt 0 5)
(define (next-look-and-say xs)
(define (next-look-and-say-helper xs cur-num num-times)
(if (null? xs)
(cons num-times (cons cur-num '()))
(if
(= (car xs) cur-num)
(next-look-and-say-helper (cdr xs) cur-num (+ num-times 1))
(cons num-times (cons cur-num (next-look-and-say-helper (cdr xs) (car xs) 1)))
)
)
)
(next-look-and-say-helper (cdr xs) (car xs) 1)
)
;(next-look-and-say '(1 1 2 3 3))
(define (is-em? xs oper f)
(define (contains? elem xs)
(if (null? xs)
#f
(if (= (car xs) elem)
#t
(contains? elem (cdr xs))
)
)
)
(define (asociativity? elem xs)
(if (null? xs)
#t
(if (= (oper (f elem) (f (car xs))) (f (oper elem (car xs))))
(asociativity? elem (cdr xs))
#f
)
)
)
(define (em-helper ys)
(if (null? ys)
#t
(if (and (contains? (f (car ys)) xs) (asociativity? (car ys) xs))
(em-helper (cdr ys))
#f
)
)
)
(em-helper xs)
)
;(is-em? '(0 1 4 6) + (lambda (x) (remainder x 3)))
(define (accumulate op null-value start end term next)
(if (> start end)
null-value
(op (term start) (accumulate op null-value (next start) end term next))
)
)
(define (average f g)
(lambda (x) (/ (+ (f x) (g x)) 2))
)
(define (calcprod f n)
(accumulate * 1 1 n (lambda (i) ((average f (lambda (x) (expt i x))) i)) (lambda (x) (+ x 1)))
)
;(calcprod (lambda (x) (+ x 2)) 3)
(define (max-unique list)
(define (is-unique elem list count)
(if (null? list)
(if (= count 1)
#t
#f
)
(if (= elem (car list))
(is-unique elem (cdr list) (+ count 1))
(is-unique elem (cdr list) count)
)
)
)
(define (find-max-unique list-original list-remainder)
(if (null? list-remainder)
0
(if (is-unique (car list-remainder) list-original 0)
(max (car list-remainder) (find-max-unique list-original (cdr list-remainder)))
(find-max-unique list-original (cdr list-remainder))
)
)
)
(if (null? list)
0
(max (find-max-unique (car list) (car list)) (max-unique (cdr list)))
)
)
;(max-unique '((1 2 3 2) (5 5 6) (0)))
(define (find-root f)
(define (find-root-helper min max)
(if (< (abs (f (/ (+ min max) 2))) (expt 10 -5))
(/ (+ min max) 2)
(if (< (* (f min) (f (/ (+ min max) 2))) 0)
(find-root-helper min (/ (+ min max) 2))
(find-root-helper (/ (+ min max) 2) max)
)
)
)
(define (find-min-max min max)
(if (< (* (f min) (f max)) 0)
(list min max)
(find-min-max (* min 2) (* max 2))
)
)
(let ((min-max (find-min-max -10 10))
)
(find-root-helper (list-ref min-max 0) (list-ref min-max 1))
)
)
(find-root (lambda (x) (- (* 3 x) 6)))
(define (longest-descending xs)
(define (longest-descending-helper xs prev cur-max abs-max)
(if (null? xs)
abs-max
(if (> prev (car xs))
(longest-descending-helper (cdr xs) (car xs) (cons (car xs) cur-max) abs-max)
(if (> (length cur-max) (length abs-max))
(longest-descending-helper (cdr xs) (car xs) (cons (car xs) '()) cur-max)
(longest-descending-helper (cdr xs) (car xs) (cons (car xs) '()) abs-max)
)
)
)
)
(reverse (longest-descending-helper (cdr xs) (car xs) (cons (car xs) '()) '()))
)
(longest-descending '(5 3 8 6 4 2 6 7 1))