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.

 
 
Joshua Cranmer
Guest
Posts: n/a
 
      09-20-2007
WuyaSea Operator wrote:
> 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.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
 
 
 
Daniel Pitts
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


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[i];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?



 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      09-20-2007
Daniel Pitts wrote:
> 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.

--
Lew
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      09-20-2007
On Sep 20, 5:56 am, Lew <(E-Mail Removed)> wrote:
> Daniel Pitts wrote:
> > 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.
>
> --
> Lew


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.

 
Reply With Quote
 
Piotr Kobzda
Guest
Posts: n/a
 
      09-20-2007
Daniel Pitts wrote:

> 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[i];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[i]).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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-20-2007
Piotr Kobzda wrote:
> 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[i]).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.

--
Lew
 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      09-20-2007
Piotr Kobzda wrote:
> 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[i]).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)


--
Thomas
 
Reply With Quote
 
Piotr Kobzda
Guest
Posts: n/a
 
      09-20-2007
Lew wrote:

> 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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      09-20-2007
On Sep 20, 10:23 am, Thomas Fritsch <(E-Mail Removed)>
wrote:
> Piotr Kobzda wrote:
> > 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[i]).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)
>
>
> --
> Thomas


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',(cha r)
(64+i)));System.out.println(s);System.exit(0);}}

 
Reply With Quote
 
Piotr Kobzda
Guest
Posts: n/a
 
      09-20-2007
Daniel Pitts wrote:
> On Sep 20, 10:23 am, Thomas Fritsch <(E-Mail Removed)>
> wrote:
>> Piotr Kobzda wrote:
>>> 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[i]).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)
>>
>>
>> --
>> Thomas

>
> 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',(cha r)
> (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.r eplace('A',(char)(64+i)));System.out.println(s);Sy stem.exit(0);}}




piotr
 
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