The Artima Developer Community
Sponsored Link

Java Answers Forum
Interesting Palindrome problem

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

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
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 4:07 PM
Reply to this message Reply
Advertisement
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 4:13 PM
Reply to this message Reply
So, what is the interesting part?

Mike Pinkley

Posts: 1
Nickname: joojoo
Registered: Mar, 2003

Re: Interesting Palindrome problem Posted: Mar 19, 2003 6:48 PM
Reply to this message Reply
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 9:48 PM
Reply to this message Reply
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 1:12 PM
Reply to this message Reply
Thanks!!!

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Interesting Palindrome problem Posted: Mar 20, 2003 2:55 PM
Reply to this message Reply
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 7:31 PM
Reply to this message Reply
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 9:02 PM
Reply to this message Reply
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 10:29 PM
Reply to this message Reply
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 2, 2003 2:46 AM
Reply to this message Reply
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 9:32 AM
Reply to this message Reply
> 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.

I love a little madness.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Interesting Palindrome problem Posted: Jun 2, 2003 3:02 PM
Reply to this message Reply
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.
 *
 * Copyright 2003, Matt Gerrans.
 *
 * 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 4:26 PM
Reply to this message Reply
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 5:59 PM
Reply to this message Reply
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 7:38 AM
Reply to this message Reply
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 | » ]
Topic: working with decimal point numbers Previous Topic   Next Topic Topic: promotion forum question


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us