Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitd3ca8bf66eb67f21c511c856a7ac54c11d8b406a (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-11-17 16:54:07 +0200
Committer
 Syndamia <kamen@syndamia.com>
2022-11-17 16:54:07 +0200
Files882e7e85190e6bc819d4bddac1170ffc1d5ed0d6
uni-projects-d3ca8bf66eb67f21c511c856a7ac54c11d8b406a.tar
uni-projects-d3ca8bf66eb67f21c511c856a7ac54c11d8b406a.tar.gz
uni-projects-d3ca8bf66eb67f21c511c856a7ac54c11d8b406a.zip
Parentb098780fd1ec892283985a9c9300838edf866f8a (diff)
[FP] Solved first exam from 2021
1 files changed, 86 insertions, 0 deletions
-rw-r--r--Year 2/Functional Programming/Exam1-2021.rkt86
diff --git a/Year 2/Functional Programming/Exam1-2021.rkt b/Year 2/Functional Programming/Exam1-2021.rkt
new file mode 100644
index 0000000..e5b96e5
--- /dev/null
+++ b/Year 2/Functional Programming/Exam1-2021.rkt
@@ -0,0 +1,86 @@
+#lang racket
+
+;;; Exercise 1
+
+(define (^2 n)
+ (* n n))
+
+(define (contains-caar elem lst)
+ (define (calc lst ind)
+ (cond [(null? lst) #f)]
+ [(= (caar lst) elem) ind]
+ [else (calc (cdr lst) (add1 ind))])
+ (calc lst 0))
+
+(define (digit-occur n)
+ (define (calc n ret)
+ (cond [(= n 0) ret]
+ [()]
+ )))
+
+(define (get-distribution n)
+ (sort (digit-occur n) (lambda (x y) (< (car x) (car y)))))
+
+;;; Exercise 2
+
+(define (find-missing lst)
+ (cond [(null? lst) #f]
+ [(> (- (cadr lst) (car lst)) 1) (add1 (car lst))]
+ [else (find-missing (cdr lst))]))
+
+(define (get-missing-length xss)
+ (define lenlis (map (lambda (lst) (length lst)) xss))
+ (find-missing (sort lenlis <)))
+
+(get-missing-length '((1 2) (4 5 1 1) (1) (5 6 7 8 9))) ; → 3
+(get-missing-length '(("a", "a", "a") ("a", "a") ("a", "a", "a","a") ("a") ("a", "a", "a", "a", "a", "a"))) ; → 5
+
+;;; Exercise 3
+
+(define (biggest-5^k-lt n)
+ (define (calc current)
+ (if (> current n)
+ (quotient current 5)
+ (calc (* current 5))))
+ (calc 5))
+
+(define (trailing-of-n n)
+ (define (calc 5^k sum)
+ (if (= 5^k 1)
+ sum
+ (calc (quotient 5^k 5) (+ sum (floor (/ n 5^k))))))
+ (calc (biggest-5^k-lt n) 0))
+
+(define (trailing-zeros n)
+ (lambda (p)
+ (p (trailing-of-n n))))
+
+((trailing-zeros 6) even?) ; → #f (броят на влачещите нули е 1. 1 не е четно)
+((trailing-zeros 1000) even?) ; → #f (броят на влачещите нули е 249. 249 не е четно)
+((trailing-zeros 100000) even?) ; → #f (броят на влачещите нули е 24999. 24999 не е четно)
+((trailing-zeros 1000000000) even?) ; → #t (броят на влачещите нули е 249999998. 249999998 е четно)
+
+;;; Exercise 4
+
+(define (mult-digits n)
+ (define (calc n current)
+ (if (= n 0)
+ current
+ (calc (quotient n 10) (* current (remainder n 10)))))
+ (calc n 1))
+
+(define (list-md n)
+ (define (calc n res)
+ (if (< n 10)
+ res
+ (calc (mult-digits n) (append res (list (mult-digits n))))))
+ (calc n '()))
+
+(define (persistence n)
+ (define ys (if (< n 10) (list n) (list-md n)))
+ (cons ys (length ys)))
+
+(persistence 39) ; → '((27 14 4) . 3) ; 3*9=27, 2*7=14, 1*4=4
+(persistence 126) ; → '((12 2) . 2) ; 1*2*6=12, 1*2=2
+(persistence 4) ; → '((4) . 1)
+(persistence 999) ; → '((729 126 12 2) . 4)