
-----------------------------------
Zeroth
Sat Jun 21, 2008 12:29 pm

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. ;) Oh, and \t characters count as one character. ;)

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.

-----------------------------------
rizzix
Sat Jun 21, 2008 1:08 pm

RE:Multi-language golf contest!
-----------------------------------
Mods can sponsor bits.

However, I'm curious, why limit it to only those languages?

-----------------------------------
Zeroth
Sat Jun 21, 2008 1:12 pm

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.

-----------------------------------
rizzix
Sat Jun 21, 2008 1:20 pm

RE:Multi-language golf contest!
-----------------------------------
Perl, Haskell, Scala, O'Caml, Common Lisp, to name a few.

-----------------------------------
Zeroth
Sat Jun 21, 2008 1:38 pm

Re: Multi-language golf contest!
-----------------------------------
Umm, what are the comment delimiters for those languages?

-----------------------------------
rizzix
Sat Jun 21, 2008 1:47 pm

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)

-----------------------------------
gitoxa
Sat Jun 21, 2008 9:33 pm

RE:Multi-language golf contest!
-----------------------------------
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. :P

-----------------------------------
Zeroth
Sat Jun 21, 2008 10:01 pm

Re: Multi-language golf contest!
-----------------------------------
Well, its assumed if anyone finds a bug in my code, they'll point it out. :P You're disqualified. ;) (Not really, just kidding)

-----------------------------------
gitoxa
Sat Jun 21, 2008 10:17 pm

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.
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.

-----------------------------------
Zeroth
Sat Jun 21, 2008 11:32 pm

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. :)

-----------------------------------
Zeroth
Sat Jun 21, 2008 11:33 pm

Re: Multi-language golf contest!
-----------------------------------
Don't forget to handle strings!

-----------------------------------
gitoxa
Sun Jun 22, 2008 1:37 am

RE:Multi-language golf contest!
-----------------------------------
after a lengthy google search, o'caml's comment is
(*  Comments here *)

-----------------------------------
Zeroth
Sun Jun 22, 2008 8:55 pm

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!

-----------------------------------
Saad
Sun Jun 22, 2008 8:58 pm

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.

-----------------------------------
Zeroth
Sun Jun 22, 2008 9:47 pm

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.

-----------------------------------
btiffin
Sun Jun 22, 2008 10:22 pm

Re: Multi-language golf contest!
-----------------------------------
Here is one in REBOL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; rebol script to count non comment characters
REBOL 
>> 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 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.

-----------------------------------
btiffin
Sun Jun 22, 2008 11:16 pm

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
;; 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
Mon Jun 23, 2008 9:11 am

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
Mon Jun 23, 2008 7:24 pm

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.
text = text.replace('\r\n', '\n')
What does that do? More specifically, what is the "\r" character?

-----------------------------------
btiffin
Mon Jun 23, 2008 7:40 pm

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
Mon Jun 23, 2008 9:59 pm

Re: Multi-language golf contest!
-----------------------------------
Of course I won't mind. It was helpful and constructive. ;)

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
Mon Jun 23, 2008 10:37 pm

RE:Multi-language golf contest!
-----------------------------------
Ugh, don't  you mean windows. :\

-----------------------------------
gitoxa
Mon Jun 23, 2008 11:23 pm

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 :)
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