ruby - Finding palindromes by adding its reversed to itself through x to y doing only depth number of calculation -
i'm having trouble doing challenge in ruby palindromes.
i looked many sources , think brain melted bit novice programmer.
lemme sum question :
so user beginning value ending value, , depth value.we take beginning value , check if number palindrome,if isn't add reversed itself( 20, reverse(20)=02, add , 22) , check number find if palindrome.if isn't take calculated number , add reversed itself.but can "depth" value times. , print if find palindrome through calculations print value ----> xxxx if not print value ----> special number.
i'm able find palindromes, through given , ending value.but cant implement depth thing.my problem looks lot setting limit on loops , calculating palindromes in c programming since i'm ruby novice cant make sense of c now.any appreciated.
edit : x y , beginning value ending value, if type 20 beginning , 30 ending value, check 20 if palindrome add reversed etc.once finish checking check next number, 21 22,23... 30.
def doit(n, max_tries) max_tries.times.each rev = n.to_s.reverse.to_i (n == rev) ? (return n) : n += rev end nil end doit(22, 1) #=> 22 doit(21, 1) #=> nil doit(21, 2) #=> 33 doit(137, 1) #=> nil doit(137, 2) #=> 868 doit(1373, 2) #=> nil doit(1373, 3) #=> 9119 doit(13732, 9) #=> nil doit(13732, 10) #=> 134202431
Comments
Post a Comment