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 ]
Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

Re: Interesting Palindrome problem Posted: Jun 3, 2003 10:33 AM
Reply to this message Reply
Advertisement
> Just a minor enahnement to problem..What changes are to be
> made to print 5 digit palindromes numbers in ascending
> order?

I think the easy answer is, instead of calling System.out.println, make each number a new entry into a string array, hash table, or some such collection, sort the collection, then print that. This will also be easily adaptable for when you next ask "what about printing them in descending order?". You could also slap together a random number generator that spans the indices of the array and print them out in random order, too. Or start in the middle then print them in some funky inside out manner.

It's been a long, long time since I wrote any Java at all, so I'll have to kindly leave the implementation up to somebody more knowledgeable in the language. I'm just assuming you can somewhat easily do the above.

I'm sure there is a much more efficient method, but after 3 seconds of thought, that's one option.

I'm sure Mr. Gerrans can come up with a much more robust and flexible solution ;-) Poor guy comes up with a fancy robust solution and get ripped for it. Life is hard, sometimes.

Manohar

Posts: 3
Nickname: manukar
Registered: Jun, 2003

Re: Interesting Palindrome problem Posted: Jun 4, 2003 12:02 AM
Reply to this message Reply
I appreciate collection and sort alg..
There is one more way of doing this..

Change loop structure as

for(k=1;k<10;k++)
for(j=0;j<10;j++)
for(i=0;i<10;i++)
System.out(...)

10001,10101,10201,10301,....,11011,11111,.....


Descending order
for(k=9;k>=1;k--)
for(j=9;j>=0;j--)
for(i=9;i>=0;i--)
System.out(...)

99999,99899,99799,...,98989,98889,....,89998,...,....

John Channing

Posts: 17
Nickname: drc
Registered: Jun, 2003

Re: Interesting Palindrome problem Posted: Jun 5, 2003 7:09 AM
Reply to this message Reply
For a slightly more mathematical approach which gives you access to the numbers rather than just a string.

public static void main( String[] args )
{
for( int i=0; i<=9; ++i )
for( int j=0; j<=9; ++j )
for( int k=0; k<=9; ++k )
{
int num = 1010*i + 10001*j + 100*k;
if( num>10000 ) System.out.println( num );
}
}

John Channing

Posts: 17
Nickname: drc
Registered: Jun, 2003

Re: Interesting Palindrome problem Posted: Jun 5, 2003 11:35 AM
Reply to this message Reply
Here is a slightly tweaked ordered solution, it's also very fast.
John

public static void main( String[] args )
{
ArrayList pals = new ArrayList();
for( int j=1; j<10; ++j )
for( int k=0; k<10; ++k )
for( int i=0; i<10; ++i )
pals.add( new Integer( 1010*i + 10001*j + 100*k ) );

Collections.sort( pals );
for( Iterator itr = pals.iterator(); itr.hasNext(); )
System.out.println( itr.next() );
}

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Interesting Palindrome problem Posted: Jun 5, 2003 11:09 PM
Reply to this message Reply
Let us close this message until Mr. Geran(s) comes up with some new robust and fancy solution!!!

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Interesting Palindrome problem Posted: Jun 16, 2003 8:07 AM
Reply to this message Reply
> 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
.

I think everyone knows that all these people want is to have someone else do their homework for them (which they will then present as their own work).

> Try to help these students if you can.

Why? Such students are dishonest. In asking for the answer on a plate, they are trying to cheat. In artificially boosting their own grades, they may even be having a detrimental effect on the grades of honest students.

Presenting a fully worked out solution:
a) condones such behaviour,
b) places honest students at a disadvantage.

It is well established in coding forums that if you really want to help student with their homework then you ask them what precisely is stooping them and then present them with enough information to help themselves.

(For what it's worth, since the damage has been done, here's (one way) I might have done it.)
public class Palindrome
{
    public static void main(String[] args)
    {
        for (int i = 000; i<=999; i++) 
            System.out.println((i/100)+""+((i/10)%10)+""+(i%10)+""+((i/10)%10)+""+(i/100));
    }
}


Vince.

Greg McClure

Posts: 3
Nickname: chromex
Registered: Jun, 2003

Re: Interesting Palindrome problem Posted: Jun 16, 2003 2:10 PM
Reply to this message Reply
Yeah, I tend to agree. There are plenty of places online where students can go online and "get hints" for their homework problems. At the very least we should force these students to be just a wee bit more subtle than, "Here's an interesting problem!"--oh, he fooled us there, but this is the killer--"Please email me the solution."

I also agree with many who say that this isn't an interesting problem. It really isn't. But it could be made a little more interesting by, say, posing this problem from USACO that was similar:

Generate all PRIME palindromes from 2 to 100,000,000.

Not very difficult, but it certainly raises the stakes over the previous ...

Hey, I have another interesting problem! Try paying all my bills on my income! If you come up with any money, please mail it to me.

Peter Hickman

Posts: 41
Nickname: peterhi
Registered: Mar, 2003

Re: Interesting Palindrome problem Posted: Jun 16, 2003 4:13 PM
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?

Just reading the code makes me think that it will do this already. Or have I missed something.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Interesting Palindrome problem Posted: Jun 17, 2003 5:46 AM
Reply to this message Reply
> int lowerLimit = 10000 ;
> int upperLimit = 99999 ;


This solution doesn't include 5 digit palindromes in the range 00000 to 09999 (e.g. 00000, 00100, etc.)?

I wonder if he'll lose marks for that?

V.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Interesting Palindrome problem Posted: Jun 17, 2003 6:19 PM
Reply to this message Reply
Placing two zeros before 100 doesn't make 100 as five digit number. I think you need to go back to school with Mr. Mat(t) Ger(r)an(s) to learn digits and numbers!!!

Dauphin Mil

Posts: 1
Nickname: dauphin
Registered: Nov, 2013

Re: Interesting Palindrome problem Posted: Nov 22, 2013 9:30 PM
Reply to this message Reply
public class Palindrome
{
public static void main (String[] args)
{
int n1, n2, n3, n4, n5;// first, second, third, fourth and fifth digits
//String sumNum; //string name for all int numbers
// sumNum = (n1 +"" +n2+"" +n3+"" +n4+""+n5);

for(n1 = 0; n1 < 10 ; n1++ )
for(n2 = 0; n2 < 10; n2++)
for(n3 = 0; n3 < 10; n3++)
for(n4 = 0; n4 < 10; n4++)
for(n5 = 0; n5 < 10; n5++)

if(n1 == n5)
if(n2 == n4)
{
System.out.println(n1 +"" +n2+"" +n3+"" +n4+""+n5);
}


}

I also have this homework and this is what I came up with.

Works perfect.

Arnav Kumar

Posts: 5
Nickname: arnavkumar
Registered: Nov, 2013

Re: Interesting Palindrome problem Posted: Dec 4, 2013 4:17 AM
Reply to this message Reply
The only interest is to see so much replies here without any interest.

Flat View: This topic has 26 replies on 2 pages [ « | 1  2 ]
Topic: servlet Previous Topic   Next Topic Topic: command-line argument


Sponsored Links



Google
  Web Artima.com   

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