Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Multi-language golf contest!
Index -> Contests
Goto page Previous  1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
btiffin




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
btiffin




PostPosted: 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
Zeroth




PostPosted: 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)
gitoxa




PostPosted: 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?
btiffin




PostPosted: 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
Zeroth




PostPosted: 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?
rizzix




PostPosted: Mon Jun 23, 2008 10:37 pm   Post subject: RE:Multi-language golf contest!

Ugh, don't you mean windows. :\
gitoxa




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Zeroth




PostPosted: 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.
Aziz




PostPosted: 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.
Zeroth




PostPosted: 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.
Aziz




PostPosted: Tue Jun 24, 2008 12:04 pm   Post subject: RE:Multi-language golf contest!

So? :<
Zeroth




PostPosted: 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.
apomb




PostPosted: 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.
Aziz




PostPosted: Tue Jun 24, 2008 2:45 pm   Post subject: RE:Multi-language golf contest!

Oh dear god, not perl golf.
Display posts from previous:   
   Index -> Contests
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 3  [ 32 Posts ]
Goto page Previous  1, 2, 3  Next
Jump to:   


Style:  
Search: