Computer Science Canada

Multi-language golf contest!

Author:  Zeroth [ Sat Jun 21, 2008 12:29 pm ]
Post subject:  Multi-language golf contest!

This is a very simple contest I decided to run. I want to encourage skill and discussion in various programming languages. Anyway, the contest is: Write code, in one of the language categories solving one of the tasks given, in as few characters as possible. I will not be comparing languages against each; Each entry will only be compared against entries in the same language.

Language categories:

  • Java
  • C
  • C++
  • Turing
  • Python
  • Ruby


I've only thought up one task, but if you guys have any suggestions, I'll be glad to hear them.
The first task is: make a compact code entry character counter. It must be able to ignore comments in any of the entries, count newlines and carriage returns. Carriage returns should count as one character. Oh, and don't forget to skip over characters in a string. Wink Oh, and \t characters count as one character. Wink

Of course, the entries should work.

What do you win? Why, prestige of course. I'll also donate a bunch of bits... except I don't have many, lol. Probably learn stuff, and become a better programmer.

Here is my "naive" entry, which I believe works 100%. If your entry does not give the same result, it is disqualified. And for reference, using it on itself, gives a result of 910 characters.

Author:  rizzix [ Sat Jun 21, 2008 1:08 pm ]
Post subject:  RE:Multi-language golf contest!

Mods can sponsor bits.

However, I'm curious, why limit it to only those languages?

Author:  Zeroth [ Sat Jun 21, 2008 1:12 pm ]
Post subject:  Re: Multi-language golf contest!

Well, because I knew the comment delimiting characters in those languages? Remember, the task of the compact character counter includes skipping over comments. If you have a language you'd like to try, suggest it.

Author:  rizzix [ Sat Jun 21, 2008 1:20 pm ]
Post subject:  RE:Multi-language golf contest!

Perl, Haskell, Scala, O'Caml, Common Lisp, to name a few.

Author:  Zeroth [ Sat Jun 21, 2008 1:38 pm ]
Post subject:  Re: Multi-language golf contest!

Umm, what are the comment delimiters for those languages?

Author:  rizzix [ Sat Jun 21, 2008 1:47 pm ]
Post subject:  RE:Multi-language golf contest!

For Scala its the same as Java.
For Perl, its the same as Python, Ruby, etc.
For Haskell: --
For Lisp: ;

For O'Caml, well I don't know.


(I'm assuming by comments we're talking about line-comments only)

Author:  gitoxa [ Sat Jun 21, 2008 9:33 pm ]
Post subject:  RE:Multi-language golf contest!

Quote:
which I believe works 100%. If your entry does not give the same result, it is disqualified


There's something wrong with those two sentences.
Not bashing your program at all; just your logic. Razz

Author:  Zeroth [ Sat Jun 21, 2008 10:01 pm ]
Post subject:  Re: Multi-language golf contest!

Well, its assumed if anyone finds a bug in my code, they'll point it out. Razz You're disqualified. Wink (Not really, just kidding)

Author:  gitoxa [ Sat Jun 21, 2008 10:17 pm ]
Post subject:  RE:Multi-language golf contest!

Haha, true enough. On to making my own, though. I'll probably do turing first, just to help me get my thoughts out easily with a familiar language, then give another one a try. Perhaps python, since I already have something to compare with after.

Oh, and are we supposed to somewhat follow standard coding conventions? Or are we just going for the lowest amount of characters... heh.

One other quick question, I don't completely understand what you mean about the strings. Is an entire string being counted as 1, are you ignoring the quotes, or does python do something weird with quotes when trying to get input?

yet another edit:
I noticed your program counts the line feeds at the end of comments as a character, for example, the following results 7.
code:
Hello #Hi
#Goodbye

is that on purpose?


And I may come up with some other questions shortly. I ran my program on your code and got a different number. ...A very different number.

Author:  Zeroth [ Sat Jun 21, 2008 11:32 pm ]
Post subject:  Re: Multi-language golf contest!

That is on purpose. Because \n still counts as a character, that IS read by compilers/interpreters. Sometimes it has meaning, sometimes it doesn't. Comments however, are not read by compilers/interpreters. Good questions. Smile

Author:  Zeroth [ Sat Jun 21, 2008 11:33 pm ]
Post subject:  Re: Multi-language golf contest!

Don't forget to handle strings!

Author:  gitoxa [ Sun Jun 22, 2008 1:37 am ]
Post subject:  RE:Multi-language golf contest!

after a lengthy google search, o'caml's comment is
(* Comments here *)

Author:  Zeroth [ Sun Jun 22, 2008 8:55 pm ]
Post subject:  Re: Multi-language golf contest!

Okay, Gitoxa has his entries in:
For the languages I suggested, in Turing, his code comes in at 409 characters.
For all the languages, in Turing, his code comes in at 548 characters. Good job Gitoxa!

Author:  Saad [ Sun Jun 22, 2008 8:58 pm ]
Post subject:  Re: Multi-language golf contest!

Will there be nested quotes, like "Foo \" nested Foobar \" Bar" ?

Edit: I also see your code supports pything, turing and ruby, but from the main post I am confused about what language (s) are supposed to be supported by our code.

Author:  Zeroth [ Sun Jun 22, 2008 9:47 pm ]
Post subject:  Re: Multi-language golf contest!

The main qualities are the languages I stated in my main post. You can also support the languages rizzix suggested as well, for a separate category.

Author:  btiffin [ Sun Jun 22, 2008 10:22 pm ]
Post subject:  Re: Multi-language golf contest!

Here is one in REBOL
REBOL:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; rebol script to count non comment characters
REBOL [
    ;Author: "Brian Tiffin"
    ;Date: 22-Jun-2008
]
;; define the line comments
cmts: [
    rebol [";" "^/" ";" "^/"]
    lisp [";" "^/" ";" "^/"]
    python ["#" "^/" "#" "^/"]
    ruby ["#" "^/" "#" "^/"]
    perl ["#" "^/" "#" "^/"]
    turing ["%" "^/" "%" "^/"]
    c ["/*" "*/" "/*" "*/"]
    haskell ["--" "^/" "--" "^/"]
    java ["//" "^/" "/*" "*/"]
]
;; replace \\ with \ and \t with tab
replen: func [str] [length? replace/all replace/all str "\\" "\" "\t" "^-"]
;; function to count the characters
count: func [data lang] [
    ;; select start/end comment string! by language
    cs: select cmts to word! lang
    ;; rebol is the only different one for apostrophe versus brace
    qs: copy ["'" "'"]
    if lang = 'rebol [qs/1: "{" qs/2: "}"]
    ;; the source is either string! of read from file!
    src: either file? data [read data][data]
    c: 0
    ;; start a pattern match parse pass, and return count
    parse/all src [
        some [()
          ;; match over \" and count as 1
          "\^"" (c: c + 1)
          ;; or match over and count quoted strings
          | ["^"" copy str thru "^"" | qs/1 copy str thru qs/2]
              (c: add add c replen str 1)
          ;; or skip thru line comments or C style, only supports 2
          | cs/1 [thru cs/2 | to end]
          | cs/3 [thru cs/4 | to end]
          ;; otherwise count it
          | skip (c: c + 1)
        ]
    ] c
]
;; two sample runs
print count %chars.r 'rebol
print count %compact.py 'python

code:
>> do %chars.r
Script: "Untitled" (none)
1001
906

I cheated in a few places; In real life the parse block would be built at run-time by language, but that would cost more characters. REBOL doesn't need newlines or indentation in sources, so this can shrink down a few hundred bytes but it starts becoming obfuscated.

And just for fun. This is REBOL code that counts non-comment characters of REBOL code, saved as %rebchars.r
REBOL:

REBOL []
count: func [data] [print length? mold/all load/all data]
count %rebchars.r
97

Cheers
P.S. Zeroth, I'm not in it for the bits. I'm in it to prod others to try your exercise and ra! ra! REBOL ra!.
P.P.S. Anyone reading this. I've not done any unit level testing ... bad form ... this code would not be accepted by any of the management I deal with and I don't trust it to be correct. My 906 versus Zeroth's 910 means something is up. This was just for fun.

Author:  btiffin [ Sun Jun 22, 2008 11:16 pm ]
Post subject:  Re: Multi-language golf contest!

Rambling now. The off by four bugged me. So it was the counting of the end of line line comment newline that was the difference.

To get 910 from my code, I'd have to change
code:
;; skip to line comments or C style, only supports 2
    | cs/1 [to cs/2 | to end]
    | cs/3 [thru cs/4 | to end]
the first thru PARSE command becomes a to PARSE command. Now those newlines are counted. The second thru would stay as is. It supports Java (or C), but Java in particular, /* */ block comments along with // line comments

Cheers, and excuse the old guy

Author:  Zeroth [ Mon Jun 23, 2008 9:11 am ]
Post subject:  Re: Multi-language golf contest!

I have to admit that is very freakin' awesome. I've been wondering just how small I could get my python entry. I will look into REBOL when I have the time(namely when I don't have two Java frontends to build at my job, and my spare-time projects are finished. XD)

Author:  gitoxa [ Mon Jun 23, 2008 7:24 pm ]
Post subject:  RE:Multi-language golf contest!

Zeroth, I gave a shot at writing the program in python, and it seems to work fine. But upon comparing it to your code, after opening the file, you used this line.
code:
text = text.replace('\r\n', '\n')

What does that do? More specifically, what is the "\r" character?

Author:  btiffin [ Mon Jun 23, 2008 7:40 pm ]
Post subject:  RE:Multi-language golf contest!

Hoping Zeroth doesn't mind...

\r is return. This changes any possible MSDOS CRLF line terminators to LF (sane) newline terminators.

Cheers

Author:  Zeroth [ Mon Jun 23, 2008 9:59 pm ]
Post subject:  Re: Multi-language golf contest!

Of course I won't mind. It was helpful and constructive. Wink

Further explanation: \r\n is called a "carriage return", and unix uses it still. So, no sense to penalize unfairly the *nix users that would enter the contest by counting the extraneous character, is it?

Author:  rizzix [ Mon Jun 23, 2008 10:37 pm ]
Post subject:  RE:Multi-language golf contest!

Ugh, don't you mean windows. :\

Author:  gitoxa [ Mon Jun 23, 2008 11:23 pm ]
Post subject:  Re: Multi-language golf contest!

I had trouble with the comments part, tried doing it the same way as my turing program, but it was giving me trouble.

results 283 on itself Smile
code:
def a(N,L):#for L, 1=python/ruby, 2=turing, 3=Java, 4=c/c++
        f=open(N)
        F=f.read()
        C=0
        I=0
        S=False
        for X in F:
                if X=='"'or X=="'":
                        S=~S
                if((L==1and X=="#")or(L==2and X=="%")or(L==3and X=="/")or(L==4and F[I:I+2]=="//"))and S==False:
                        E=F[I:].find("\n")
                        if E<0:
                                return C
                        C-=E
                C+=1
                I+=1
        f.close()
        return C

Author:  Zeroth [ Tue Jun 24, 2008 9:42 am ]
Post subject:  Re: Multi-language golf contest!

Unfortunately, I found a logic error in your code, and it does not give 910 when tested on compact.py

I can't find the error, even after tinkering with it. I keep getting 286, or 281 from my baseline counter.

What do you mean Rizzix? Oh wait, yeah, I had meant windows for the \r\n thing. Whoops, lol.

Author:  Aziz [ Tue Jun 24, 2008 10:20 am ]
Post subject:  RE:Multi-language golf contest!

And why did no one catch on in my Ruby Quiz. I may give this a quick try over lunch.

Author:  Zeroth [ Tue Jun 24, 2008 11:50 am ]
Post subject:  Re: RE:Multi-language golf contest!

Aziz @ Tue Jun 24, 2008 7:20 am wrote:
And why did no one catch on in my Ruby Quiz. I may give this a quick try over lunch.
Its because I'm sexier.

Author:  Aziz [ Tue Jun 24, 2008 12:04 pm ]
Post subject:  RE:Multi-language golf contest!

So? :<

Author:  Zeroth [ Tue Jun 24, 2008 1:15 pm ]
Post subject:  Re: Multi-language golf contest!

The best I could do was 811 characters in python. Full support of all the languages, including haskell, string handling, etc.
EDIT: with some changes to lines, got it to 777.

Author:  apomb [ Tue Jun 24, 2008 2:40 pm ]
Post subject:  RE:Multi-language golf contest!

I will try this when i have some free time... unless there is an ending date, it might be a while before i get to it

look for my entry to be coded in Perl, because in my opinion it is probably the most well suited for this type of task.

Author:  Aziz [ Tue Jun 24, 2008 2:45 pm ]
Post subject:  RE:Multi-language golf contest!

Oh dear god, not perl golf.

Author:  apomb [ Tue Jun 24, 2008 2:47 pm ]
Post subject:  RE:Multi-language golf contest!

dont worry, it will be completely obfuscation-free Wink

Author:  Aziz [ Tue Jun 24, 2008 3:02 pm ]
Post subject:  RE:Multi-language golf contest!

All we need is a one-liner bash submission Smile


: