Howard Bullock
(KiX Supporter)
2002-06-04 04:38 AM
KixGolf scoring script

code:
; This function scores the specified file and returns the KixGolf score.
; That is the total number of key strokes used to build a script not counting
; comments, spaces, tabs, or any ascii character above 126.
Function KixGolf($A)
if open (1,$A) = 0
$X=0
$Q=""
$C=readLine(1)
while @error=0
$j=len($C) ; Get length of the line
for $h=1 to $j ; Loop through each character in line
$D=asc(substr($C,$h,1))
if $D=34 or $D=39
if $Q=""
$Q=$D
else
if $Q=$D
$Q=""
endif
endif
endif

if $D=59 ; Ignore comment lines
$h=$j ; Terminate loop
else
if $Q<>""
$X=$X+1 ; Count the valid characters
else
if $D>32 and $D<127
$X=$X+1 ; Count the valid characters
endif
endif
endif
next
$C=readLine(1)
loop
else
? "$A ??" ; Show bad file name
endif
if $Q<>""
? "Mis-matched quotes. Probable score error." ?
endif
$KixGolf=$X
EndFunction

? "KixGolf score = " + KixGolf($A) ;$A must be initialized on the command line.
;%tmp%\kix32 kixgolf.kix $A=Kixgolf.kix



[ 04 June 2002, 19:46: Message edited by: Howard Bullock ]


ShawnAdministrator
(KiX Supporter)
2002-06-04 02:30 PM
Re: KixGolf scoring script

Nice work Howard - ok, we have our clubs (Kixtart) and now we have our scorecards (KixGolf), all we need now is a fairway (Jens) and a rainy weekend ... four !!! [or should we say: four point one zero!!!] [Wink]

-Shawn


ShawnAdministrator
(KiX Supporter)
2002-06-04 03:39 PM
Re: KixGolf scoring script

So Howard, I guess the strategy would be to develop our solution as a UDF (assuming the puzzle was presented as such) in a seperate file, say kixgolf.kix, and this script would contain just a single function - the UDF being worked on. Then we would also have a "proof script", that included and called the puzzle UDF, this would be our driver script.

Then, we would run your kixgolf script against our solution script, and that we be the official golf score, yeah ?

-Shawn


Howard Bullock
(KiX Supporter)
2002-06-04 04:04 PM
Re: KixGolf scoring script

Once presented with a problem, we could all write a solution then post just the KixGolf score. It would be interesting if everyone that tried would post thier score even if it was not the best. They could then retry and post an improved score if possible. Then after 24 hours we could post the code.

The solution could be what ever was called for, a UDF or just script code. You would save your entry to a file (you choose the name). Then score your solution by executing:

Kix32 KixGolf $A=YourFile

You will first have to save the above code to a file called KixGolf.kix. It just so happens that I used the script on itself just to see how many strokes it was. [Wink]

{edit}
I see how the score for the scoring script can be improved by one stroke already.

[ 04 June 2002, 16:08: Message edited by: Howard Bullock ]


ShawnAdministrator
(KiX Supporter)
2002-06-04 04:25 PM
Re: KixGolf scoring script

Might be able to improve the kixgolf script score by a few strokes if you used the split function to "parse-out" the script, line-by-line. A default split (no second parm specified) should be able automatically skip over all the white-space that your checking for now, then all thats left is to do some sort of instr($c,";") to get at the comments ?

-Shawn


BrianTX
(Korg Regular)
2002-06-04 04:26 PM
Re: KixGolf scoring script

slight problem with the script:

What if someone encloses a ; between quotes? Or what if a space is enclosed between quotes? Shouldn't these count as part of your score as well because they are pertinent to the code?

IMHO:

The only spaces or tabs that shouldn't be counted are at the beginning and end of a line. All text within quotes should be counted. Other characters above ASCII 127 should count as well, because who would use them unless they pertained to the code?

Brian

[ 04 June 2002, 16:28: Message edited by: BrianTX ]


Howard Bullock
(KiX Supporter)
2002-06-04 04:31 PM
Re: KixGolf scoring script

Good catch. Any suggestions to correct it. If not, I'll look into it.

Questions: Should all text within quotes be counted? What characters should definitely not be counted?

Before we start using it, I want it to be sound and everyone buy off on it.

[ 04 June 2002, 16:32: Message edited by: Howard Bullock ]


Howard Bullock
(KiX Supporter)
2002-06-04 04:35 PM
Re: KixGolf scoring script

Brian, would you penalized someone for:

$A = 1 vs. $A=1
or
$A=1 ;Set var $a

because the spaces are not at the beginning of the line?

[ 04 June 2002, 16:35: Message edited by: Howard Bullock ]


BrianTX
(Korg Regular)
2002-06-04 04:41 PM
Re: KixGolf scoring script

You have a good point, but I thought the objective was NOT to maximize readability, but to minimized the size of the file. If you're talking about having a space where one need not be, then why penalize for the size of variable names, either?

Either you can optimize for execution speed (i.e. number of keywords, variables, operators), or you can optimize for size.

Just my 2 cents.

Brian

[ 04 June 2002, 16:42: Message edited by: BrianTX ]


Howard Bullock
(KiX Supporter)
2002-06-04 04:51 PM
Re: KixGolf scoring script

I guess since we have a potential scoring script that could permit some enhanced readability we could score around those additional spaces. The variable length is also a good point but that would start to complicate the scoring script. I guess we need to clarify what is really important and what the ultimate goal really is.

If we really want cryptic hard to read/understand code we could go with raw file size, but if the goal is to strive for good coding technique then we could permit some improved readability and attempt to only count applicable code.

I just want to write the solutions and had a hard time understanding how they compared to to others. This is why I wrote the scoring script. that way we all could easily calculate a score based on the same rules.

We just need to clarify what those rule will be.

[ 04 June 2002, 16:52: Message edited by: Howard Bullock ]


BrianTX
(Korg Regular)
2002-06-04 05:08 PM
Re: KixGolf scoring script

I'm with you on that! I agree that a scoring script/function is a great idea. I just think that we need to make sure that the rules regarding what syntax is counted follows common sense.

Obviously, having extra spaces where they are not needed is done strictly for readability, but some spaces are still necessary for scripts to function properly.

Examples:

code:
If $hello = "hi, how are you?" $yes = 1 Endif

Cannot be shortened to:
code:
If$hello="hi,howareyou?"$yes=1Endif

However,
code:
$a = $a + 1

can be shortened to:
code:
$a=$a+1

I suppose my point is that KiXtart code needs spaces and carriage returns in some cases and in other cases does not. As such, should these be counted?

I feel that definitely ALL characters in quotes should be counted. As for counting which spaces are necessary and which are not, that is probably not very easy to do.. that's why I suggested counting all spaces not at the beginning or end of the line. If this sounds like it is making things too complex, then it's not really a big deal. Any further thoughts?

Brian


Howard Bullock
(KiX Supporter)
2002-06-04 05:58 PM
Re: KixGolf scoring script

The Script at the top of this post has been modified to count all characters enclosed in quotes.

Sealeopard
(KiX Master)
2002-06-04 08:10 PM
Re: KixGolf scoring script

I guess I should chime in, too, since I did manually counting for the scores. Thus
code:
$a=$a+1
$a = $a + 1

both lines get the same score since the spaces are not important.
Also,
code:
IF $a=1 ? 'test' ENDIF
IF $a=1
? 'test'
ENDIF

will also result in the same score. Oh, and everything enclosed within wuotes gets counted, too.
That's why i think a GolfScore UDF is hard to implement because people write code differently. Anyway, I will try to come up with another challenge by this weekend (couldn't do anything last weekend because I trashed my home computer with a stupid mistake). I will also update the rules that nobody is allowed to post a solution within the first 24 hours except the KiXtart Golf score. In a second 24 hour period, everybody is allowed to post code and improve other people's code, too. Therefore, the fairways will get harder, too. No more par 3's.

[ 04 June 2002, 20:11: Message edited by: sealeopard ]


Howard Bullock
(KiX Supporter)
2002-06-04 08:14 PM
Re: KixGolf scoring script

Jens, did you give the KixGolf.kix a go around yet? If not, please check it out and let me know if it scores as you would.

BrianTX
(Korg Regular)
2002-06-04 09:55 PM
Re: KixGolf scoring script

I thought of another situation.. what if you have single quotes inside double quotes or the other way? How will you handle that?
(Howard, hope you don't mind.. I borrowed a little from your script...) I added enhancements including multiple quote handling, file closing, better text formatting on printed messages....

Give this script a go:
code:
$Q = 0 
$R = 0
$C = 0
if Open (1,$f) = 0
While @error=0
$L = READLINE(1)
$M = LEN($L)
For $I = 1 to $M
$A=asc(substr($L,$i,1))
IF $A = 34 AND $R = 0
$Q = $Q + 1 & 1
ENDIF
IF $A = 39 AND $Q = 0
$R = $R + 1 & 1
Endif
If $A = 59 $I = $M
Else
If $Q|$R
$C = $C + 1
Else
If $A > 32 AND $A < 127
$C = $C + 1
Endif
Endif
Endif
Next
Loop
$ = Close(1)
Else
"$F?" ?
Endif
If $Q|$R
"Mis-matched quotes. Probable score error." ?
Endif
"KixGolf score = " + $C

$f is passed on the commandline
$f=filename.kix

Brian


Howard Bullock
(KiX Supporter)
2002-06-05 01:45 AM
Re: KixGolf scoring script

Brian, I don't mind help but I do not quite understand the double quote issue that your code corrects. The currently posted code above handles quotes within quotes.

Both the current script and your script seem to yield the same results on my test files.

I wasn't concerned with the nicety of using close since file handle "1" isn't used again and would be forced closed when Kix32 exits. Formally closing the file handle is better style but I do not believe it matters in this case.

I do like your use of the "&" to keep $Q and $R to either "0" or "1". This reduced the code requirement that my empty string or ASCII value approach required. You score some points. [Smile]

[ 05 June 2002, 02:32: Message edited by: Howard Bullock ]


Radimus
(KiX Supporter)
2002-06-05 02:34 AM
Re: KixGolf scoring script

how about just manually stripping out comments
and not counting any spaces, even if in text. If you have more and a few spaces, you would probably have quite a few 'words' which would most likely push up your score anyway. 4 additional 'words' would probably have about 10-15 or more characters in it, the 3 spaces wouldn't make a big deal of difference.

The idea is efficiency... succinct(sp) output should be a requirement also


Howard Bullock
(KiX Supporter)
2002-06-05 02:46 AM
Re: KixGolf scoring script

Radimus, I think that we are just about finished with the scoring script. If we can agree on the script rules, scoring would be much faster. Did you give the scripts a try yet?

NTDOCAdministrator
(KiX Master)
2002-06-05 03:37 AM
Re: KixGolf scoring script

When can I supply CODE for you guys to clean up? [Big Grin] hehehe

ShawnAdministrator
(KiX Supporter)
2002-06-05 04:12 AM
Re: KixGolf scoring script

Well Doc, you have to be "in the game" to get your code scanned - so we'll make sure to invite you to the "Kixtart Classic Invitational Golf Tournie" this weekend (if we play). Depends on the "weather", and on "whether" Jens can get another course designed ...

DrillSergeant
(MM club member)
2002-06-05 01:21 PM
Re: KixGolf scoring script

The Sarge is jumping up and down, yelling: can I play? can I play? please, please, please !!!

[Wink] [Big Grin]

[ 05 June 2002, 13:22: Message edited by: DrillSergeant ]


ShawnAdministrator
(KiX Supporter)
2002-06-05 02:27 PM
Re: KixGolf scoring script

LOL, as long as you promise to stay - and post at least once a day ! [Wink]

Howard, Jens, I'm getting a little nervous now - all these other big guns want a piece of the action - afraid my Kixtart slice might come back to haunt me ...

-Shawn

[nice to see you back my friend - missed you !]


Sealeopard
(KiX Master)
2002-06-05 03:43 PM
Re: KixGolf scoring script

Okay, just to make it official: Despite having a dead computer at home right now (messed up the BOOT.INI during a repartitioning operation [and yes, I do have a backup ;-)]) I am already designing a new course. It will be posted on Saturday morning EST and will contain updated rules for KiXtart Golf to make it a) more challenging, b) more interesting, c) give you the chance of messing with others people's code, too.

I am also soliciting ideas for the coding challenge, it does not need to be anything practical, though.

I will also give the KiXGolf script a try.

[ 05 June 2002, 16:05: Message edited by: sealeopard ]


BrianTX
(Korg Regular)
2002-06-05 05:18 PM
Re: KixGolf scoring script

You're right, Howard. The problem with the quotes was in MY script, not yours. [Smile] I had just assumed you had to work around it as well. After counting, I think I only saved about 5 keystrokes total in the script. [Big Grin]

Brian


Howard Bullock
(KiX Supporter)
2002-06-05 05:47 PM
Re: KixGolf scoring script

We seem to concur...

KixGolf.kix is my code
KixGolf2.kix is Brian's code

C:\Data\Scripts>%tmp%\kix32 kixgolf.kix $A=KixGolf.kix

KixGolf score = 393

C:\Data\Scripts>%tmp%\kix32 kixgolf.kix $A=KixGolf2.kix

KixGolf score = 335

C:\Data\Scripts>%tmp%\kix32 kixgolf2.kix $F=KixGolf.kix

KixGolf score = 393

C:\Data\Scripts>%tmp%\kix32 kixgolf2.kix $F=KixGolf2.kix

KixGolf score = 335

[ 05 June 2002, 17:48: Message edited by: Howard Bullock ]


ShawnAdministrator
(KiX Supporter)
2002-06-05 07:54 PM
Re: KixGolf scoring script

Ok, probably too late to mention this (and may be repetitive) but I always thought what we were counting was the "non-white-space" in the script. The way I was doing that was with Microsoft Word and the "wordcount" option in the Tools menu. Specifically the Character Count reported under that. If I take Howards script, and strip-out the comments, I get a count of 385 characters. Does that make sense ? Why the descrepency ?

Sorry for bumping this to the top again - but I mean, we should sort this out because, by my reckoning, we got two scripts that report different results, yeah ?

ps. oops. is that because your are counting the whitespace inside quotes, right ?

[ 05 June 2002, 19:57: Message edited by: Shawn ]


Radimus
(KiX Supporter)
2002-06-05 07:59 PM
Re: KixGolf scoring script

Hey Jens, There a bunch of stuff on this board that isn't practical...

Why should this be any different? [Big Grin]


Howard Bullock
(KiX Supporter)
2002-06-05 08:16 PM
Re: KixGolf scoring script

Yes Shawn I have 8 white-space characters in quotes. That added to your 385 makes 393.

I am glad to see that the code was at least executed a few times. [Big Grin]

[ 05 June 2002, 20:17: Message edited by: Howard Bullock ]


Sealeopard
(KiX Master)
2002-06-05 08:24 PM
Re: KixGolf scoring script

Whitespace (actually anything) inside quotes does count. So
code:
? 'This is a text!'

should result in a score of 18 since only the space between ? and doesn't count.


Howard Bullock
(KiX Supporter)
2002-06-05 08:28 PM
Re: KixGolf scoring script

Jens, it appears that your right! [Big Grin]

C:\Data\Scripts>%tmp%\kix32 kixgolf.kix $A=junk.txt

KixGolf score = 18

where junk.txt =
code:
? 'This is a text!'  



Sealeopard
(KiX Master)
2002-06-05 08:36 PM
Re: KixGolf scoring script

Yeah, and I did this even without KiXtart [Wink]

Does that make me an old-timer?


ShawnAdministrator
(KiX Supporter)
2002-06-05 08:37 PM
Re: KixGolf scoring script

Right than ... lets hit the links ...



BrianTX
(Korg Regular)
2002-06-07 04:49 PM
Re: KixGolf scoring script

It seems pretty slow... I wonder when the KixGolf will start?

Brian


Sealeopard
(KiX Master)
2002-06-07 04:53 PM
Re: KixGolf scoring script

I already have an idea and will post the next KiXtart Golf challenge on Saturday morning EST.

kholm
(Korg Regular)
2002-07-17 10:14 PM
Re: KixGolf scoring script

Howard,

[Eek!] [Eek!] [Eek!]

Your KixGolf scoring script is reporting to few point !!!

Each CRLF must be counted as one point (nessesary whitespace)
Also it dosn't count the nessary whitespaces inside the code,
your script will count this code:
code:
If $a>5 $x=1 EndIf

As 15 points
Assuming this would be working KiX-code:
code:
If$a>5$x=1EndIf

The result should be 18, because all the whitespaces are required.

The following UDF, also counts required whitespaces, it might
stil need some minor ajusts.

I have added the possibility to count parts of a script.
If the optional parameter $CountMarkers is used and <> '' or 0 the UDF wil
look for the line:
;GolfStart
before beginning to count
And wil stop counting when the line:
;GolfEnd
is found.

code:
Function KixGolf($A,Optional $CountMarkers)
$SpaceChars=Chr(9)+' ,;=<>()+-*/"'+"'"
If Open(1,$A) = 0
$X=0
If $CountMarkers
Do
$C=ReadLine(1)
Until $C = ';GolfStart'
EndIf
$C=ReadLine(1)
While @Error=0 And $C <> ';GolfEnd'
$InStr = 0
$StrCh = ''
$j=len($C)
For $h=1 To $j
$Ch = substr($C,$h,1)
If $Ch = '"' Or $Ch = "'"
If $InStr
If $Ch = $StrCh
$InStr = 0
EndIf
Else
$InStr = 1
$StrCh = $Ch
EndIf
$X=$X+1 ; Count stringdelimiter
Else
If $InStr
$X=$X+1 ; Count all chars inside strings
Else
$D=Asc($Ch)
If $D=59 ; Ignore comments in rest of line
$h=$j
Else
If $D=9 Or $D=32 ; Space or Tab: Count only if whitespace is demanded
$Count = 1
If InStr($SpaceChars,SubStr($C,$h-1,1))
$Count=0
Else
If InStr($SpaceChars,SubStr($C,$h+1,1))
$Count=0
EndIf
EndIf
$X=$X+$Count
Else
$X=$X+1 ; Count the character
EndIf
EndIf
EndIf
EndIf
Next
$C=Trim(ReadLine(1))
If $C <> '' And Left(Trim($C),1) <> ';'
$X=$X+1 ; Each CRLF counts as one whitespace, If not a full comment-line
EndIf
Loop
$RC=Close(1)
$KixGolf=$X
Else
$KixGolf = "KixGolf, bad filename: " + $A
EndIf
EndFunction

-Erik

ps.
I belive that only expirienced 'KiXters' will use this function, so it is not commented
on how to use.
I won't release it in the UDF section because i am sure it can be improved, and maybe
some of you wil have something to add.
Also, this UDF is of no general use, so maybe this or something similar should be linked
directly to the active Golf tournament.

[ 17 July 2002, 22:19: Message edited by: kholm ]


Sealeopard
(KiX Master)
2002-07-17 10:22 PM
Re: KixGolf scoring script

'If$a>5$x=1EndIf' should be 'If $a>5 $x=1 EndIf' and is therefore KiXtart Golf score 18, indeed. Nevertheless, the @CRLF is not counted as per the rules. i am not aware of any programming Golf competition that counts the CRLF as a character.

LonkeroAdministrator
(KiX Master Guru)
2002-07-17 10:26 PM
Re: KixGolf scoring script

so, we should work around with crlf with maybe 2000000 strokes which is working...
cracking is possible you know.

would be nice to make that sized script with score of 454 [Big Grin]


kholm
(Korg Regular)
2002-07-17 10:49 PM
Re: KixGolf scoring script

Jens,

You have to count CRLF as ONE whitespace, because this code is also reported as 15 points
code:
If $a>5
$x=1
EndIf

Statements has to be separated by at least one whitespace, being:
@CRLF
Space
Tabulator

If you omit to count the @CRLF the reported size wil not be of an operational script!!

-Erik


kholm
(Korg Regular)
2002-07-18 12:59 AM
Re: KixGolf scoring script

The goal of a golf tournament should be to create the smallest WORKING KiX-script

The current rules state:
Strokes are all characters in a piece of code except whitespace characters, unless the whitespace character is necessary for the line of code to work. Therefore, carriage returns and line feeds do not count.

I my opinion this should be refrased to:
Strokes are all characters in a piece of code except whitespace characters, unless the whitespace character is necessary for the code to work.
@CRLF counts 1 if the line containes code !!!!

If the goal of the golf tournament is to find the size of the smallest possible WORKING KiX-script to accomplish the job @CRLF has to be counted as 1 point when a line containes code.

Observe that @CRLF would normally count as 2. So you do get a small discount [Wink]

-Erik

[ 18 July 2002, 01:16: Message edited by: kholm ]


Howard Bullock
(KiX Supporter)
2002-07-18 01:40 AM
Re: KixGolf scoring script

I'll let you guys argue this one out. The code I provide strips all white out and counts what left. I have not sat down to to examine all the possibilities where a white space character is or is not required. I do count all spaces within quoted strings. I think that as long as we measure with the same yard/meter stick the judging will be OK, especially when the spread seems to be 1,000 strokes. If the finalist are 1 or 2 strokes off at the end and they want to be so detailed, then they can counted anyway they want.

kholm
(Korg Regular)
2002-07-18 02:44 AM
Re: KixGolf scoring script

Howard,

I know you rest your case.

Your script counts as expected by the current rules!!

But if you use a KiX statement with 'many' reserved words like:

For Each $x In $y
Or
For $i = 1 To 10 Step 2

The score wil be unfair if you don't count in the nessasery whitespaces.

Summarizing:
Your script is flawles
BUT
I uppose to the current rules.

-Erik


Sealeopard
(KiX Master)
2002-07-18 04:01 AM
Re: KixGolf scoring script

Erik:

I get your point. I guess we then should count the @CRLF as one stroke, namely the [ENTER] key.

The added sideeffect would be that less lines of codes result in less strokes becasue of less @CRLF.

I will have this corrected in the rules.

Erik: Thanks for the discussion on it. BTW, do you participate in the current challange, there's still time to join since we won't go into the second phase until Saturday evening EST (midnight board time).