Created
December 6, 2024 04:17
-
-
Save tail-call/d9c5110529dd94459a902d190b382814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(define/contract (reverse x) | |
(-> exact-integer? exact-integer?) | |
(cond ((= x 0) 0) | |
((< x 0) (- (reverse (- x)))) | |
(else (let ((reduced-x (floor (/ x 10))) | |
(digit (remainder (floor x) 10)) | |
(z (expt 10 (round (log x 10))))) | |
(print (list x reduced-x digit z)) | |
(+ (inexact->exact (* digit z)) (reverse reduced-x)))))) | |
(reverse 123) | |
(reverse -123) | |
(reverse 120) | |
(reverse 900000) | |
'(12 3 100.0)'(1 2 10.0)'(0 1 1)321 | |
'(12 3 100.0)'(1 2 10.0)'(0 1 1)-321 | |
'(12 0 100.0)'(1 2 10.0)'(0 1 1)21 | |
'(90000 0 1000000.0)'(9000 0 100000.0)'(900 0 10000.0)'(90 0 1000.0)'(9 0 100.0)'(0 9 10.0)90 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Attempt at a leetcode, failed attempt using log