Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginners - would you like to do this question ?  RSS feed

 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOTE - Please post your solution here, even if someone has done it correctly before you.

Question - Make a method to print an array of integers in this format 3,7,8,9...

Thanks
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There you are. And even ******** ******** can’t do worse than that!
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There you are. And even ******** ******** can’t do worse than that!


Different from what I had in mind - a method that accepts an int array and then prints it in the form - a0,a1,a2,a3....aN.


 
Paul Clapham
Sheriff
Posts: 22471
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just goes to show... if you make up a question for beginners, you should write clear specifications. Otherwise you're going to have to argue with various people's interpretations of your unclear specifications.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what's the award for giving the answer you have in mind?
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kemal Sokolovic wrote:And what's the award for giving the answer you have in mind?

No reward. Just want to look at the different solutions people come up with for the same problem.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There you are. And even ******** ******** can’t do worse than that!

And if you use an Integer[], that last statement can be
System.out.printf("%d,%d,%d,%d...%n", numbers);

Winston
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My, I really confused everybody with my joke solution, didn’t I? I did imply that was the worst solution you were likely to get.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:My, I really confused everybody with my joke solution, didn’t I? I did imply that was the worst solution you were likely to get.

I know. Just thought I'd inject a bit of education into the proceedings...

Winston
 
Paul Clapham
Sheriff
Posts: 22471
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:My, I really confused everybody with my joke solution, didn’t I? I did imply that was the worst solution you were likely to get.


How about this one then?



From the passive-aggressive school of programming.
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow ! everyone but the beginners are here. Its turning into a meaningless drivel post.
 
Johan Allis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would this work? o.o
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johan Allis wrote:Would this work? o.o


The output is - 3,7,8,9,
I don't want a comma after the last element.
 
Johan Allis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then this?
 
Moe Kennedy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solution without an if:
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about the [...]? You haven’t printed any square brackets.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:How about this one then?

From the passive-aggressive school of programming.

Nice, and then of course there's:from the school of over-engineering.

Winston
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any more replies welcome. this thread is getting interesting.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Jack wrote:any more replies welcome. this thread is getting interesting.

Glad you're taking it in good humour. Some of us have nothing better to do with our time...

Winston
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moe Kennedy wrote:Solution without an if:


********************************************************************

Johan Allis wrote:Then this?


********************************************************************

I am not trying to pick on anyone. Both solutions will generate the output. But Johan's solution
does an if check each time the for loop runs. It makes the processor do unnecessary work, which
can be avoided by using Moe's solution. I guess that the speed of execution of both programs will
be different, but unnoticeable even if the array is large.

Believe it or not, this was one of the many questions that I was asked recently in an interview.
Obviously, the other questions were not this simple. I used the if-else approach too, but I realized
my mistake later. Keeping my fingers crossed.


 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moe Kennedy wrote:Solution without an if:


********************************************************************

Johan Allis wrote:Then this?


********************************************************************

I am not trying to pick on anyone. Both solutions will generate the output. But Johan's solution
does an if check each time the for loop runs. It makes the processor do unnecessary work, which
can be avoided by using Moe's solution. I guess that the speed of execution of both programs will
be different, but unnoticeable even if the array is large.

Believe it or not, this was one of the many questions that I was asked recently in an interview.
Obviously, the other questions were not this simple. I used the if-else approach too, but I realized
my mistake later. I wonder if the interviewer is crazy. What purpose do questions like these serve ?
Keeping my fingers crossed.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I would try it properly, so I went to my Java folder and tried to create a class: gedit ArrayPrinter.java. Only an old class appeared, dated “Oct 19 14:54”. It looks like this:-
java ArrayPrinter 1 2 69 4
{1, 2, 69, 4}
It prints args, which is a String[] rather than an int[], but the principles are the same. Work out what line 10 means. It prints {} if you pass an empty args array.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Jack wrote:Both solutions will generate the output. But Johan's solution
does an if check each time the for loop runs. It makes the processor do unnecessary work, which
can be avoided by using Moe's solution.

Actually, Moe's solution also contains an inefficiency: it has to determine the result of array.length - 1 on each iteration. One alternative is:but my slight preference is:Also, neither solution (nor the ones above) actually checks whether there's anything to print, so they will throw an Exception for a null or empty array.

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Also, neither solution (nor the ones above) actually checks whether there's anything to print, so they will throw an Exception for a null or empty array.

Johan's solution won't throw an exception for an empty array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!