Re: I need a simple hangman program
- Posted by Hawke <mdeland at NWINFO.NET> Oct 18, 1998
- 565 views
Lucius Hilley wrote: >I am Lucius Hilley III yes, you've done very good work in the past... recognized the name right off, as someone not to be reckoned with :) >I formerly welcome your expertise and enthusiasm. thankee! >As for Nate. 'nuff said. > [Hangman function] > I have pasted your function below. It can use find(). actually, technically, it can indeed use find(). your usage of find() is one of two I thought of that could use find() and solve the problem. (the other solution of find() slices wrd instead of incrementing the index and passes smaller and smaller slices of wrd to find() ) so in that context, and taking my function comments literally, i rather misspoke myself when writing those comments. i meant (and should have said) that you cannot use find() efficiently, easily and neatly. remembering that this hangman program was written to assist 'newbies' to euphoria (one in particular), i tried many many different sets of code for almost every function in the program. the ones that won and were included into that hangman program were the ones that strongly met 2 conditions; namely:speed and ease of reading/understanding. why speed? naturally, you wouldn't want to show a newcomer sloppy, inefficient code if you could help it... starts them off with bad habits, dontcha think? the other condition i think is self-evident considering the context and goal of the program (being a teaching aid). your routine works, and I'm glad you spotted that comment and how it was phrased. whilst teaching, precision is paramount, and you receive my official kudo of 'good eyes'. :) > BTW, I never bothered timing the two for speed. i had, prior to the posting of the function/program... >My guess is..find() will..be slower in..short cases. >But if..used in large sequences..then..find()..would prevail. unfortunately, your guess as to speed using find() is rather incorrect, which is why the function i put in the program 'won'. (i also think it's easier to understand) i present the following benchmark program and results, and if you see a grievous error in my testing, please, by all means, inform me and I/we shall remedy it tootsweet. :) ----begin code include machine.e tick_rate(1000) constant True=1 constant Blank="\r" & repeat(' ',70) & "\r" sequence Word,GuessArray atom now,key object junk function UpdateHawke(sequence garr,atom key,sequence wrd) --my original function, sans comments for i=1 to length(wrd) do if wrd[i]=key then garr[i]=True end if end for return garr end function function UpdateLucius(sequence garr,atom key,sequence wrd) --lucius' function posted to the listserv, sans comments integer i, last_i, len i = 0 len = length(wrd) last_i = find(key, wrd) while last_i do -- while i != 0 do -- might still be a faster, Robert? i = i + last_i garr[i] = True last_i = find(key, wrd[i + 1..len]) end while return garr end function procedure Pause() puts(1,"\nPress a key...") while get_key()=-1 do end while puts(1,Blank) --erase Press a key... end procedure function RandLetter() return rand(26) + 'a' end function procedure benchmark(integer size) --init key=RandLetter() puts(1,"Building sequences...") Word = repeat(0,size) GuessArray = repeat(0,size) for z=1 to size do Word[z] = RandLetter() end for puts(1,Blank) --erase Building sequences... --benchmark printf(1,"%9d ",size) junk = {} now=time() junk=UpdateHawke(GuessArray,key,Word) now = time() - now printf(1,"%10.6f",now) junk = {} now=time() junk=UpdateLucius(GuessArray,key,Word) now = time() - now printf(1," %10.6f",now) Pause() end procedure puts(1, "Benchmark size" & " " & "UpdateHawke" & " " & "UpdateLucius\n") puts(1,repeat('-',50) & "\n") for z=5000 to 100000 by 5000 do benchmark(z) end for ---------- end code results on my machine:p200mmx,48Mram Benchmark size UpdateHawke UpdateLucius ------------------------------------------------- 5000 0.002000 0.050992 10000 0.003000 0.191971 15000 0.004999 0.467929 20000 0.006999 0.919860 25000 0.009998 1.404786 30000 0.011998 1.922707 35000 0.011998 2.922555 40000 0.014998 3.652444 45000 0.015998 4.733279 50000 0.018997 5.407177 55000 0.020997 7.004933 60000 0.021997 8.616688 65000 0.023996 9.984480 70000 0.028996 11.797204 75000 0.027996 14.100853 80000 0.030995 15.880582 85000 0.032995 18.426194 90000 0.034995 20.191925 95000 0.036994 22.964503 100000 0.037994 25.959047 i actually went to a size of a million... UpdateHawke was less than a second!!! UpdateLucius... well, I went to smoke a cig, came back over *ten minutes* later and it still hadn't posted a result... I gave up waiting... :) take care--Hawke'