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.

 
 
Daniel Pitts
Guest
Posts: n/a
 
      09-22-2007
On Sep 21, 1:31 pm, Piotr Kobzda <(E-Mail Removed)> wrote:
> Daniel Pitts wrote:
> >> enum C{C;{String s=" ";for(char
> >> c=64;c++<91System.out.println(c-64+(s+=(c-5)%10).replaceAll(c<91?"\\d":"!",""+c));System.exi t(0);}}

>
> Here is a slightly rewritten variant of it:
>
> enum C{C;{String s=" 0";for(char
> c=64;c++<91;s+=(c-4)%10)System.out.println(c-64+(c<91?s.replaceAll("\\d",""+c):s));System.exit( 0);}}
>
> Count is the same, 133, but the latter appears a bit nicer to me.
>
>
>
> > Nice...

>
> Thanks, and the same to you Daniel! Without your great Java hacks I
> would likely have never had come to my result.
>
> But the contest is still open!
>
> Hope we'll see shorter code soon.
>
> piotr


Perhaps a brute-force approach should be used. Write different java
files and compile them until one compiles, and when executed outputs
the expected output.


 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      09-22-2007
On Sep 21, 1:31 pm, Piotr Kobzda <(E-Mail Removed)> wrote:
> Daniel Pitts wrote:
> >> enum C{C;{String s=" ";for(char
> >> c=64;c++<91System.out.println(c-64+(s+=(c-5)%10).replaceAll(c<91?"\\d":"!",""+c));System.exi t(0);}}

>
> Here is a slightly rewritten variant of it:
>
> enum C{C;{String s=" 0";for(char
> c=64;c++<91;s+=(c-4)%10)System.out.println(c-64+(c<91?s.replaceAll("\\d",""+c):s));System.exit( 0);}}
>
> Count is the same, 133, but the latter appears a bit nicer to me.
>
>
>
> > Nice...

>
> Thanks, and the same to you Daniel! Without your great Java hacks I
> would likely have never had come to my result.
>
> But the contest is still open!
>
> Hope we'll see shorter code soon.
>
> piotr


125 chars:

enum C{C;{String g="%d ",s=g+0;for(int i=0;i++<27;s+=i
%10)System.out.format(i<27?(g+="%2$c")+"\n":s,i,i
+64);System.exit(0);}}

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

> 125 chars:
>
> enum C{C;{String g="%d ",s=g+0;for(int i=0;i++<27;s+=i
> %10)System.out.format(i<27?(g+="%2$c")+"\n":s,i,i
> +64);System.exit(0);}}


Super... I was thinking about %c before, but you did it!

Just one char less:

enum C{C;int i;{String g="%d ",s=g+0;for(;i++<27;s+=i%10)
System.out.format(i<27?(g+="%2$c")+"\n":s,i,i+64); System.exit(0);}}




piotr
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-22-2007
Piotr Kobzda wrote:
> Daniel Pitts wrote:
>
>> 125 chars:
>>
>> enum C{C;{String g="%d ",s=g+0;for(int i=0;i++<27;s+=i
>> %10)System.out.format(i<27?(g+="%2$c")+"\n":s,i,i
>> +64);System.exit(0);}}

>
> Super... I was thinking about %c before, but you did it!
>
> Just one char less:
>
> enum C{C;int i;{String g="%d ",s=g+0;for(;i++<27;s+=i%10)
> System.out.format(i<27?(g+="%2$c")+"\n":s,i,i+64); System.exit(0);}}
>
>


See? Java does rival Ruby for compactness and obscurity of expression!

--
Lew
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      09-22-2007
On Sep 22, 3:09 am, Piotr Kobzda <(E-Mail Removed)> wrote:
> Daniel Pitts wrote:
> > 125 chars:

>
> > enum C{C;{String g="%d ",s=g+0;for(int i=0;i++<27;s+=i
> > %10)System.out.format(i<27?(g+="%2$c")+"\n":s,i,i
> > +64);System.exit(0);}}

>
> Super... I was thinking about %c before, but you did it!
>
> Just one char less:
>
> enum C{C;int i;{String g="%d ",s=g+0;for(;i++<27;s+=i%10)
> System.out.format(i<27?(g+="%2$c")+"\n":s,i,i+64); System.exit(0);}}
>
>
>
> piotr


Nice. I think I'm satisfied of the compactness of this program.

Unless you consider passing things in from the command line (could be
cheating, but whatever)
73 characters:

enum C{C;{System.out.format(System.getProperty("o"),
10);System.exit(0);}}

Run it as
java "-Do=1 A%1$c2 BB%1$c3 CCC%1$c4 DDDD%1$c5 EEEEE%1$c6 FFFFFF%1$c7
GGGGGGG%1$c8 HHHHHHHH%1$c9 IIIIIIIII%1$c10 JJJJJJJJJJ%1$c11 KKKKKKKKKKK
%1$c12 LLLLLLLLLLLL%1$c13 MMMMMMMMMMMMM%1$c14 NNNNNNNNNNNNNN%1$c15
OOOOOOOOOOOOOOO%1$c16 PPPPPPPPPPPPPPPP%1$c17 QQQQQQQQQQQQQQQQQ%1$c18
RRRRRRRRRRRRRRRRRR%1$c19 SSSSSSSSSSSSSSSSSSS%1$c20 TTTTTTTTTTTTTTTTTTTT
%1$c21 UUUUUUUUUUUUUUUUUUUUU%1$c22 VVVVVVVVVVVVVVVVVVVVVV%1$c23
WWWWWWWWWWWWWWWWWWWWWWW%1$c24 XXXXXXXXXXXXXXXXXXXXXXXX%1$c25
YYYYYYYYYYYYYYYYYYYYYYYYY%1$c26 ZZZZZZZZZZZZZZZZZZZZZZZZZZ%1$c27
012345678901234567890123456%1$c" C

You could also use the environment variables instead, but setting that
depends on the system. It shaves 4 off though (getProperty -
getenv). Also, depending on your system, you could replace all the
%1$c with an actual linefeed, and then you can change "format" to
"print", and remove the ",10" to save an additional 4.

Although, I really don't consider this type of solution as interesting.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-22-2007
Daniel Pitts wrote:
> Unless you consider passing things in from the command line (could be
> cheating, but whatever)
> 73 characters:

....
> Although, I really don't consider this type of solution as interesting.


Perhaps not to you, but it does represent a basic principle of good OO
programming - externalize your magic values. For one thing, with the text in
a resource or command-line argument, you have now a 73-character i18n version
of the algorithm; the original Ruby version was tied to American/Western
locale only.

Apropos of which, given the multinational character of the United States, and
the prevalence of at least Espaņol as a widespread language there, it may be
that ASCII no longer suffices for American-locale strings anyway.

--
Lew


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

> Nice. I think I'm satisfied of the compactness of this program.


Yes, it's compact. But we have a new one...

119 chars:

enum C{C;int i,j;{for(;j<27;j++)System.out.printf(j>0?"%c":
++i+" %c",j<i?i<27?i+64:j%10+48:11+(j=-1));System.exit(0);}}


> Unless you consider passing things in from the command line (could be
> cheating, but whatever)
> 73 characters:
>
> enum C{C;{System.out.format(System.getProperty("o"),
> 10);System.exit(0);}}
>
> Run it as
> java "-Do=1 A%1$c2 BB%1$c3 CCC%1$c4 DDDD%1$c5 EEEEE%1$c6 FFFFFF%1$c7
> GGGGGGG%1$c8 HHHHHHHH%1$c9 IIIIIIIII%1$c10 JJJJJJJJJJ%1$c11 KKKKKKKKKKK
> %1$c12 LLLLLLLLLLLL%1$c13 MMMMMMMMMMMMM%1$c14 NNNNNNNNNNNNNN%1$c15
> OOOOOOOOOOOOOOO%1$c16 PPPPPPPPPPPPPPPP%1$c17 QQQQQQQQQQQQQQQQQ%1$c18
> RRRRRRRRRRRRRRRRRR%1$c19 SSSSSSSSSSSSSSSSSSS%1$c20 TTTTTTTTTTTTTTTTTTTT
> %1$c21 UUUUUUUUUUUUUUUUUUUUU%1$c22 VVVVVVVVVVVVVVVVVVVVVV%1$c23
> WWWWWWWWWWWWWWWWWWWWWWW%1$c24 XXXXXXXXXXXXXXXXXXXXXXXX%1$c25
> YYYYYYYYYYYYYYYYYYYYYYYYY%1$c26 ZZZZZZZZZZZZZZZZZZZZZZZZZZ%1$c27
> 012345678901234567890123456%1$c" C


So what would you say about *0*-length solution?

Run it as:

jrunscript -e "for(i=0;i++<27{print(i+'
');for(j=0;j<i;++j)print(i<27?String.fromCharCode( i+64):j%10);println('');}"

(Requires JDK 1.6+)


> You could also use the environment variables instead, but setting that
> depends on the system. It shaves 4 off though (getProperty -
> getenv). Also, depending on your system, you could replace all the
> %1$c with an actual linefeed [...]


We don't need it since we have %n.

> Although, I really don't consider this type of solution as interesting.


Neither do I.


piotr
 
Reply With Quote
 
Piotr Kobzda
Guest
Posts: n/a
 
      09-24-2007
Piotr Kobzda wrote:

> 119 chars:
>
> enum C{C;int i,j;{for(;j<27;j++)System.out.printf(j>0?"%c":
> ++i+" %c",j<i?i<27?i+64:j%10+48:11+(j=-1));System.exit(0);}}


118:

enum C{C;int i,j;{for(;j<27;j++)System.out.printf(j>0?"%c":
++i+" %c",j<i?i<27?i+64:j%10+48:9-(j=-1));System.exit(0);}}




piotr
 
Reply With Quote
 
Ben Phillips
Guest
Posts: n/a
 
      09-24-2007
Piotr Kobzda wrote:
> Piotr Kobzda wrote:
>
>> 119 chars:
>>
>> enum C{C;int i,j;{for(;j<27;j++)System.out.printf(j>0?"%c":
>> ++i+" %c",j<i?i<27?i+64:j%10+48:11+(j=-1));System.exit(0);}}

>
>
> 118:
>
> enum C{C;int i,j;{for(;j<27;j++)System.out.printf(j>0?"%c":
> ++i+" %c",j<i?i<27?i+64:j%10+48:9-(j=-1));System.exit(0);}}


This gives one an interesting impression of what it may look like under
the hood when the compiler is doing optimizations ... and why the
resulting assembly/bytecode/whatever is frequently gibberish spaghetti
code nobody on earth could possibly hope to maintain. Not exactly, mind
you, since it optimizes for speed rather than space! But still ...
 
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