Hoi allemaal,
Ik las vanavond het volgende probleem bij een coder wedstrijd van Oracle:
De klasse namen e.d. kloppen niet helemaal volgens de specs. Maar het ging me alleen maar om het idee...
Wat vinden jullie van de code? En hoe zou die verder verbeterd kunnen worden?
Ik las vanavond het volgende probleem bij een coder wedstrijd van Oracle:
Ik had even niks te doen, dus ik dacht laat ik het eens proberen. Dit is de code waar ik op ben gekomen:Sample Problem Statement #2:
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
Example 3: N = 11, return 11
Example 4: N = 607, return 4444
Example 5: N = 196, return -1
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| public class Test {
private int count = 0;
public Test () {
for (long i = 0; i < 1000; i++) {
System.out.println("" + i + ": " + palindrome(i));
}
System.out.println("I found: " + count + " palindromes.");
}
public long palindrome(long find) {
if (find > 100000000) {
return -1;
}
if (isPalindrome(find)) {
count++;
return find;
} else {
find = find + reverse(find);
return palindrome(find);
}
}
public boolean isPalindrome(long find) {
String s = "" + find;
if (find == reverse(find)) {
return true;
}
return false;
}
public long reverse (long find) {
String s_value = "" + find;
s_value = reverse(s_value);
long return_val = -1;
try {
return_val = Long.parseLong(s_value);
} catch (NumberFormatException e) {
e.printStackTrace();
}
return Long.parseLong(s_value);
}
public String reverse(String find) {
char reversed[] = new char[find.length()];
for (int i = 0; i < find.length(); i++)
reversed[i] = find.charAt(find.length() - i - 1);
return new String(reversed);
}
public static void main(String args[]) {
new Test();
}
} |
De klasse namen e.d. kloppen niet helemaal volgens de specs. Maar het ging me alleen maar om het idee...
Wat vinden jullie van de code? En hoe zou die verder verbeterd kunnen worden?