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

L

Lew

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

That tells how your message is sent, not how your source is stored.
AFAIK each my Java source file is in bytes. Java compiler just converts

According to the Java Language Specification, Java source files are in characters:
<http://java.sun.com/docs/books/jls/third_edition/html/lexical.html>

Piotr said:
them later into characters applying character-set (AKA encoding) used to
produce these source files (usually default platform's charset).

Other way around. JSL Chapter 3:
Piotr Kobzda:
But you right, to avoid confusions in our small contest, better is to
count characters. :)

The JLS requires it.
 
D

Daniel Pitts

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


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

Or, if you don't need an exit, 147: Warning, you'll have to ctrl-break
out of this one
class C{static{String s="27 0",g=" A";for(int i=1;i<27;s+=i++%10,g
+='A')System.out.println(i==28?s:(i+g.replace('A',(char)
(64+i))));while(s!=g);}}
 
L

Lew

Piotr said:
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);}}

How does this class get loaded into the JVM?
 
D

Daniel Pitts

Daniel said:
On Sep 20, 10:23 am, Thomas Fritsch <[email protected]>
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).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',(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

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

Or, if you don't need an exit, 147: Warning, you'll have to ctrl-break
out of this one
class C{static{String s="27 0",g=" A";for(int i=1;i<27;s+=i++%10,g
+='A')System.out.println(i==28?s:(i+g.replace('A',(char)
(64+i))));while(s!=g);}}


Oops, bug in my last post...
i<27 should be i<28, and i==28 should be i==27.

Oh, and skim one more char off, change the i==27 to i>26
 
D

Daniel Pitts

How does this class get loaded into the JVM?

java C

This will first load the class C, which executes the static
initializer. The initializer exits the JVM before it has a chance to
look for the main method, so no exception occurs.
 
L

Laurent D.A.M. MENTEN

Lew a écrit :
How does this class get loaded into the JVM?

Java C

and it works! so we can deduce that the JVM executes the static
initialiser before it looks up the main method...
 
L

Lew

Daniel said:
java C

This will first load the class C, which executes the static
initializer. The initializer exits the JVM before it has a chance to
look for the main method, so no exception occurs.

What a great hack!
 
D

Daniel Pitts

Daniel Pitts wrote:
On Sep 20, 10:23 am, Thomas Fritsch <[email protected]>
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).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',(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

150:
class C{static{String s="27 0",g=" A";for(int i=1;i<27;s+=i++%10,g
+='A')System.out.println(i==28?s:(i+g.replace('A',(char)
(64+i))));System.exit(0);}}
Or, if you don't need an exit, 147: Warning, you'll have to ctrl-break
out of this one
class C{static{String s="27 0",g=" A";for(int i=1;i<27;s+=i++%10,g
+='A')System.out.println(i==28?s:(i+g.replace('A',(char)
(64+i))));while(s!=g);}}

Oops, bug in my last post...
i<27 should be i<28, and i==28 should be i==27.

Oh, and skim one more char off, change the i==27 to i>26


Okay, skimmed even more off (no, I'm not bored!)

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

Character total: 144 (or trim 3 off with the "while(s!=g);" hack.
 
P

Piotr Kobzda

Lew said:
That tells how your message is sent, not how your source is stored.

Well, not necessarily stored as such. But believe me, on my disk a copy
of my message is stored exactly as was transmitted, that is in bytes
representing a source characters (encoded using charset ISO-8859-2) you
(and others) have received later.

Similar is the original source file (C.java) of a published piece of
code, which size is exactly 177 bytes. The only minor difference
between the post message and the file is that the source code was
converted into bytes using Cp1250 charset, which in this particular
source code case gives exactly the same sequence of bytes, what using
ISO-8859-2 charset gives.

According to the Java Language Specification, Java source files are in
characters:

You see? Are *written*, not necessarily *stored* as such.
Other way around. JSL Chapter 3:

The /Unicode escapes/ are completely unrelated to what we are talking
about. They are being processed after conversion of a source file bytes
into characters (ASCII, or Unicode). In other words, they are already
characters -- called a /raw Unicode character stream/ -- which are
translated into other Unicode characters. Translation into sequence of
input tokens begins just after that translation.

The JLS requires it.

Nope. AIUI, I can _store_ the source code in whatever form I like, and
the JLS can not prevent me from doing that. The only requirement is to
instruct my compiler (normally using -encoding option) on how Unicode
(or ASCII) characters are encoded (as bytes) in my Java source files.


piotr
 
S

Stefan Ram

WuyaSea Operator said:
write a program to produce the following output:

public class Main
{ public static void main ( final java.lang.String[] args )
{ java.lang.System.out.println
( "1 A\n2 BB\n3 CCC\n......\n25 YYYYYYYYYYYYYYYYYYYYYYYYY\n26 ZZZZZZZZZZ" +
"ZZZZZZZZZZZZZZZZ\n27 012345678901234567890123456\n\nIn java, this is " +
"probably what most of programmers do:\n public class Text {\n public" +
" static void main(String args[]) {\n int x = 0;\n for (char c =" +
" 'A'; c <= 'Z'; c++) {\n x++;\n System.out.print(x + \" \")" +
";\n for (int i = 0; i < x; i++) {\n System.out.print( c )" +
";\n }\n System.out.println();\n }\n System.out.print(" +
"x + 1 + \" \");\n for (int i = 0; i <= x; i++) {\n System.out" +
".print(i % 10);\n }\n }\n}\n\n\nSame problem, can be tackled in r" +
"uby by just 3 lines.\nn = ?A - 1\n(1..26).each{|i| puts \"#{i} #{(i+n" +
").chr*i} \"} # prints A-Z\nputs \"27 \" + (0..27).collect{|i| i%1" +
"0}.to_s # prints numbers line\n\n# some ruby language functions\n#" +
"\n# ?<X> return the ASCII code of the character\n# ?A => 65\n# ?B => " +
"66\n# ?\\n => 10 backspace ASCII code is 10\n#\n#\n# <N>.chr returns " +
"the character from given ASCII code\n# 65.chr => \"A\"\n# 66.chr => \"" +
"B\"\n# 10.chr => \"\\n\"\n#\n#\n# <STR>*<n> multiplies given string n" +
" times.\n# \"ABC\"*2 => ABCABC\n#\n#\n# ruby use \"#{var}\" syntax to" +
" print variable in a string\n# x = \"abc\"\n# puts \"output: #{x}\" =" +
"> \"output: abc\"\n\n\nIf you're ready to learn ruby, and Ruby on Rai" +
"ls.\nvisit http://groups.wuyasea.com/group/ruby-on-rails\n\nI'll answ" +
"er all your ruby and rails questions.\n\nDorren\nhttp://groups.wuyase" +
"a.com/profile/dorren\n" ); }}
 
J

Joshua Cranmer

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

Character total: 144 (or trim 3 off with the "while(s!=g);" hack.

Change your
(char)(64+i)
to first
'A'+(i-1)
to shave off three characters and then change that to
64+i
to shave off another five, for a total of 136 or 133 bytes.
 
D

Daniel Pitts

Change your
(char)(64+i)
to first
'A'+(i-1)
to shave off three characters and then change that to
64+i
to shave off another five, for a total of 136 or 133 bytes.

That doesn't work.
You HAVE to cast to char.
 
L

Lew

Piotr said:
Well, not necessarily stored as such.

Well, OK, your points are well taken, but it is off what I was originally
trying to say.

If one of these APL contests used a Unicode character like \u000a I would
count it as one, not six. What would you do?

To be fair, I think since Java says its source is in Unicode, that we should
judge Java source-length contests by Java's rules, and count characters, not
bytes.

Source storage format is completely irrelevant, as is the byte length of a
character.

This also makes source files from international sources commensurate. A
character length of a string is well defined, the byte length - eh, not so much.

That is my original point. Not really how it's stored but how it's counted.
 
D

Daniel Pitts

That doesn't work.
You HAVE to cast to char.

Although, I did find a way to trim two more chars off:

enum C{a;C(){String g=" ",s="27 0";for(int i=1;i<28;s+=i++
%10)System.out.println(i>26?s:i+(g+=0).replace('0',(char)
(64+i)));System.exit(0);}}
 
D

Daniel Pitts

What a great hack!

Here's a better one: I got the code down to 139 characters...
enum C{a;{String g=" ",s="27 0";for(int i=1;i<28;s+=i++%10,g
+=0)System.out.println(i>26?s:i+g.replace('0',(char)
(64+i)));System.exit(0);}}


The trick to this one:

enums create one instance of their children during their static
initialization. This of course, calls the non-static initializer of
the super class, and then the child class.

So, as far as I know, the following is the smallest (source) non-
erroring program
enum C{a;{System.exit(0);}}
taking up only 28 chars, it is smaller than the other version by 5
chars:
class C{static{System.exit(0);}}
which takes 33 chars, and is itself *13* chars shorter than the
"standard" approach:
class C{public static void main(String[]a){}}
at a whopping 46 chars.

:)
 
P

Piotr Kobzda

Daniel said:
Although, I did find a way to trim two more chars off:

enum C{a;C(){String g=" ",s="27 0";for(int i=1;i<28;s+=i++
%10)System.out.println(i>26?s:i+(g+=0).replace('0',(char)
(64+i)));System.exit(0);}}

You may cut another three in it, which results in 139 chars needed.

However, the following needs 133 chars to do the same:

enum C{C;{String s=" ";for(char
c=64;c++<91;)System.out.println(c-64+(s+=(c-5)%10).replaceAll(c<91?"\\d":"!",""+c));System.exit(0);}}


piotr
 
P

Piotr Kobzda

Lew said:
To be fair, I think since Java says its source is in Unicode, that we
should judge Java source-length contests by Java's rules, and count
characters, not bytes.

I think that's the right way.
This also makes source files from international sources commensurate. A
character length of a string is well defined, the byte length - eh, not
so much.

Agreed, not so much... :) But four our purposes the length() of a
String is enough IMHO -- I'm nearly sure that nobody will use Unicode
escapes, nor surrogate pairs in theirs solutions, simply because it
gives longer code.


piotr
 
D

Daniel Pitts

You may cut another three in it, which results in 139 chars needed.

However, the following needs 133 chars to do the same:

enum C{C;{String s=" ";for(char
c=64;c++<91;)System.out.println(c-64+(s+=(c-5)%10).replaceAll(c<91?"\\d":"!",""+c));System.exit(0);}}

piotr

Actually, thats 134 by my count.

Oh, actually, all my counts are one off :) Silly me. You're right,
133.

Nice...
 
C

Chris Smith

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

For what it's worth, Ruby is not a functional programming language. It
simply uses a few concepts that originated in functional languages...
just like Java, C#, and most other popular languages out there.

In any case, the problem here is not with Ruby; the problem is a spammer
posting in an unrelated newsgroup to promote his web site.
 
P

Piotr Kobzda

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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top