File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ #lang racket
2
+
3
+ ; receives a function and a list of integers
4
+ ; the function f determines if it's gonna be a sum of two or three numbers
5
+ (define (find-2020 f xs)
6
+ (let ([value (f (first xs) (rest xs))])
7
+ (if (number? value) value (find-2020 f (rest xs)))))
8
+
9
+ ; returns the multiplication of two numbers which the sum is equal to 2020
10
+ ; if there is no such a pair, returns false
11
+ (define (check-two x xs)
12
+ (cond
13
+ [(null? xs) #f ]
14
+ [(equal? (+ x (first xs)) 2020 )
15
+ (* x (first xs))]
16
+ [else
17
+ (check-two x (rest xs))]))
18
+
19
+
20
+ ; same as check-two, but for three numbers
21
+ (define (check-three x xs)
22
+ (cond
23
+ [(null? xs) #f ]
24
+ [else
25
+ (let ([value (compare-three x (first xs) xs)])
26
+ (if (number? value) value (check-three x (rest xs))))]))
27
+
28
+
29
+ ; if there's some number from xs that x + y + number is equal to 2020, returns their multiplication
30
+ ; otherwise returns false
31
+ (define (compare-three x y xs)
32
+ (cond
33
+ [(null? xs) #f ]
34
+ [(equal? (+ x y (first xs)) 2020 )
35
+ (* x y (first xs))]
36
+ [else (compare-three x y (rest xs))]))
37
+
38
+
39
+
You can’t perform that action at this time.
0 commit comments