Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Functional Programming
diff options
context:
space:
mode:
Commit083017dac65fda5c171ca0b223686f0f1773a157 (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-10-20 11:48:04 +0300
Committer
 Syndamia <kamen@syndamia.com>
2022-10-20 11:48:04 +0300
Filesd52f242efc80459720ba22d8ef3a4408e4bffca4 /Year 2/Functional Programming
uni-projects-083017dac65fda5c171ca0b223686f0f1773a157.tar
uni-projects-083017dac65fda5c171ca0b223686f0f1773a157.tar.gz
uni-projects-083017dac65fda5c171ca0b223686f0f1773a157.zip
Parentb503560635cc91092bc9e93d76011338afa36656 (diff)
[FP/HW1] Reworked variable/parameter names and added examples
1 files changed, 33 insertions, 17 deletions
-rw-r--r--Year 2/Functional Programming/hw1.rkt50
diff --git a/Year 2/Functional Programming/hw1.rkt b/Year 2/Functional Programming/hw1.rkt
index 3d3e0e2..46bed91 100644
--- a/Year 2/Functional Programming/hw1.rkt
+++ b/Year 2/Functional Programming/hw1.rkt
@@ -3,39 +3,46 @@
;;; Задача 1
(define (sum-digits n)
- (define (calc-iter n total)
- (if (= n 0)
+ (define (calc-iter working-n total)
+ (if (= working-n 0)
total
- (calc-iter (quotient n 10) (+ total (remainder n 10)))))
+ (calc-iter (quotient working-n 10) (+ total (remainder working-n 10)))))
(calc-iter n 0))
(define (count-specials k a b)
(if (or (<= k 0) (> a b))
0
(+ (count-specials k (add1 a) b)
- (if (and (= 0 (remainder a k))
- (= 0 (remainder (sum-digits a) k)))
- 1 0))))
+ (if (and (= 0 (remainder a k)) (= 0 (remainder (sum-digits a) k)))
+ 1 0)))) ; ако е k-специално, добавяме единица към бройката, иначе не
+
+;; Примери
+(count-specials 3 3 9)
+(count-specials 5 10 100)
+(count-specials 8 100 200)
+(count-specials 15 1000 2000)
+
+
;;; Задача 2
(define (^10 n)
- (define (calc-iter n val)
- (if (= n 0)
+ (define (calc-iter power val)
+ (if (= power 0)
val
- (calc-iter (sub1 n) (* val 10))))
+ (calc-iter (sub1 power) (* val 10))))
(calc-iter n 1))
(define (numlen n)
- (define (calc-iter n len)
- (if (= n 0)
+ (define (calc-iter working-n len)
+ (if (= working-n 0)
len
- (calc-iter (quotient n 10) (add1 len))))
+ (calc-iter (quotient working-n 10) (add1 len))))
(calc-iter n 0))
(define (rotate-left n)
(define 10^len-1 (^10 (sub1 (numlen n))))
- (+ (* (remainder n 10^len-1) 10) ; всичко без най-дясната цифра
+ (+ (* (remainder n 10^len-1) 10) ; всичко без най-дясната цифра * 10
(quotient n 10^len-1))) ; най-дясната цифра
(define (rotate-left-preserved n d)
@@ -43,10 +50,10 @@
(define to-rotate (remainder n 10^rotatelen))
(if (<= n 9)
n
- (+ (- n to-rotate) ; запазените цифри са запазени, другите са нули
- (if (< to-rotate (quotient 10^rotatelen 10)) ; ако to-rotate "започва" с нули
- (- (rotate-left (+ to-rotate 10^rotatelen)) 1) ; "долепяме" 1ца до левия край временно, за да може да се звърти
- (rotate-left to-rotate)))))
+ (+ (- n to-rotate) ; незапазените цифри стават нули
+ (if (< to-rotate (quotient 10^rotatelen 10)) ; ако to-rotate "започва" с нули
+ (- (rotate-left (+ to-rotate 10^rotatelen)) 1) ; временно "долепяме" 1ца до левия край, за да може незапазените да се звъртят правилно
+ (rotate-left to-rotate))))) ; иначе просто завъртаме незапазените
(define (max-rot n)
(define len-1 (sub1 (numlen n)))
@@ -57,3 +64,12 @@
(rotate-left-preserved current (add1 preserved))
(if (> current biggest) current biggest))))
(calc-iter 0 (rotate-left n) 0))
+
+;; Примери
+(max-rot 56789)
+(max-rot 12490)
+(max-rot 38458215)
+(max-rot 195881031)
+(max-rot 896219342)
+(max-rot 69418307)
+(max-rot 257117280)