Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   palindrome (http://www.velocityreviews.com/forums/t603444-palindrome.html)

Benjamin Rushing 04-01-2008 11:16 PM

palindrome
 
I've been tasked with writing a simple java program where the user inputs a
5 digit number and the program states whether it's a palindrome or not.
I can't use an array or use the 'reverse' function of code. Anybody willing
to give me a hint on how to procede?



Peter Duniho 04-01-2008 11:29 PM

Re: palindrome
 
On Tue, 01 Apr 2008 16:16:49 -0700, Benjamin Rushing
<benrushing75@verizon.net> wrote:

> I've been tasked with writing a simple java program where the user
> inputs a
> 5 digit number and the program states whether it's a palindrome or not.


Tasked by whom? A teacher assigning homework?

> I can't use an array or use the 'reverse' function of code.


Does the person assigning the task consider a string to be an array? How
about other collection types? Do they mean that you're only prohibited
from using things that are literally arrays? Or is any collection of data
that uses array-like semantics a problem for some reason?

> Anybody willing to give me a hint on how to procede?


It seems to me that the easiest way is to take the input data, and
iterative compare pairs of characters from the beginning and ending of the
string. If you reach the middle of the string and every character
matched, then it's a palindrome. If you find two characters that don't
match, it's not.

As long as you are allowed to use a string in your processing, the above
should be simple enough. Between the String class, a for() loop, and an
if() that controls the flow for each character comparison, you've got
everything you need.

Pete

Roedy Green 04-02-2008 12:05 AM

Re: palindrome
 
On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
<benrushing75@verizon.net> wrote, quoted or indirectly quoted someone
who said :

> Anybody willing
>to give me a hint on how to procede?


you will use a loop that will chase left to right and compute the
corresponding right to left char.

Draw some on paper, writing down the indexes of the characters.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Eric Sosman 04-02-2008 01:12 AM

Re: palindrome
 
Benjamin Rushing wrote:
> I've been tasked with writing a simple java program where the user inputs a
> 5 digit number and the program states whether it's a palindrome or not.
> I can't use an array or use the 'reverse' function of code. Anybody willing
> to give me a hint on how to procede?


class DetectPalindrome {
public static void main(String[] args) {
int number = Integer.parseInt(args[0]);
System.out.print(number + " is ");
if (number < 0)
System.out.println("not a palindrome.");
else
System.out.println("a palindrome "
+ " when written in base "
+ Math.max(number + 1L, 2));
}
}

There are, I suppose, less elegant approaches involving
the % operator, or conversion to String form and comparing
the results of carefully-chosen charAt() methods. Ho-hum.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Logan Shaw 04-02-2008 02:53 AM

Re: palindrome
 
Benjamin Rushing wrote:
> I've been tasked with writing a simple java program where the user inputs a
> 5 digit number and the program states whether it's a palindrome or not.

^^^^^^^^^^^^^^

Guaranteed to be exactly 5 digits? Use this to your advantage!

- Logan

Roedy Green 04-02-2008 02:36 PM

Re: palindrome
 
On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
<benrushing75@verizon.net> wrote, quoted or indirectly quoted someone
who said :

>I've been tasked with writing a simple java program where the user inputs a
>5 digit number and the program states whether it's a palindrome or not.
>I can't use an array or use the 'reverse' function of code. Anybody willing
>to give me a hint on how to procede?


Mathematician's algorithm:

generate all possible palindromes. Put them in an HashSet.

Look up the candidate. If it in is the set it is a Palindrome.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lord Zoltar 04-02-2008 02:45 PM

Re: palindrome
 
On Apr 1, 7:16*pm, "Benjamin Rushing" <benrushin...@verizon.net>
wrote:
> I've been tasked with writing a simple java program where the user inputs a
> 5 digit number and the program states whether it's a palindrome or not.
> I can't use an array or use the 'reverse' function of code. *Anybody willing
> to give me a hint on how to procede?


First, calculate the probability that any given 5 digit number COULD
be a palindrome.
Then ignore the input and output yes or no with the same probability
that the input MIGHT be a palindrom.
For very large datasets, this might be sufficient! ;)


seriously... if it's ALWAYS going to be 5 characters then it's very
easy: compare 1st and last character, 2nd and 3rd character. you could
say:
(input[0]==input[4] && input[1]==input[3]) => palindrome
you will have to find some way to split up your input string... If
you're not sure how then check through Java's string library. There's
several ways to do this. ;)

Lord Zoltar 04-02-2008 02:49 PM

Re: palindrome
 
On Apr 1, 9:12*pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> Benjamin Rushing wrote:
> > I've been tasked with writing a simple java program where the user inputs a
> > 5 digit number and the program states whether it's a palindrome or not.
> > I can't use an array or use the 'reverse' function of code. *Anybody willing
> > to give me a hint on how to procede?

>
> * * * * class DetectPalindrome {
> * * * * * * public static void main(String[] args) {
> * * * * * * * * int number = Integer.parseInt(args[0]);
> * * * * * * * * System.out.print(number + " is ");
> * * * * * * * * if (number < 0)
> * * * * * * * * * * System.out.println("not a palindrome.");
> * * * * * * * * else
> * * * * * * * * * * System.out.println("a palindrome "
> * * * * * * * * * * * * + " when written in base "
> * * * * * * * * * * * * + Math.max(number + 1L, 2));
> * * * * * * }
> * * * * }
>
> * * *There are, I suppose, less elegant approaches involving
> the % operator, or conversion to String form and comparing
> the results of carefully-chosen charAt() methods. *Ho-hum.
>
> --
> Eric Sosman
> esos...@ieee-dot-org.invalid


Actually, I think I like this solution the best!


Roedy Green 04-02-2008 03:55 PM

Re: palindrome
 
On Tue, 01 Apr 2008 23:16:49 GMT, "Benjamin Rushing"
<benrushing75@verizon.net> wrote, quoted or indirectly quoted someone
who said :

>I've been tasked with writing a simple java program where the user inputs a
>5 digit number and the program states whether it's a palindrome or not.
>I can't use an array or use the 'reverse' function of code. Anybody willing
>to give me a hint on how to procede?


Slacker's solution:

Reading the number as a string is no problem. It is extra work to read
it as an int.

See http://mindprod.com/applet/fileio.html

reverse the string. Compare.

To reverse the string without using reverse, cheat, and see how
reverse does it by looking at the code in source.zip

But the point is to LEARN java, not create a palindrome detector, so
this is a stupid solution.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


All times are GMT. The time now is 10:45 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.