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.

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


Piotr Kobzda wrote:
> 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:
>> Programs are written in Unicode ...


and
>> Programs are written using the Unicode character set.
>> Information about this character set and its associated
>> character encodings may be found at:
>>
>> http://www.unicode.org
>>
>> The Java platform tracks the Unicode specification as it evolves.
>> The precise version of Unicode used by a given release is specified
>> in the documentation of the class Character.

<http://java.sun.com/docs/books/jls/third_edition/html/lexical.html>

Piotr Kobzda wrote:
> 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:
>> lexical translations are provided (§3.2) so that Unicode escapes (§3.3)
>> can be used to include any Unicode character using only ASCII characters.


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


The JLS requires it.

--
Lew
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      09-20-2007
On Sep 20, 11:28 am, Piotr Kobzda <pi...@gazeta.pl> wrote:
> Daniel Pitts wrote:
> > On Sep 20, 10:23 am, Thomas Fritsch <i.dont.like.s...@invalid.com>
> > 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


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?si+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?si+g.replace('A', (char)
(64+i))));while(s!=g);}}


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


How does this class get loaded into the JVM?

--
Lew
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      09-20-2007
On Sep 20, 11:38 am, Daniel Pitts <googlegrou...@coloraura.com> wrote:
> On Sep 20, 11:28 am, Piotr Kobzda <pi...@gazeta.pl> wrote:
>
>
>
> > Daniel Pitts wrote:
> > > On Sep 20, 10:23 am, Thomas Fritsch <i.dont.like.s...@invalid.com>
> > > 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

>
> 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?si+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?si+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

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      09-20-2007
On Sep 20, 11:39 am, Lew <l...@lewscanon.com> wrote:
> Piotr Kobzda wrote:
> > 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);}}

>
> How does this class get loaded into the JVM?
>
> --
> Lew


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.

 
Reply With Quote
 
Laurent D.A.M. MENTEN
Guest
Posts: n/a
 
      09-20-2007
Lew a écrit :
> Piotr Kobzda wrote:
>> 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);}}

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


Lew wrote:
>> How does this class get loaded into the JVM?


Daniel Pitts wrote:
> 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!

--
Lew
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      09-20-2007
On Sep 20, 11:40 am, Daniel Pitts <googlegrou...@coloraura.com> wrote:
> On Sep 20, 11:38 am, Daniel Pitts <googlegrou...@coloraura.com> wrote:
>
>
>
> > On Sep 20, 11:28 am, Piotr Kobzda <pi...@gazeta.pl> wrote:

>
> > > Daniel Pitts wrote:
> > > > On Sep 20, 10:23 am, Thomas Fritsch <i.dont.like.s...@invalid.com>
> > > > 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

>
> > 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?si+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?si+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.

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

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


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:
>>> Programs are written in Unicode ...

>
> and
>>> Programs are written using the Unicode character set.


You see? Are *written*, not necessarily *stored* as such.

> Other way around. JSL Chapter 3:
>>> lexical translations are provided (§3.2) so that Unicode escapes
>>> (§3.3) can be used to include any Unicode character using only ASCII
>>> characters.


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.


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

>
> 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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      09-20-2007
WuyaSea Operator <> writes:
>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" ); }}

 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57