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-504-base7.rkt | |
parent | bc5387512ba529072338648d6337ddab731b8b8c (diff) | |
download | gleam_aoc-fafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1.tar.gz gleam_aoc-fafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1.zip |
adding old leetcode solutions
Diffstat (limited to 'leetcode/lc-504-base7.rkt')
-rw-r--r-- | leetcode/lc-504-base7.rkt | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/leetcode/lc-504-base7.rkt b/leetcode/lc-504-base7.rkt new file mode 100644 index 0000000..3e75052 --- /dev/null +++ b/leetcode/lc-504-base7.rkt @@ -0,0 +1,16 @@ +#lang racket + +(define/contract (convert-to-base7 num) + (-> exact-integer? string?) + (define (max-base-power n base [pow 1]) + (cond [(n . = . (expt base pow)) pow] + [(n . < . (expt base pow)) (sub1 pow)] + [else (max-base-power n base (add1 pow))])) + (define (add-next-digit n pow acc) + (cond [(= pow 0) (string-append acc (number->string n))] + [else (add-next-digit (remainder n (expt 7 pow)) + (sub1 pow) + (string-append acc + (number->string (quotient n (expt 7 pow)))))])) + (string-append (if (negative? num) "-" "") + (add-next-digit (abs num) (max-base-power (abs num) 7) "")))
\ No newline at end of file |