aboutsummaryrefslogtreecommitdiff
path: root/leetcode/lc-415-add-strings.rkt
diff options
context:
space:
mode:
Diffstat (limited to 'leetcode/lc-415-add-strings.rkt')
-rw-r--r--leetcode/lc-415-add-strings.rkt28
1 files changed, 0 insertions, 28 deletions
diff --git a/leetcode/lc-415-add-strings.rkt b/leetcode/lc-415-add-strings.rkt
deleted file mode 100644
index e140155..0000000
--- a/leetcode/lc-415-add-strings.rkt
+++ /dev/null
@@ -1,28 +0,0 @@
-#lang racket
-
-(define/contract (add-strings num1 num2)
- (-> string? string? string?)
- (define (char->integer c)
- ((compose string->number string) c))
- (define pad-length
- (add1 (apply max (map string-length (list num1 num2)))))
- (define (pad-with-zeroes n)
- (~a n
- #:align 'right
- #:min-width pad-length
- #:pad-string "0"))
- (define (string-reverse s)
- ((compose list->string reverse string->list) s))
- (define raw-sum
- (for/fold ([sum-string ""]
- [carry 0]
- #:result sum-string)
- ([n1 (string-reverse (pad-with-zeroes num1))]
- [n2 (string-reverse (pad-with-zeroes num2))])
- (let* ([digit-sum (+ carry (char->integer n1) (char->integer n2))]
- [sum-place (number->string (modulo digit-sum 10))]
- [sum-carry (quotient digit-sum 10)])
- (values (string-append sum-place sum-string)
- sum-carry))))
- (cond [(equal? raw-sum "00") "0"]
- [else (string-trim raw-sum "0" #:repeat? #t #:right? #f)])) \ No newline at end of file