diff options
author | HJ <thechairman@thechairman.info> | 2023-01-17 12:46:04 -0500 |
---|---|---|
committer | HJ <thechairman@thechairman.info> | 2023-01-17 12:46:04 -0500 |
commit | fafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1 (patch) | |
tree | d586a28650cbbfb20ece931a85bcce4c29e5e40b /leetcode/lc-43-multiply-strings.rkt | |
parent | bc5387512ba529072338648d6337ddab731b8b8c (diff) | |
download | gleam_aoc-fafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1.tar.gz gleam_aoc-fafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1.zip |
adding old leetcode solutions
Diffstat (limited to 'leetcode/lc-43-multiply-strings.rkt')
-rw-r--r-- | leetcode/lc-43-multiply-strings.rkt | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/leetcode/lc-43-multiply-strings.rkt b/leetcode/lc-43-multiply-strings.rkt new file mode 100644 index 0000000..dac8c31 --- /dev/null +++ b/leetcode/lc-43-multiply-strings.rkt @@ -0,0 +1,28 @@ +#lang racket + +(define/contract (char-digit->integer c) + (-> char? integer?) + (- (char->integer c) 48)) + +(define/contract (integer->string-digit n) + (-> integer? string?) + (string (integer->char (+ n 48)))) + +(define/contract (number->string1 n [acc ""]) + (->* (integer?) (string?) string?) + (cond [(and (= n 0) (equal? acc "")) "0"] + [(= n 0) acc] + [else (number->string1 + (quotient n 10) + (string-append (integer->string-digit (remainder n 10)) acc))])) + +(define/contract (multiply num1 num2) + (-> string? string? string?) + (define multiplication-steps + (for/list ([n1 (in-string num1)] + [place1 (in-range (sub1 (string-length num1)) -1 -1)]) + (for/list ([n2 (in-string num2)] + [place2 (in-range (sub1 (string-length num2)) -1 -1)]) + (apply * (append (map char-digit->integer (list n1 n2)) + (list (expt 10 place1) (expt 10 place2))))))) + (number->string1 (apply + (flatten multiplication-steps))))
\ No newline at end of file |