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

D

Daniel Pitts

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.




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

Daniel Pitts

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.




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);}}
 
P

Piotr Kobzda

Daniel said:
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
 
L

Lew

Piotr said:
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!
 
D

Daniel Pitts

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

Lew

Daniel said:
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.
 
P

Piotr Kobzda

Daniel said:
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
 
P

Piotr Kobzda

Piotr said:
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
 
B

Ben Phillips

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

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top