d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Topcoder Srm Help > Mass Overpay
Add Reply New Topic New Poll
Member
Posts: 3,193
Joined: Apr 28 2010
Gold: 242.05
Apr 25 2013 10:10pm
This is a repost of my old topic, looking for someone to help me do the problem for the next SRM around 12h from this post.

Raising my payment to 1000 fg for a medium problem, 2000 fg for a hard problem for division II. PM me if interested as I may not check this post. Would prefer if you can do it in Java.

sample problem:

Code
A palindrome is a number that is the same whether it is read from left-to-right or right-to-left. For example, 121 and 34543 are both palindromes. It turns out that nearly every integer can be transformed into a palindrome by reversing its digits and adding it to the original number. If that does not create a palindrome, add the reverse of the new number to itself. A palindrome is created by repeating the process of reversing the number and adding it to itself until the number is a palindrome.

Create a class Transform that contains the method palindrome, which takes a number N that is to be transformed and returns a number that is the resultant palindrome from this process. Of course if N is already a palindrome, return it without changing it. Though it is theorized that all numbers can be transformed to palindromes in this way, some numbers do not converge in a reasonable amount of time. For instance, 196 has been carried out to 26,000 digits without finding a palindrome. So if the method finds that the resultant palindrome must be greater than 1,000,000,000, return the special value -1 instead.

DEFINITION
Class: Transform
Method: palindrome
Parameters: int
Returns: int
Method signature (be sure your method is public): int palindrome(int N);

NOTES
- Leading zeroes are never considered part of a number when it is reversed. For instance, 12's reverse will always be 21 regardless of whether it is represented as 12, 012, or 0012. Examples with leading zeroes use the leading zeroes for clarity only.

TopCoder will ensure the validity of the inputs. Inputs are valid if all of the following criteria are met:
- N will be between 1 and 10000 inclusive.

EXAMPLES
Worked examples:
Example 1: N = 28
28 + 82 = 110
110 + 011 = 121, a palindrome. Return 121

Example 2: N = 51
51 + 15 = 66, a palindrome. Return 66

Further examples:
Example 3: N = 11, return 11
Example 4: N = 607, return 4444
Example 5: N = 196, return -1


This post was edited by Naiva on Apr 25 2013 10:15pm
Member
Posts: 1,783
Joined: Dec 26 2002
Gold: 5,501.88
Apr 26 2013 12:33am
Language?

The idea behind it in javascript:
Code
function find_pal(N) {
   N = parseInt(N, 10);
   if (is_palindrome(N)) return N;
   return find_pal(N + reverse(N));
}

function is_palindrome(num) {
   num = parseInt(num, 10);

   if (num === reverse(num)) return true;

   return false;
}

function reverse(num) {
     parseInt(num.split("").reverse().join(""), 10);
}


If you were to do it in C++ it's still pretty easy and not much longer. And you could easily meet the class requirements.

It says create a class, but converting that to a class is trivial. I'm aware that it is a sample problem and not your actual problem. "Create a class", does that imply it's in Java or C#? If so I have no experience with Java besides reading and porting some of the code, and I haven't had a need to play with C#.

This post was edited by Dishonru on Apr 26 2013 12:36am
Go Back To Homework Help Topic List
Add Reply New Topic New Poll