Java Forum / General / September 2007
one interview question, 17 lines in java, 3 lines in ruby.
WuyaSea Operator - 19 Sep 2007 20:21 GMT 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. visit http://groups.wuyasea.com/group/ruby-on-rails
I'll answer all your ruby and rails questions.
Dorren http://groups.wuyasea.com/profile/dorren
Lew - 19 Sep 2007 23:42 GMT > Same problem, can be tackled in ruby [sic] by just 3 lines. God, that is /so/ much better! After all, line counts are /so/ important! Much more so than readability!
> 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 APL programmers used to have "one-liner" contests - how much could you do in a one-line program?
Their programs were even more unreadable than your Ruby example.
Which, by the way, is locale-sensitive and will break in some locales.
Is it a benefit or a detriment to express the algorithm so succinctly?
 Signature Lew
Joshua Cranmer - 19 Sep 2007 23:52 GMT > write a program to produce the following output: > 1 A [quoted text clipped - 23 lines] > } > } Want succinctness? I can shrink that further: public class Test { public static void main(String... args) { for (int i=0;i<26;i++) { System.out.print("\n"+(i+1)+" "); for (int j=0;j<=i;j++) System.out.print('A'+j); } System.out.println("\n27 012345678901234567890123456"); } }
That's 10 lines (if you don't what that extra newline, then it'd need to be 11 lines). Actually, there are only five lines of functional logic.
> 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 "Can be" != "should be". Even my ugly compact Java file is more readable than your three lines of Ruby.
Besides, so your Ruby program can print out a contrived example more succinctly than Java. What's the functional difference in the program if the code to be written was to write a fully-functional CSS parser?
> [ snip useless Ruby info ] > [quoted text clipped - 5 lines] > Dorren > http://groups.wuyasea.com/profile/dorren Don't you want the Ruby newsgroup, you spamming troll?
 Signature Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
WuyaSea Operator - 20 Sep 2007 01:17 GMT > System.out.println("\n27 012345678901234567890123456"); > That's 10 lines (if you don't what that extra newline, then it'd need to if you're going to cut down the line count by cheating there, nothing i can say.
> > 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 if you have trouble reading the 2nd line, here is rewrite: n = ?A - 1 (1..26).each do |i| puts i.to_s + " " + (i+n).chr*i end puts "27 " + (0..27).collect{|i| i%10 }.to_s # prints numbers line
array.each{|item| operation...} notation is called closure, powerful and very commonly used in ruby.
> > [ snip useless Ruby info ] with that attitude, there's nothing i can help you.
> Don't you want the Ruby newsgroup, you spamming troll? Do not call me troll, you cheater.
once again, when you're ready. visit http://groups.wuyasea.com/group/ruby-on-rails
Dorren http://groups.wuyasea.com/profile/dorren
Lasse Reichstein Nielsen - 20 Sep 2007 01:41 GMT > array.each{|item| operation...} notation is called closure, powerful > and very commonly used in ruby. ... and many other languages. Admittedly, Java is not one of them.
> with that attitude, there's nothing i can help you. I seem to remember you comming into a Java discussion group with an attitude of "see, my language is better than yours". That's called "being off-topic". Nobody here asked for your help.
> Do not call me troll, you cheater. Either a troll or a spammer. That depends on whether you want people to get insulted, or you are merely disingenious enough not to expect it.
/L
 Signature Lasse Reichstein Nielsen - lrn@hotpop.com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'
Lew - 20 Sep 2007 01:57 GMT WuyaSea Operator <wuyasea@gmail.com> writes:
>> Do not call me troll, you cheater.
> Either a troll or a spammer. That depends on whether you want people > to get insulted, or you are merely disingenious enough not to expect > it. One of the marks of a troll is that they respond to perceived /ad hominem/ attacks, but ignore the measured responses to the technical points.
Does that apply here?
Let's see, Joshua said,
> "Can be" != "should be". > Even my ugly compact Java file is more readable than your three lines of Ruby. Not answered.
> Besides, so your Ruby program can print out a contrived example more succinctly than Java. > What's the functional difference in the program if the code to be written was > to write a fully-functional CSS parser? Not answered.
> Don't you want the Ruby newsgroup, Not answered.
> you spamming troll? Answered.
A full participant wouldn't have reacted negatively, but understood that posting their website URL and offering to answer people's questions makes their post seem an awful lot like spam, especially given the supercilious tone of the posts, and worked to overcome that negative impression instead of reacting with defensive hostility.
Speaking of which, "cheater"? That's a witty thing to call Joshua. How does "cheater" apply in any plausible way to Joshua? At what could he have been cheating? How is that even a response worthy of pressing "Send"?
I actually am very strongly interested in Ruby, and shall be learning it post-haste. It's totally the next wave - but comparing Java to Ruby is like comparing jet liners to hang gliders => "You see? My hang glider doesn't require all those radios and fuel lines and control panels!"
For sure I will not use the OP or their web site for any guidance now that they have completely burned their credibility with me. Oh, I suppose if they respond to this post in a reasonable manner I'll reconsider that decision.
Perhaps.
 Signature Lew
WuyaSea Operator - 20 Sep 2007 02:07 GMT > I seem to remember you comming into a Java discussion group with an > attitude of "see, my language is better than yours". That's called [quoted text clipped - 3 lines] > to get insulted, or you are merely disingenious enough not to expect > it. 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.
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.
Thanks for the advice though.
If you're ready to learn ruby, and Ruby on Rails. visit http://groups.wuyasea.com/group/ruby-on-rails
Dorren http://groups.wuyasea.com/profile/dorren
arabzwaj50@gmail.com - 20 Sep 2007 03:38 GMT http://arabwazaef.com/arab/
http://www.arabzwaj.com/welcome/viewtopic.php?t=2417&sid=eedb9c67db690f3d5fa9cd2 7e6f93e7d
http://www.arabzwaj.com/welcome/viewtopic.php?t=2416&sid=eedb9c67db690f3d5fa9cd2 7e6f93e7d
.....
http://www.arabzwaj.com/welcome/viewtopic.php?t=2423&sid=eedb9c67db690f3d5fa9cd2 7e6f93e7d http://www.arabzwaj.com/welcome/viewtopic.php?t=2422&sid=eedb9c67db690f3d5fa9cd2 7e6f93e7d http://www.arabzwaj.com/welcome/viewtopic.php?t=2418&sid=eedb9c67db690f3d5fa9cd2 7e6f93e7d
!!! http://www.arabzwaj.com/welcome/viewtopic.php?t=2367&sid=6c6614c5f9dd9b9eaa8c035 5e13e8f9c
http://www.arabzwaj.com/welcome/viewtopic.php?t=2332 ...!
http://www.arabzwaj.com/welcome/viewtopic.php?p=11408#11408
http://www.arabzwaj.com/welcome/viewtopic.php?p=11395#11395 http://www.arabzwaj.com/welcome/viewtopic.php?t=2329
www.arabzwaj.com
http://www.arabzwaj.com/welcome/viewtopic.php?t=2333 http://www.arabzwaj.com/welcome/viewtopic.php?t=2332 http://www.arabzwaj.com/welcome/viewtopic.php?t=2328 http://www.arabzwaj.com/welcome/viewtopic.php?p=11403#11403 http://www.arabzwaj.com/welcome/viewtopic.php?p=11404#11404
http://www.arabzwaj.com/welcome/viewtopic.php?t=2336 http://www.arabzwaj.com/welcome/viewtopic.php?p=11406#11406
...!
http://www.arabzwaj.com/welcome/viewtopic.php?p=11408#11408 ! http://www.arabzwaj.com/welcome/viewtopic.php?t=2340 .............. !!!!! http://www.arabzwaj.com/welcome/viewtopic.php?t=2341
http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2303 http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2299&start=0&postdays=0 &postorder=asc&highlight= - http://www.arabzwaj.com/welcome/viewtopic.php?t=2278 ( ) http://www.arabzwaj.com/welcome/viewtopic.php?t=2279 http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2302&start=0&postdays=0 &postorder=asc&highlight= http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2301
http://www.arabzwaj.com/welcome/viewtopic.php?t=2270 http://www.arabzwaj.com/welcome/viewtopic.php?t=2272 http://www.arabzwaj.com/arabzwaj/welcome/viewtopic.php?t=2297 ( )
http://www.arabzwaj.com/welcome/viewtopic.php?t=2271 ( ) http://www.arabzwaj.com/welcome/viewtopic.php?t=2273 http://www.arabzwaj.com/welcome/viewtopic.php?t=2264
>>FIFA 2007 http://www.arabzwaj.com/welcome/viewtopic.php?t=2266
www.arabzwaj.com
for marrige and friend ship www.arabzwaj.com
www.arabzwaj.com
www.arabzwaj.com
the best web site formarrige and frindship www.arabzwaj.com
:- www.arabzwaj.com/welcome
enter here and watch sexy arab girls
www.arabwazaef.com/arab
http://www.arabzwaj.com/welcome/viewtopic.php?t=1892 http://www.arabzwaj.com/welcome/viewtopic.php?t=900 **** **** http://www.arabzwaj.com/welcome/viewtopic.php?t=1894 ( ) http://www.arabzwaj.com/welcome/viewtopic.php?t=1880 http://www.arabzwaj.com/welcome/viewtopic.php?t=1879
a + + http://www.arabzwaj.com/welcome/viewtopic.php?t=1956
http://www.arabzwaj.com/welcome/viewtopic.php?p=10201#10201 .... http://www.arabzwaj.com/welcome/viewtopic.php?p=10210#10210 000000 0 http://www.arabzwaj.com/welcome/viewtopic.php?t=686 ..! http://www.arabzwaj.com/welcome/viewtopic.php?p=10203#10203 http://www.arabzwaj.com/welcome/viewtopic.php?p=10207#10207 http://www.arabzwaj.com/welcome/viewtopic.php?p=10211#10211
.... !!! http://www.arabzwaj.com/welcome/viewtopic.php?p=10200#10200 http://www.arabzwaj.com/welcome/viewtopic.php?p=10212#10212 ..... !! http://www.arabzwaj.com/welcome/viewtopic.php?p=10208#10208
:- http://www.arabzwaj.com/welcome/viewtopic.php?p=10095#10095 5000 http://www.arabzwaj.com/welcome/viewtopic.php?t=1952&sid=15f0596ebc9c205da75f65e b723025a2 http://www.arabzwaj.com/welcome/viewtopic.php?t=1587&start=0&postdays=0&postorde r=asc&highlight= http://www.arabzwaj.com/welcome/viewtopic.php?p=10213#10213
http://www.arabzwaj.com/welcome/viewtopic.php?p=10209#10209 http://www.arabzwaj.com/welcome/viewtopic.php?p=10205#10205 ....... http://www.arabzwaj.com/welcome/viewtopic.php?p=10206#10206
:- http://www.arabzwaj.com/welcome/viewtopic.php?p=9855#9855
000000 0 http://www.arabzwaj.com/welcome/viewtopic.php?t=686 http://www.arabzwaj.com/welcome/viewtopic.php?t=1873 http://www.arabzwaj.com/welcome/viewtopic.php?t=124
:- http://www.arabzwaj.com/welcome/viewtopic.php?p=9855#9855
( ) http://www.arabzwaj.com/welcome/viewtopic.php?p=9736#9736 Windows Live Messenger 8.0 http://www.arabzwaj.com/welcome/viewtopic.php?t=1834 ( ) http://www.arabzwaj.com/welcome/viewtopic.php?t=1831 http://www.arabzwaj.com/welcome/viewtopic.php?t=1812
http://www.arabzwaj.com/welcome/posting.php?mode=editpost&p=9729
... .. http://www.arabzwaj.com/welcome/viewtopic.php?t=1796
http://www.arabzwaj.com/welcome/viewtopic.php?t=1789 http://www.arabzwaj.com/welcome/viewtopic.php?t=1568&highlight=%E3%E1%DD 3 http://www.arabzwaj.com/welcome/viewtopic.php?t=1830
http://www.arabzwaj.com/welcome/viewtopic.php?t=1790 http://www.arabzwaj.com/welcome/viewtopic.php?t=102
00, http://www.arabzwaj.com/welcome/viewtopic.php?p=9739#9739 ( ) http://www.arabzwaj.com/welcome/viewtopic.php?p=9740#9740 2038 http://www.arabzwaj.com/welcome/viewtopic.php?t=1837 http://www.arabzwaj.com/welcome/viewtopic.php?t=1824 http://www.arabzwaj.com/welcome/viewtopic.php?t=1821 http://www.arabzwaj.com/welcome/viewtopic.php?t=1768 !! http://www.arabzwaj.com/welcome/viewtopic.php?t=1751 http://www.arabzwaj.com/welcome/viewtopic.php?t=1838 http://www.arabzwaj.com/welcome/viewforum.php?f=11 http://www.arabzwaj.com/welcome/viewtopic.php?t=234 www.arabzwaj.com :-arabzwaj@yahoo.com hide ip + : :- http://d.turboupload.com/d/839284/157615851606157516051580hide.rar.html :- http://www.megaupload.com/?d=BBCGC4J0
the best web site formarrige and frindship www.arabzwaj.com
Joshua Cranmer - 20 Sep 2007 03:40 GMT > 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.
 Signature Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
Chris Smith - 21 Sep 2007 14:59 GMT > 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.
 Signature Chris Smith
RedGrittyBrick - 19 Sep 2007 23:54 GMT [counterproductive Ruby propaganda omitted]
Please don't multipost http://www.cs.tut.fi/~jkorpela/usenet/xpost.html
Lasse Reichstein Nielsen - 20 Sep 2007 01:30 GMT > 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 Does this really give a numbers line ending in "6"?
Anyway, in Perl it is also easily doable in three lines:
my $n = ord("A") - 1; for $i (1..26) { print "$i ", chr($i+$n) x $i, "\n";} print "27 ", (map {$_ % 10} (0..26)), "\n";
This is time for considering whether doing something in three lines is really a mark of quality.
/L 'Everything can be done in three lines of Perl'
 Signature Lasse Reichstein Nielsen - lrn@hotpop.com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'
Manish Pandit - 20 Sep 2007 01:35 GMT > write a program to produce the following output: > 1 A [quoted text clipped - 58 lines] > > Dorrenhttp://groups.wuyasea.com/profile/dorren Having used Ruby a couple of times myself, I found that Ruby is pretty good, concise and robust for scripting and for prototyping something solid + workable real fast.
However, comparing programming languages based on how many lines of code does it take to do XXX may not be the best way to look at things. Everything would be a shell script if that were the case. There are things like developer productivity, cost and ease of maintenance, availability of skilled developers and resources within budget, etc. that go behind acceptance of any programming language in an organization too. Given the current market trends (and the me-too Web 2.0 wave), Ruby and frameworks around it are picking up and it is poised to become one of the mainstream languages in the near future.
-cheers, Manish
Daniel Pitts - 20 Sep 2007 06:28 GMT > write a program to produce the following output: > 1 A [quoted text clipped - 58 lines] > > 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?
Lew - 20 Sep 2007 13:56 GMT > 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.
 Signature Lew
Daniel Pitts - 20 Sep 2007 15:27 GMT > > 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 [quoted text clipped - 17 lines] > -- > 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.
Piotr Kobzda - 20 Sep 2007 17:34 GMT > 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
Lew - 20 Sep 2007 18:18 GMT > FWIW, here is 177 bytes long variant of it: > [quoted text clipped - 3 lines] > > Can anyone beat it? :-) Actually that's a 177- /char/ -long variant. Java source is stored in characters, not bytes.
 Signature Lew
Piotr Kobzda - 20 Sep 2007 19:03 GMT > 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
Lew - 20 Sep 2007 19:37 GMT 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. :-) 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 [quoted text clipped - 5 lines] >> 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>
> 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.
 Signature Lew
Piotr Kobzda - 20 Sep 2007 22:14 GMT >> 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
Lew - 20 Sep 2007 23:16 GMT >>> 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. 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.
 Signature Lew
Piotr Kobzda - 21 Sep 2007 01:32 GMT > 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
Thomas Fritsch - 20 Sep 2007 18:23 GMT > FWIW, here is 177 bytes long variant of it: > [quoted text clipped - 3 lines] > > Can anyone beat it? :-) Mine is 3 bytes shorter than yours! Just replace ('A'+i-1) by (64+i) ;-)
 Signature Thomas
Daniel Pitts - 20 Sep 2007 19:12 GMT > > FWIW, here is 177 bytes long variant of it: > [quoted text clipped - 10 lines] > -- > 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);}}
Piotr Kobzda - 20 Sep 2007 19:28 GMT >>> FWIW, here is 177 bytes long variant of it: >>> class C{static{String s="27 0";for(int [quoted text clipped - 12 lines] > %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
Daniel Pitts - 20 Sep 2007 19:38 GMT > >>> FWIW, here is 177 bytes long variant of it: > >>> class C{static{String s="27 0";for(int [quoted text clipped - 21 lines] > > 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);}}
Daniel Pitts - 20 Sep 2007 19:40 GMT > > >>> FWIW, here is 177 bytes long variant of it: > > >>> class C{static{String s="27 0";for(int [quoted text clipped - 32 lines] > +='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
Daniel Pitts - 20 Sep 2007 20:06 GMT > > > >>> FWIW, here is 177 bytes long variant of it: > > > >>> class C{static{String s="27 0";for(int [quoted text clipped - 37 lines] > > 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.
Joshua Cranmer - 20 Sep 2007 23:01 GMT > 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.
 Signature Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
Daniel Pitts - 20 Sep 2007 23:05 GMT > > 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', [quoted text clipped - 13 lines] > Beware of bugs in the above code; I have only proved it correct, not > tried it. -- Donald E. Knuth That doesn't work. You HAVE to cast to char.
Daniel Pitts - 21 Sep 2007 00:34 GMT > > > 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', [quoted text clipped - 16 lines] > 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);}}
Piotr Kobzda - 21 Sep 2007 01:27 GMT > 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
Daniel Pitts - 21 Sep 2007 02:39 GMT > > Although, I did find a way to trim two more chars off: > [quoted text clipped - 10 lines] > > piotr Actually, thats 134 by my count.
Oh, actually, all my counts are one off :-) Silly me. You're right, 133.
Nice...
Piotr Kobzda - 21 Sep 2007 21:31 GMT >> 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);}} 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
Daniel Pitts - 22 Sep 2007 01:40 GMT > >> 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);}} [quoted text clipped - 16 lines] > > 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.
Daniel Pitts - 22 Sep 2007 02:25 GMT > >> 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);}} [quoted text clipped - 16 lines] > > 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);}}
Piotr Kobzda - 22 Sep 2007 11:09 GMT > 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
Lew - 22 Sep 2007 14:45 GMT >> 125 chars: >> [quoted text clipped - 10 lines] > > :-) See? Java does rival Ruby for compactness and obscurity of expression!
 Signature Lew
Daniel Pitts - 22 Sep 2007 19:06 GMT > > 125 chars: > [quoted text clipped - 12 lines] > > 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.
Lew - 22 Sep 2007 22:19 GMT > 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.
 Signature Lew
Piotr Kobzda - 24 Sep 2007 01:57 GMT > 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) [quoted text clipped - 13 lines] > 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
Piotr Kobzda - 24 Sep 2007 02:09 GMT > 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
Ben Phillips - 24 Sep 2007 02:50 GMT >> 119 chars: >> [quoted text clipped - 5 lines] > 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 ...
Lew - 20 Sep 2007 19:39 GMT > 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?
 Signature Lew
Daniel Pitts - 20 Sep 2007 19:57 GMT > > Well, 161 chars: > [quoted text clipped - 5 lines] > -- > 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.
Lew - 20 Sep 2007 19:59 GMT 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.replace('A',(char)(64+i)));System.out.println(s);System.exit(0);}} Lew wrote:
>> 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. What a great hack!
 Signature Lew
Daniel Pitts - 21 Sep 2007 01:13 GMT > Piotr Kobzda wrote: > >>> Well, 161 chars: [quoted text clipped - 12 lines] > -- > Lew 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.
:-) Laurent D.A.M. MENTEN - 20 Sep 2007 19:59 GMT Lew a écrit :
>> 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? Java C
and it works! so we can deduce that the JVM executes the static initialiser before it looks up the main method...
Stefan Ram - 20 Sep 2007 22:20 GMT >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" ); }}
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|