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

W

WuyaSea Operator

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
 
L

Lew

WuyaSea said:
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?
 
J

Joshua Cranmer

WuyaSea said:
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?
 
W

WuyaSea Operator

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.



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
 
L

Lasse Reichstein Nielsen

WuyaSea Operator said:
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'
 
M

Manish Pandit

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
 
L

Lasse Reichstein Nielsen

WuyaSea Operator said:
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
 
L

Lew

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.
 
W

WuyaSea Operator

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
 
J

Joshua Cranmer

WuyaSea said:
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.

I doubt that Java programmers will be flocking to Ruby any time soon
unless their bosses suddenly decide that Ruby is the magic answer (like
UML and XML). For starters, Ruby (AFAICT) is functional programming and
shifting from OOP to FP can be quite a journey.
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.

Waxing poetical about Ruby at the expense of Java classifies as a
negative comment from my point of view. Then ending by promoting your
site for learning Ruby begins to seem... trollish.
Thanks for the advice though.

I apologize for my rather harsh treatment if you have learned some
general tips for posting to c.l.j.p.
 
D

Daniel Pitts

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

remove the line wraps, and I got it down to 238 bytes:
iimport static java.lang.System.*;class s{static{String s="27
0";for(int i=1;i<27;++i){char[]c=new char;java.util.Arrays.fill(c,"
ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(i));out.print(i+"
");out.println(c);s+=i%10;}out.println(s);exit(0);}}

Although, I don't typically think of this as a problem I need to solve
often. How is ruby at real problems, like writing a web app that can
handle 60+ million hits per day?
 
L

Lew

Daniel said:
Although, I don't typically think of this as a problem I need to solve
often. How is ruby at real problems, like writing a web app that can
handle 60+ million hits per day?

How about a system to take in >100 million documents in a matter of weeks,
parse them for adherence to certain surface edits, then pass them on to a
mainframe for further processing, load-balanced across geographically
disparate server farms, each server with 32 CPUs, programmed by several teams
with a few dozen developers overall, and have no downtime?

I'm working on one of those now, and it's in Java (JEE). Could Ruby deal with
that, I wonder?

Static type-checking alone probably saves my customer a few hundred million
dollars a year.

Jet liners are the cat's meow when you have too many passengers to fit on a
hang glider.
 
D

Daniel Pitts

How about a system to take in >100 million documents in a matter of weeks,
parse them for adherence to certain surface edits, then pass them on to a
mainframe for further processing, load-balanced across geographically
disparate server farms, each server with 32 CPUs, programmed by several teams
with a few dozen developers overall, and have no downtime?

I'm working on one of those now, and it's in Java (JEE). Could Ruby deal with
that, I wonder?

Static type-checking alone probably saves my customer a few hundred million
dollars a year.

Jet liners are the cat's meow when you have too many passengers to fit on a
hang glider.

Indeed. For prototyping, I'd probably choose Python over Ruby
anyway :)

Or, I would use the built-in Rhino engine. JavaScript is actually a
very interesting language if you get over its sordid past.
 
P

Piotr Kobzda

Daniel said:
remove the line wraps, and I got it down to 238 bytes:
iimport static java.lang.System.*;class s{static{String s="27
0";for(int i=1;i<27;++i){char[]c=new char;java.util.Arrays.fill(c,"
ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(i));out.print(i+"
");out.println(c);s+=i%10;}out.println(s);exit(0);}}


FWIW, here is 177 bytes long variant of it:

class C{static{String s="27 0";for(int
i=1;i<27;s+=i++%10)System.out.println(i+" "+new String(new
char).replace('\0',(char)('A'+i-1)));System.out.println(s);System.exit(0);}}

Can anyone beat it? :)

FYI: Ruby version without comments is 100 bytes long.


piotr
 
L

Lew

Piotr said:
FWIW, here is 177 bytes long variant of it:

class C{static{String s="27 0";for(int
i=1;i<27;s+=i++%10)System.out.println(i+" "+new String(new
char).replace('\0',(char)('A'+i-1)));System.out.println(s);System.exit(0);}}


Can anyone beat it? :)


Actually that's a 177- /char/ -long variant. Java source is stored in
characters, not bytes.
 
T

Thomas Fritsch

Piotr said:
FWIW, here is 177 bytes long variant of it:

class C{static{String s="27 0";for(int
i=1;i<27;s+=i++%10)System.out.println(i+" "+new String(new
char).replace('\0',(char)('A'+i-1)));System.out.println(s);System.exit(0);}}

Can anyone beat it? :)


Mine is 3 bytes shorter than yours!
Just replace ('A'+i-1) by (64+i)
;-)
 
P

Piotr Kobzda

Lew said:
Actually that's a 177- /char/ -long variant. Java source is stored in
characters, not bytes.

Well, my one is stored in bytes -- see a "Content-Type" field of my
message. :)

AFAIK each my Java source file is in bytes. Java compiler just converts
them later into characters applying character-set (AKA encoding) used to
produce these source files (usually default platform's charset).

But you right, to avoid confusions in our small contest, better is to
count characters. :)

So, current leader is Thomas -- 174 chars. ;-)


piotr
 
D

Daniel Pitts

Piotr said:
FWIW, here is 177 bytes long variant of it:
class C{static{String s="27 0";for(int
i=1;i<27;s+=i++%10)System.out.println(i+" "+new String(new
char).replace('\0',(char)('A'+i-1)));System.out.println(s);System.exit(0);}}

Can anyone beat it? :)

Mine is 3 bytes shorter than yours!
Just replace ('A'+i-1) by (64+i)
;-)


169 chars baby:
class C{static{String s="27 0";String g=" A";for(int i=1;i<27;s+=i++
%10,g+='A')System.out.println(i+g.replace('A',(char)
(64+i)));System.out.println(s);System.exit(0);}}
 
P

Piotr Kobzda

Daniel said:
Piotr said:
FWIW, here is 177 bytes long variant of it:
class C{static{String s="27 0";for(int
i=1;i<27;s+=i++%10)System.out.println(i+" "+new String(new
char).replace('\0',(char)('A'+i-1)));System.out.println(s);System.exit(0);}}
Can anyone beat it? :)

Mine is 3 bytes shorter than yours!
Just replace ('A'+i-1) by (64+i)
;-)


169 chars baby:
class C{static{String s="27 0";String g=" A";for(int i=1;i<27;s+=i++
%10,g+='A')System.out.println(i+g.replace('A',(char)
(64+i)));System.out.println(s);System.exit(0);}}


Well, 161 chars:

class C{static{String s="27 0",g=" A";for(int
i=1;i<27;s+=i++%10,g+='A')System.out.println(i+g.replace('A',(char)(64+i)));System.out.println(s);System.exit(0);}}

:)


piotr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top