Define a predicate same-digits? (a b) which returns #t if the integers a and b contain the same digits. If a digit occurs n times in a, it must also occur n times in b. For example, (same-digits? 133042 420313) → #t, but (same-digits? 133042 42013) → #f.
- Define a predicate same-digits? (a b) which returns #t if the integers a and b contain the same digits. If a digit occurs n times in a, it must also occur n times in b. For example, (same-digits? 133042 420313) → #t, but (same-digits? 133042 42013) → #f.
To solve the problem, one technique would be to add some extra procedures for manipulating digits inside a number. Here are some hints:
- To find the number of digits in a number, count how many times you need to divide it by 10 until you reach 0.
- To extract the second digit of 5784: floor( 5784/102 ) mod 10=7. Note that mod in racket is modulo or remainder
- To assemble the digits 3, 4, and 5 into a number: 3×102+4×101+5×100 = 345
The standard Scheme procedures modulo and floor may prove useful. The solution only needs to consider non-negative integers.
Or..... you could "cheat" and convert each number to a list, sort, and check for equality.
Given:-
# source_code
# lang racket/base
# lang racket/base
(define(reverse_digit n)
(define(loop n reserved_Num)
(if (<=n 0 )
reversed_ Num
(let((lastDigit(modulo n 10)))
(loop(/(-n lastDigit)10)(+(*reversed_Num 10)lastDigit)))))
(loop n 0)
(reserve_digit 397164)
Output:
461793
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images