Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > one interview question, 17 lines in java, 3 lines in ruby.

Reply
Thread Tools

one interview question, 17 lines in java, 3 lines in ruby.

 
 
WuyaSea Operator
Guest
Posts: n/a
 
      09-19-2007
write a program to produce the following output:
1 A
2 BB
3 CCC
.......
25 YYYYYYYYYYYYYYYYYYYYYYYYY
26 ZZZZZZZZZZZZZZZZZZZZZZZZZZ
27 012345678901234567890123456

In java, this is probably what most of programmers do:
public class Text {
public static void main(String args[]) {
int x = 0;
for (char c = 'A'; c <= 'Z'; c++) {
x++;
System.out.print(x + " ");
for (int i = 0; i < x; i++) {
System.out.print( c );
}
System.out.println();
}
System.out.print(x + 1 + " ");
for (int i = 0; i <= x; i++) {
System.out.print(i % 10);
}
}
}


Same problem, can be tackled in ruby by just 3 lines.
n = ?A - 1
(1..26).each{|i| puts "#{i} #{(i+n).chr*i} "} # prints A-Z
puts "27 " + (0..27).collect{|i| i%10}.to_s # prints numbers line

# some ruby language functions
#
# ?<X> return the ASCII code of the character
# ?A => 65
# ?B => 66
# ?\n => 10 backspace ASCII code is 10
#
#
# <N>.chr returns the character from given ASCII code
# 65.chr => "A"
# 66.chr => "B"
# 10.chr => "\n"
#
#
# <STR>*<n> multiplies given string n times.
# "ABC"*2 => ABCABC
#
#
# ruby use "#{var}" syntax to print variable in a string
# x = "abc"
# puts "output: #{x}" => "output: abc"


If you're ready to learn ruby, and Ruby on Rails.
visit http://groups.wuyasea.com/group/ruby-on-rails

I'll answer all your ruby and rails questions.

Dorren
http://groups.wuyasea.com/profile/dorren

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      09-19-2007
WuyaSea Operator wrote:
> Same problem, can be tackled in ruby [sic] by just 3 lines.


God, that is /so/ much better! After all, line counts are /so/ important!
Much more so than readability!

> n = ?A - 1
> (1..26).each{|i| puts "#{i} #{(i+n).chr*i} "} # prints A-Z
> puts "27 " + (0..27).collect{|i| i%10}.to_s # prints numbers line


APL programmers used to have "one-liner" contests - how much could you do in a
one-line program?

Their programs were even more unreadable than your Ruby example.

Which, by the way, is locale-sensitive and will break in some locales.

Is it a benefit or a detriment to express the algorithm so succinctly?

--
Lew
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      09-19-2007
WuyaSea Operator wrote:
> write a program to produce the following output:
> 1 A
> 2 BB
> 3 CCC
> ......
> 25 YYYYYYYYYYYYYYYYYYYYYYYYY
> 26 ZZZZZZZZZZZZZZZZZZZZZZZZZZ
> 27 012345678901234567890123456
>
> In java, this is probably what most of programmers do:
> public class Text {
> public static void main(String args[]) {
> int x = 0;
> for (char c = 'A'; c <= 'Z'; c++) {
> x++;
> System.out.print(x + " ");
> for (int i = 0; i < x; i++) {
> System.out.print( c );
> }
> System.out.println();
> }
> System.out.print(x + 1 + " ");
> for (int i = 0; i <= x; i++) {
> System.out.print(i % 10);
> }
> }
> }


Want succinctness? I can shrink that further:
public class Test {
public static void main(String... args) {
for (int i=0;i<26;i++) {
System.out.print("\n"+(i+1)+" ");
for (int j=0;j<=i;j++)
System.out.print('A'+j);
}
System.out.println("\n27 012345678901234567890123456");
}
}

That's 10 lines (if you don't what that extra newline, then it'd need to
be 11 lines). Actually, there are only five lines of functional logic.

> Same problem, can be tackled in ruby by just 3 lines.
> n = ?A - 1
> (1..26).each{|i| puts "#{i} #{(i+n).chr*i} "} # prints A-Z
> puts "27 " + (0..27).collect{|i| i%10}.to_s # prints numbers line


"Can be" != "should be". Even my ugly compact Java file is more readable
than your three lines of Ruby.

Besides, so your Ruby program can print out a contrived example more
succinctly than Java. What's the functional difference in the program if
the code to be written was to write a fully-functional CSS parser?

> [ snip useless Ruby info ]
>
> If you're ready to learn ruby, and Ruby on Rails.
> visit http://groups.wuyasea.com/group/ruby-on-rails
>
> I'll answer all your ruby and rails questions.
>
> Dorren
> http://groups.wuyasea.com/profile/dorren
>


Don't you want the Ruby newsgroup, you spamming troll?

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      09-19-2007
WuyaSea Operator wrote:

[counterproductive Ruby propaganda omitted]

Please don't multipost
http://www.cs.tut.fi/~jkorpela/usenet/xpost.html
 
Reply With Quote
 
WuyaSea Operator
Guest
Posts: n/a
 
      09-20-2007

> System.out.println("\n27 012345678901234567890123456");
> That's 10 lines (if you don't what that extra newline, then it'd need to

if you're going to cut down the line count by cheating there, nothing
i can say.



> > Same problem, can be tackled in ruby by just 3 lines.
> > n = ?A - 1
> > (1..26).each{|i| puts "#{i} #{(i+n).chr*i} "} # prints A-Z
> > puts "27 " + (0..27).collect{|i| i%10}.to_s # prints numbers line


if you have trouble reading the 2nd line, here is rewrite:
n = ?A - 1
(1..26).each do |i| puts i.to_s + " " + (i+n).chr*i end
puts "27 " + (0..27).collect{|i| i%10 }.to_s # prints numbers
line

array.each{|item| operation...} notation is called closure, powerful
and very commonly used in ruby.



> > [ snip useless Ruby info ]

with that attitude, there's nothing i can help you.


> Don't you want the Ruby newsgroup, you spamming troll?

Do not call me troll, you cheater.



once again, when you're ready.
visit http://groups.wuyasea.com/group/ruby-on-rails

Dorren
http://groups.wuyasea.com/profile/dorren


 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-20-2007
WuyaSea Operator <(E-Mail Removed)> writes:

> Same problem, can be tackled in ruby by just 3 lines.
> n = ?A - 1
> (1..26).each{|i| puts "#{i} #{(i+n).chr*i} "} # prints A-Z
> puts "27 " + (0..27).collect{|i| i%10}.to_s # prints numbers line


Does this really give a numbers line ending in "6"?

Anyway, in Perl it is also easily doable in three lines:

my $n = ord("A") - 1;
for $i (1..26) { print "$i ", chr($i+$n) x $i, "\n";}
print "27 ", (map {$_ % 10} (0..26)), "\n";

This is time for considering whether doing something in three
lines is really a mark of quality.

/L 'Everything can be done in three lines of Perl'
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Manish Pandit
Guest
Posts: n/a
 
      09-20-2007
On Sep 19, 12:21 pm, WuyaSea Operator <(E-Mail Removed)> wrote:
> write a program to produce the following output:
> 1 A
> 2 BB
> 3 CCC
> ......
> 25 YYYYYYYYYYYYYYYYYYYYYYYYY
> 26 ZZZZZZZZZZZZZZZZZZZZZZZZZZ
> 27 012345678901234567890123456
>
> In java, this is probably what most of programmers do:
> public class Text {
> public static void main(String args[]) {
> int x = 0;
> for (char c = 'A'; c <= 'Z'; c++) {
> x++;
> System.out.print(x + " ");
> for (int i = 0; i < x; i++) {
> System.out.print( c );
> }
> System.out.println();
> }
> System.out.print(x + 1 + " ");
> for (int i = 0; i <= x; i++) {
> System.out.print(i % 10);
> }
> }
>
> }
>
> Same problem, can be tackled in ruby by just 3 lines.
> n = ?A - 1
> (1..26).each{|i| puts "#{i} #{(i+n).chr*i} "} # prints A-Z
> puts "27 " + (0..27).collect{|i| i%10}.to_s # prints numbers line
>
> # some ruby language functions
> #
> # ?<X> return the ASCII code of the character
> # ?A => 65
> # ?B => 66
> # ?\n => 10 backspace ASCII code is 10
> #
> #
> # <N>.chr returns the character from given ASCII code
> # 65.chr => "A"
> # 66.chr => "B"
> # 10.chr => "\n"
> #
> #
> # <STR>*<n> multiplies given string n times.
> # "ABC"*2 => ABCABC
> #
> #
> # ruby use "#{var}" syntax to print variable in a string
> # x = "abc"
> # puts "output: #{x}" => "output: abc"
>
> If you're ready to learn ruby, and Ruby on Rails.
> visithttp://groups.wuyasea.com/group/ruby-on-rails
>
> I'll answer all your ruby and rails questions.
>
> Dorrenhttp://groups.wuyasea.com/profile/dorren


Having used Ruby a couple of times myself, I found that Ruby is pretty
good, concise and robust for scripting and for prototyping something
solid + workable real fast.

However, comparing programming languages based on how many lines of
code does it take to do XXX may not be the best way to look at things.
Everything would be a shell script if that were the case. There are
things like developer productivity, cost and ease of maintenance,
availability of skilled developers and resources within budget, etc.
that go behind acceptance of any programming language in an
organization too. Given the current market trends (and the me-too Web
2.0 wave), Ruby and frameworks around it are picking up and it is
poised to become one of the mainstream languages in the near future.

-cheers,
Manish

 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-20-2007
WuyaSea Operator <(E-Mail Removed)> writes:

> array.each{|item| operation...} notation is called closure, powerful
> and very commonly used in ruby.


.... and many other languages. Admittedly, Java is not one of them.

> with that attitude, there's nothing i can help you.


I seem to remember you comming into a Java discussion group with an
attitude of "see, my language is better than yours". That's called
"being off-topic". Nobody here asked for your help.

> Do not call me troll, you cheater.


Either a troll or a spammer. That depends on whether you want people
to get insulted, or you are merely disingenious enough not to expect
it.

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-20-2007
WuyaSea Operator <(E-Mail Removed)> writes:
>> Do not call me troll, you cheater.


Lasse Reichstein Nielsen wrote:
> Either a troll or a spammer. That depends on whether you want people
> to get insulted, or you are merely disingenious enough not to expect
> it.


One of the marks of a troll is that they respond to perceived /ad hominem/
attacks, but ignore the measured responses to the technical points.

Does that apply here?

Let's see, Joshua said,
> "Can be" != "should be".
> Even my ugly compact Java file is more readable than your three lines of Ruby.


Not answered.

> Besides, so your Ruby program can print out a contrived example more succinctly than Java.
> What's the functional difference in the program if the code to be written was
> to write a fully-functional CSS parser?


Not answered.

> Don't you want the Ruby newsgroup,


Not answered.

> you spamming troll?


Answered.

A full participant wouldn't have reacted negatively, but understood that
posting their website URL and offering to answer people's questions makes
their post seem an awful lot like spam, especially given the supercilious tone
of the posts, and worked to overcome that negative impression instead of
reacting with defensive hostility.

Speaking of which, "cheater"? That's a witty thing to call Joshua. How does
"cheater" apply in any plausible way to Joshua? At what could he have been
cheating? How is that even a response worthy of pressing "Send"?

I actually am very strongly interested in Ruby, and shall be learning it
post-haste. It's totally the next wave - but comparing Java to Ruby is like
comparing jet liners to hang gliders => "You see? My hang glider doesn't
require all those radios and fuel lines and control panels!"

For sure I will not use the OP or their web site for any guidance now that
they have completely burned their credibility with me. Oh, I suppose if they
respond to this post in a reasonable manner I'll reconsider that decision.

Perhaps.

--
Lew
 
Reply With Quote
 
WuyaSea Operator
Guest
Posts: n/a
 
      09-20-2007
>
> I seem to remember you comming into a Java discussion group with an
> attitude of "see, my language is better than yours". That's called
> "being off-topic". Nobody here asked for your help.
>
>
> Either a troll or a spammer. That depends on whether you want people
> to get insulted, or you are merely disingenious enough not to expect
> it.


The reason i came to the group is to generate interest of Ruby to java
programmers who like to find out more. By no means I intend to insult
or step on anyone. I, myself was a java/j2ee programmer for many
years, only lately embarked on ruby, and find it's much more
productive than java in webapp development.

Programming language comes and goes, I don't identify with PL so
personally. In my very first post, I didn't express any negative
comments about java, if you think that way, please don't.

Thanks for the advice though.



If you're ready to learn ruby, and Ruby on Rails.
visit http://groups.wuyasea.com/group/ruby-on-rails

Dorren
http://groups.wuyasea.com/profile/dorren

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shortest way to read all lines (one by one) from a text file? Robin Wenger Java 11 02-14-2011 12:08 PM
ASP Interview Questions ASP Interview Questions reema ASP General 0 08-26-2008 11:57 AM
.NET Interview Question, C#, ASP.NET Interview Questions dotnetuncle Javascript 0 10-30-2007 03:08 PM
Re: one interview question, 17 lines in java, 3 lines in ruby. Lew Java 0 09-20-2007 12:34 AM
Asp.Net Calender, how to display 5 lines if there are only 5 lines in one month? Jack ASP .Net 9 10-12-2005 03:44 AM



Advertisments