diff options
Diffstat (limited to 'racket/leetcode/lc-415-add-strings.rkt')
-rw-r--r-- | racket/leetcode/lc-415-add-strings.rkt | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/racket/leetcode/lc-415-add-strings.rkt b/racket/leetcode/lc-415-add-strings.rkt new file mode 100644 index 0000000..e140155 --- /dev/null +++ b/racket/leetcode/lc-415-add-strings.rkt @@ -0,0 +1,28 @@ +#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 |