From d2ef0b367822c69e7acc43de09eafe0dac760eca Mon Sep 17 00:00:00 2001 From: zafar-hussain Date: Thu, 8 Oct 2020 20:37:18 +0500 Subject: [PATCH] Adding solution for issue #36 'Check Palindrome of integers' --- problems/palindrome/isPalindrome?.rkt | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 problems/palindrome/isPalindrome?.rkt diff --git a/problems/palindrome/isPalindrome?.rkt b/problems/palindrome/isPalindrome?.rkt new file mode 100644 index 00000000..1c050467 --- /dev/null +++ b/problems/palindrome/isPalindrome?.rkt @@ -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)