Interesting Palindrome problem

26 replies on 2 pages. Most recent reply: Dec 4, 2013 1:17 AM by Arnav Kumar

 Previous Topic Next Topic
 Flat View: This topic has 26 replies on 2 pages [ 1 2 | » ]
 Dima Posts: 2 Nickname: dima Registered: Mar, 2003
Interesting Palindrome problem Posted: Mar 19, 2003 1:07 PM
Hello People!
I have an interesting problem for you to solve, since I could not do it myself. If anyone manages to do it please send me the code at kuznechik39@mail.com

PALINDROME PROBLEM

Write a program to print all the 5-digit palindrome numbers.
Note: A number with the same digits from either end is called a palindrome. Examples: 16261, 93439, 20102.

Thanks!!!

 Matt Gerrans Posts: 1153 Nickname: matt Registered: Feb, 2002
Re: Interesting Palindrome problem Posted: Mar 19, 2003 1:13 PM
So, what is the interesting part?

 Mike Pinkley Posts: 1 Nickname: joojoo Registered: Mar, 2003
Re: Interesting Palindrome problem Posted: Mar 19, 2003 3:48 PM
That's not that difficult. Are you trying to get someone else to write your homework for you?

 Kishori Sharan Posts: 211 Nickname: kishori Registered: Feb, 2002
Re: Interesting Palindrome problem Posted: Mar 19, 2003 6:48 PM
```public class Test  {

public static void main(String args[]) {
int lowerLimit = 10000 ;
int upperLimit = 99999 ;

String temp = "" ;
for ( int i = lowerLimit ; i <= upperLimit; i++ ) {
temp = String.valueOf ( i ) ;
if ( temp.charAt(0) == temp.charAt(4) && temp.charAt(1) == temp.charAt(3) ) {
System.out.println ( i ) ;
}
}
}
}
```

 Dima Posts: 2 Nickname: dima Registered: Mar, 2003
Re: Interesting Palindrome problem Posted: Mar 20, 2003 10:12 AM
Thanks!!!

 Matt Gerrans Posts: 1153 Nickname: matt Registered: Feb, 2002
Re: Interesting Palindrome problem Posted: Mar 20, 2003 11:55 AM
That wasn't very interesting. I think there are much more interesting ways to solve the same problem, using much fewer instructions. Try using that same solution on numbers up to 20 digits, for example.

 Kishori Sharan Posts: 211 Nickname: kishori Registered: Feb, 2002
Re: Interesting Palindrome problem Posted: Mar 20, 2003 4:31 PM
Keep your interesting solutions and fewer instruction with yourself. I am not interested in your so called efficient solutions with zero lines of code. You should understand that people asking such questions are beginners (most probably high school students) and they are just interested in submitting their assignments on time. By the way, you don't need to teach me how to solve palindrome problem of 20 digits in one line of code! Try to help these students if you can. But, don't make fun of them by passing sarcastic remarks.

 Matt Gerrans Posts: 1153 Nickname: matt Registered: Feb, 2002
Re: Interesting Palindrome problem Posted: Mar 20, 2003 6:02 PM
Let's not get our knickers in a twist.

There is no reason why beginners should be uninterested in learning how to write good code from the start. After all, why start out with the attitude that all you need is the bare minimum to "get it to work" when you can develop good habits from the beginning? Your best work will come from looking at and thinking about your first attempt and learning from it.

It is pretty rare in the real world that you get a problem with well-defined parameters that will never change. The whole year 2000 fiasco is a perfect example of this.

Also, the difference between a solution "that works" and one that is elegant and flexible may be the difference between a C and an A+ (if the professor has any sense of responsibility and aesthetics).

Anyway, you don't have to get all bent out of shape.

 Richard Jernigan Posts: 1 Nickname: lertulo Registered: May, 2003
Re: Interesting Palindrome problem Posted: May 30, 2003 7:29 PM
To continue learning, then, how about improving the algorithm's speed? Right now you're testing some 90,000 values; that's a lot for just 5-digit palindromes.

Here's a possible approach that might be fun for the learner to try to code: instead, you can loop just 1000 times and hit a palindrome every time. Pardon the pseudocode:

`   for lead = 10 .. 99 {      lead_reverse = swap(lead)   // so, 03 -> 30      for inner = 0 .. 9 {         print lead, inner, lead_reverse      }   }`

 Joe Black Posts: 1 Nickname: joeblack Registered: Jun, 2003
Re: Interesting Palindrome problem Posted: Jun 1, 2003 11:46 PM
This is mad. You dont have to test any numbers, just produce all palindroms in a row:

for(int i=1;i<10;i++){
for(int j=i;j<10;j++){
for(int l=j;l<10;l++)
System.out.println(""+i+j+l+j+i);
}}

 Merriodoc Brandybuck Posts: 225 Nickname: brandybuck Registered: Mar, 2003
Re: Interesting Palindrome problem Posted: Jun 2, 2003 6:32 AM
> This is mad. You dont have to test any numbers, just
> produce all palindroms in a row:
>
> for(int i=1;i<10;i++){
> for(int j=i;j<10;j++){
> for(int l=j;l<10;l++)
> System.out.println(""+i+j+l+j+i);
> }}

The inner loops (for j and l) need to go from 0 to 9. Do that and you're good to go. This algorithm will miss things like 20902. Or even 10001.

 Matt Gerrans Posts: 1153 Nickname: matt Registered: Feb, 2002
Re: Interesting Palindrome problem Posted: Jun 2, 2003 12:02 PM
Here is the answer I concocted, but never posted, when the question was first asked. Its the same idea as the last few posts: you don't need the brutish guess-and-test, you can just generate the correct results in the first place.

```/*
* MirrorWorlds.java
*
* Simple demo of generating "palindrome" numbers.
*
*
* Turning in this code as homework is a violation of ethical principals,
* and punishable by severe penalties, even if you change the variable
* names.   Feeding it to your dog is acceptible.
*/

public class MirrorWorlds
{
public static long [] getMirrorNumbers( int digits )
{
boolean odd = digits % 2 == 1 ? true : false;
digits = digits / 2 + digits % 2;

long maxnum = (long)Math.pow( 10, digits );
long minnum = maxnum / 10;

StringBuffer reversed = new StringBuffer();

long [] mirrorNumbers = new long[(int)(maxnum-minnum)];

for( long i = minnum; i < maxnum; i++ )
{
reversed.setLength(0);
reversed.insert(0,i);
if(odd)
reversed.setLength( (int)digits - 1 );
reversed.reverse();
reversed.insert(0,i);
mirrorNumbers[(int)(i-minnum)] = Long.parseLong(reversed.toString());
}

return mirrorNumbers;
}

public static void main( String [] args )
{
int digits = 5;

if( args.length >= 1 )
try
{
digits = Integer.parseInt(args[0]);
}
catch( Exception e )
{
System.out.println( "Can't parse command line, so we'll stick with the default." );
}

long [] mirrorNumbers = getMirrorNumbers( digits );

System.out.println( "All " + digits + "-digit mirror-numbers:\n" );
for( int i = 0; i < mirrorNumbers.length; i++ )
System.out.print( mirrorNumbers[i] + " " );
}
}
```

 zenykx Posts: 69 Nickname: zenykx Registered: May, 2003
Re: Interesting Palindrome problem Posted: Jun 2, 2003 1:26 PM
Sorry man but the solution above yours is more rapid.
Let's not forget the problem: find the palindrom numbers with 5 (and i underline 5) digits.
I doesn't appear to me that the problem ask for any generalization or something like this.
This is a kind of anti-pattern !!!
Firstly provide the solution problem ... later on you can redefine, redesign and so on!!!

Keep up the good solutions !!!

There are only solutions !
---
zenykx

 Kishori Sharan Posts: 211 Nickname: kishori Registered: Feb, 2002
Re: Interesting Palindrome problem Posted: Jun 2, 2003 2:59 PM
Here is how you can generate all five digits palindrome. I guess Mr. Matt Gerran gets paid by client on the basis of number of lines of codes he writes. He must be fooling his client everyday by writing hundreds lines of (...) code.

```public class PalindromeGenerator {
public static void main (String[] args) {

// i is the middle i.e. 3rd digit and it will vary from 0 to 9
for ( int i = 0; i < 10; i++ ) {

// j is the second and fourth digits and it will vary from 0 to 9
for ( int j = 0; j < 10; j++ ) {

// k is the first and fifth digits and it will vary from 1 to 9
// Note that 0 in first position is not a valid digit and so, we can
// discard 0 at fifth position too
for ( int k = 1 ; k < 10; k++ ) {
System.out.println ( "" + k + j + i + j + k );
}
}
}

}
}
```

 Manohar Posts: 3 Nickname: manukar Registered: Jun, 2003
Re: Interesting Palindrome problem Posted: Jun 3, 2003 4:38 AM
Just a minor enahnement to problem..What changes are to be made to print 5 digit palindromes numbers in ascending order?

 Flat View: This topic has 26 replies on 2 pages [ 1  2 | » ]
 Previous Topic Next Topic

 Web Artima.com