-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding solution for issue #36 'Check Palindrome of integers'
- Loading branch information
1 parent
b7f990c
commit d2ef0b3
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#lang racket | ||
|
||
;Build a function that checks weather a given number is a palindrome or not without using a strings. Implement it using any language. | ||
; | ||
;For example: | ||
;input: 12321 | ||
;output: true | ||
; | ||
; input: 12344321 | ||
; output: True | ||
|
||
;; intution | ||
;; n and reverse n will be same for a palindrome | ||
;; will use reverse-number (see pr #341 for issue #35) | ||
|
||
;; I got the Idea from link below | ||
;; https://www.integers.co/questions-answers/is-12344321-a-palindrome-number.html | ||
|
||
|
||
|
||
;; code | ||
(define (isPalinDrome n) | ||
(local [ | ||
(define (reverse-n n acc) ; using an decerasing accumulator | ||
(cond [(= (order-of-magnitude n) 0) n] ; basecase, used to stop recursion | ||
[else | ||
(+ | ||
(* (modulo n 10) (expt 10 acc)) ; get the right-most digit of n, multiply it with 10^x | ||
(reverse-n (quotient n 10) (sub1 acc)))])) | ||
|
||
(define (reverse-number num) | ||
(reverse-n num (order-of-magnitude num))) | ||
] | ||
(= (reverse-number n) n))) | ||
|
||
; test examples | ||
;(equal? (isPalinDrome 9) true) | ||
;(equal? (isPalinDrome 12349321) false) | ||
;(equal? (isPalinDrome 12321) true) |