Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

program to print "one" when input is 1.so on upto 100.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanna write a simple java program so that if i give the below input,I should get the following output
Input->output
1->one
2->two
3->three
so on till...
100 hundred,If possible I want to extend this to 1000 numbers.
When my friend asked this,I tried in if,switch,ASCII formats,but went in vain....
because the rule is:
code must be as simple as possible within 10 lines or so...!
Anyone got any Idea???
 
author
Posts: 23897
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raghavendra raghu wrote:I wanna write a simple java program so that if i give the below input,I should get the following output
Input->output
1->one
2->two
3->three
so on till...
100 hundred,If possible I want to extend this to 1000 numbers.
When my friend asked this,I tried in if,switch,ASCII formats,but went in vain....
because the rule is:
code must be as simple as possible within 10 lines or so...!
Anyone got any Idea???



Well, if the goal is the number of lines, then you can do it in two lines. One line to define the look up table as an array of strings. And one line to do the table look up... okay, plus a couple more lines to read input and print output.

In my opinion, I think it is best to get it working first. Don't worry about the number of lines. This way, you understand the problem, the details of it, and of course, your solution to it. You can refactor it to the "10 lines" restriction, once you have a working solution.

Henry
 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

One line to define the look up table as an array of strings. And one line to do the table look up...



If the input is going to be upto 1000, then hard-coding 1000 strings kills the joy of programming.

I might approach in a different way, have three arrays of strings, one containing "one","two"..."ten", next containing "eleven","twelve",..."nineteen", and the last containing "twenty","thirty"...."ninety".

Then we could use "/" and "%" operators and manipulate the code to get the result more efficiently.
 
Henry Wong
author
Posts: 23897
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashwin Sridhar wrote:

One line to define the look up table as an array of strings. And one line to do the table look up...



If the input is going to be upto 1000, then hard-coding 1000 strings kills the joy of programming.



Was kinda only focusing in on the "number of lines" requirement -- and maybe doing a little "reductio ad absurdum" argument as well.... the point was to not focus on that requirement until later.

Henry
 
lowercase baba
Posts: 12974
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You sound like you are trying to do the CattleDrive assignment 4a: Say.

Even that one is 77 lines...

Focusing on the number of lines is silly. it encourages bad practices. It might make you do things like this:

import packageA.*;import packageB.*;import packageC.*;

or even

for (int x-1;x<=100;x++){doSomething();doSomethingElse();doAThridThing();}
 
raghavendra raghu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:You sound like you are trying to do the CattleDrive assignment 4a: Say.

Even that one is 77 lines...

Focusing on the number of lines is silly. it encourages bad practices. It might make you do things like this:

import packageA.*;import packageB.*;import packageC.*;

or even

for (int x-1;x<=100;x++){doSomething();doSomethingElse();doAThridThing();}



Thanks for your reply fred.Iam actually haunting for various ways of trying different solutions for this.One would suggest that the easiest way to get down to this is:printing 100 statements like if(1) then System.out.println("one"); and so on.but this is a good way of writing bad code.YUCK!I dont want to import packages neither!
 
raghavendra raghu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Well, if the goal is the number of lines, then you can do it in two lines. One line to define the look up table as an array of strings. And one line to do the table look up... okay, plus a couple more lines to read input and print output.

In my opinion, I think it is best to get it working first. Don't worry about the number of lines. This way, you understand the problem, the details of it, and of course, your solution to it. You can refactor it to the "10 lines" restriction, once you have a working solution.

Henry


Thanks henry for your prompt reply.could you put more effort on explaining the code part..I dont know how to implement look up table indeed!I just put those 10 lines rule to make things much easier...no worries in removing that...if the solution is simpler...!
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raghavendra raghu wrote:

Henry Wong wrote:
One line to define the look up table as an array of strings. And one line to do the table look up... okay, plus a couple more lines to read input and print output.


I dont know how to implement look up table indeed


If you read the above line, you will get the answer.
 
raghavendra raghu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashwin Sridhar wrote:

One line to define the look up table as an array of strings. And one line to do the table look up...



If the input is going to be upto 1000, then hard-coding 1000 strings kills the joy of programming.

I might approach in a different way, have three arrays of strings, one containing "one","two"..."ten", next containing "eleven","twelve",..."nineteen", and the last containing "twenty","thirty"...."ninety".

Then we could use "/" and "%" operators and manipulate the code to get the result more efficiently.



Thanks Ashwin...but I think I forot to mention you input is 1---100 and output is the strings part(one,two...).but you are creating strings on 1st hand?
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raghavendra raghu wrote:

Ashwin Sridhar wrote:

One line to define the look up table as an array of strings. And one line to do the table look up...



If the input is going to be upto 1000, then hard-coding 1000 strings kills the joy of programming.

I might approach in a different way, have three arrays of strings, one containing "one","two"..."ten", next containing "eleven","twelve",..."nineteen", and the last containing "twenty","thirty"...."ninety".

Then we could use "/" and "%" operators and manipulate the code to get the result more efficiently.



Thanks Ashwin...but I think I forot to mention you input is 1---100 and output is the strings part(one,two...).but you are creating strings on 1st hand?


So that he can retrieve the String value for the corresponding input from the stored String array..
Because, you need only these strings to name all the numbers from 1 to 100 in words.. Just concatenate them to get appropriate number..
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raghavendra raghu wrote:Thanks henry for your prompt reply.could you put more effort on explaining the code part..I dont know how to implement look up table indeed!I just put those 10 lines rule to make things much easier...no worries in removing that...if the solution is simpler...!


I think you're missing his point: the fact is that you can write almost any program in one line - just don't put in any carriage returns.

Whether anyone will want to read it or not (including you) is another matter; which is why the "10 lines" business is complete nonsense.

If you write a GOOD program, no matter how many lines it has, it will almost certainly be better than your friend's 10-liner, because you'll be concentrating on doing the job properly, not in showing off how short you can make it.

And the first part of that task is to understand how we write numbers in longhand. And that you should figure out with a pencil and paper, NOT in Java.

Winston

PS: I would say to your friend that you cannot write a good generic program to do this in 10 lines without "cheating" (ie, lumping multiple statements or entire method definitions on one line).
 
raghavendra raghu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R. Jain wrote:
So that he can retrieve the String value for the corresponding input from the stored String array..
Because, you need only these strings to name all the numbers from 1 to 100 in words.. Just concatenate them to get appropriate number..



yes.I will try writing that in ashwin and henry's way..!
 
raghavendra raghu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
I think you're missing his point: the fact is that you can write almost any program in one line - just don't put in any carriage returns.

Whether anyone will want to read it or not (including you) is another matter; which is why the "10 lines" business is complete nonsense.

If you write a GOOD program, no matter how many lines it has, it will almost certainly be better than your friend's 10-liner, because you'll be concentrating on doing the job properly, not in showing off how short you can make it.

And the first part of that task is to understand how we write numbers in longhand. And that you should figure out with a pencil and paper, NOT in Java.

Winston

PS: I would say to your friend that you cannot write a good generic program to do this in 10 lines without "cheating" (ie, lumping multiple statements or entire method definitions on one line).




obviously true Winston:Iam not looking for a thin liner program,rather Iam searching for various ways to make it thin and efficient also.I dont mind if it is huge and efficient!any other way you can write this?now I got 3 nice ways to do so!Thanks all for your overwhelming response!
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raghavendra raghu wrote:Iam searching for various ways to make it thin and efficient also.I dont mind if it is huge and efficient!any other way you can write this?now I got 3 nice ways to do so!


Again, DON'T worry about efficiency. The task is inherently inefficient, since you already have a perfectly good way of representing a number - by its digits - so don't obsess about speed.

Make it correct and flexible - and one way to do that is to consider how much you would have to change it if the limit were 1,000,000,000 instead of 100.

Winston
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic