Qbasicnews.com
September 26, 2020, 03:24:01 AM
 Pages: 1 2 3 [4] 5
 Author Topic: Challenge: Validate a code  (Read 18891 times)
Agamemnus
x/ \z

Posts: 3491

 « Reply #45 on: July 29, 2003, 02:41:04 PM »

:|
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Agamemnus
x/ \z

Posts: 3491

 « Reply #46 on: July 29, 2003, 03:16:06 PM »

My entry. Uses longs! I know, BAD!

Code:

'Is this BLITZTASTIC OR WHAT??!?!?

DECLARE FUNCTION validate.all% (file1\$)
DECLARE FUNCTION num.exists% (n&)
DIM SHARED num.max AS LONG, num.max2 AS LONG: num.max = 100000: num.max2 = num.max \ 16
DIM SHARED huge.array(1 TO num.max2) AS LONG
DIM SHARED bit2(0 TO 15) AS LONG
DIM SHARED num.amount AS INTEGER: num.amount = 20000
FOR I% = 0 TO 15: bit2(I%) = 2 ^ I%: NEXT I%

CLS

'OPEN "valid.txt" FOR OUTPUT AS #1
'FOR I% = 1 TO 20000
'a\$ = LTRIM\$(STR\$(INT(RND * 100000)))
'PRINT #1, a\$
'NEXT I%
'CLOSE
'SYSTEM

t1# = TIMER
PRINT validate.all%("valid.txt")
t2# = TIMER: PRINT "Time for"; num.amount; "values to be processed:"; t2# - t1#; "seconds."

'use num.exists%(n&) for individual numbers..
DO
INPUT n&
IF n& > 99999 THEN PRINT "Number bigger than 99999..."
IF n& < 0 THEN PRINT "Number smaller than 0..."
IF num.exists%(n&) THEN PRINT "Number exists!" ELSE PRINT "Number doesn't exist.."
LOOP

enter.char\$ = CHR\$(13): a1\$ = " ": a2\$ = "  ": num1\$ = "": num2% = 0
OPEN file1\$ FOR BINARY AS #1
j% = 1: FOR I% = 1 TO num.amount
num1\$ = ""
GET #1, , a1\$: IF a1\$ = enter.char\$ THEN GOTO v2
IF INSTR("0123456789", a1\$) = 0 THEN GET #1, , a1\$: GOTO v2
FOR j% = 1 TO 4
num1\$ = num1\$ + a1\$
GET #1, , a1\$: IF a1\$ = enter.char\$ THEN GOTO v1
IF INSTR("0123456789", a1\$) = 0 THEN GET #1, , a1\$: GOTO v2
NEXT j%
num1\$ = num1\$ + a1\$
GET #1, , a1\$
v1: IF a1\$ <> enter.char\$ THEN GET #1, , a1\$: GOTO v2
num2& = VAL(num1\$)
k% = num2& MOD 16
j% = num2& \ 16 + 1
temp& = huge.array(j%)
IF (temp& AND bit2(k%)) = 0 THEN huge.array(j%) = temp& + bit2(k%)
v2: GET #1, , a1\$
NEXT I%
CLOSE
END SUB

FUNCTION num.exists% (n&)
k% = n& MOD 16
j% = n& \ 16 + 1
IF (huge.array(j%) AND bit2(k%)) THEN num.exists% = -1
END FUNCTION

FUNCTION validate.all% (file1\$)
is.valid% = -1
enter.char\$ = CHR\$(13): a1\$ = " ": a2\$ = "  ": num1\$ = "": num2% = 0
OPEN file1\$ FOR BINARY AS #1
j% = 1: FOR I% = 1 TO num.amount
num1\$ = ""
GET #1, , a1\$: IF a1\$ = enter.char\$ THEN GOTO v2b
IF INSTR("0123456789", a1\$) = 0 THEN GET #1, , a1\$: GOTO v2b
FOR j% = 1 TO 4
num1\$ = num1\$ + a1\$
GET #1, , a1\$: IF a1\$ = enter.char\$ THEN GOTO v1b
IF INSTR("0123456789", a1\$) = 0 THEN GET #1, , a1\$: GOTO v2b
NEXT j%
num1\$ = num1\$ + a1\$
GET #1, , a1\$
v1b: IF a1\$ <> enter.char\$ THEN GET #1, , a1\$: GOTO v2b
num2& = VAL(num1\$)
k% = num2& MOD 16
j% = num2& \ 16 + 1
temp& = huge.array(j%)
IF (temp& AND bit2(k%)) = 0 THEN is.valid% = 0
v2b: GET #1, , a1\$
NEXT I%
CLOSE
validate.all% = is.valid%
END FUNCTION
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Moneo
Na_th_an

Posts: 1971

 « Reply #47 on: July 29, 2003, 03:33:53 PM »

Aga, Congratulations, it works!
However, it looks a bit complicated.

One small observation. On user input it allows leading, embedded, and trailing blanks on the numbers. Not exactly "clean", but that's my opinion.
*****
 Logged
Agamemnus
x/ \z

Posts: 3491

 « Reply #48 on: July 29, 2003, 03:37:24 PM »

Arr! I thought we already discussed that it removes all of that? aRr!!!!
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Moneo
Na_th_an

Posts: 1971

 « Reply #49 on: July 29, 2003, 03:53:08 PM »

CODE VALIDATION CHALLENGE FINAL RESULTS:

First Place: MEG.  Brilliant solution and concise implementation.

Tied for Second: ANTONI and BLITZ. Excellent solutions and implementations.

Third: AGA. Very good, working solution.

Fourth: STERLING. Participated.

*****
 Logged
Blitz
I hold this place together

Posts: 853

 « Reply #50 on: July 29, 2003, 03:53:50 PM »

Jesus christ, that code will give me nightmares for weeks....

And reading from video ram is around 100 times slower then sys ram. And writing to it is around 10 times slower then sys ram. It's faster then files, but a huge array would have been faster. So if it was performance you were after...

Why do i get the feeling that meg won cuz she's the only girl here?
 Logged

oship me and i will give you lots of guurrls and beeea
Agamemnus
x/ \z

Posts: 3491

 « Reply #51 on: July 29, 2003, 04:06:56 PM »

Quote from: "Moneo"

Write the program in the most efficient manner possible, WITHOUT USING ANY ADDITIONAL FILES.

Efficiency is equal to SPEED, not source code length, as Moneo thinks..

BTW, Mr. Blitz, could you compare yours to mine...? Mine reads in 20,000 numbers, then reads them in again and searches for them. Also checks that they are numbers and proper format.

I could do this myself, but you see, if I keep looking at YOUR code (50% whitespace..), *I* might have nightmares for weeks..  :bounce:
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Moneo
Na_th_an

Posts: 1971

 « Reply #52 on: July 29, 2003, 04:10:55 PM »

Blitz,
Speed was not an issue here. Speed is seldom an issue when a program has a user interface. The user can't telll the difference  waiting for 100ms or 200ms between his input and a response from the program.

I like simplicity. Meg's program is 39% the size of yours, and accomplishes the same task.
*****
 Logged
Agamemnus
x/ \z

Posts: 3491

 « Reply #53 on: July 29, 2003, 04:13:25 PM »

Moneo, moneo..

My validation program is 3 lines of code:

Code:

FUNCTION num.exists% (n&)
k% = n& MOD 16
j% = n& \ 16 + 1
IF (huge.array(j%) AND bit2(k%)) THEN num.exists% = -1
END FUNCTION
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Moneo
Na_th_an

Posts: 1971

 « Reply #54 on: July 29, 2003, 04:17:29 PM »

Aga, are you kidding me?
That's the validation function. You entire validation program takes 90 lines of code.
*****
 Logged
Agamemnus
x/ \z

Posts: 3491

 « Reply #55 on: July 29, 2003, 04:24:01 PM »

Er, I meant function.

Realize that I have three functions. One to test an entire file of numbers, one to load a file of numbers, one to test one number, and some code for user input.

Also realize that writing to the screen takes much more moving-around- the-computer-code (SPEED) than writing to an array.

So, if you ignore what's inside the functions, the actual program is only 3 lines, too.
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Blitz
I hold this place together

Posts: 853

 « Reply #56 on: July 29, 2003, 04:44:20 PM »

Come on man, i don't care who wins. I never even enter these callenges becuase of the people who are supose to judge it. What annoys me is that you don't actually seem to understand any of the code. For instance, if it's the shortest code you wanted you would have seen that my first one was basically this. When you remove all the dims, functions, consts, defint, comments. All the things that make clear code. You would have gotten this. Which is quite a bit smaller then megs when you count actual lines of code. So i don't think you're qualified to judge this. In my opnion Antoni should have won. Even though it was basically my code replaced with a bit array. A bit array didn't come to my mind at first. So i think he should have won. Although i would have given him less points for coding style. He would still be the winner.

Code:

'\$dynamic
dim shared keyTable( 7, 16383 ) as integer

open "valid.txt" for input a #1
while ( not eof( 1 ) )
input #1, currKey&
keyTable( currKey& \ 16384& , currKey& and 16383&  ) = -1
wend
close #1

do
input "Enter a key or -1 to exit: ", keyToCheck&

if ( keyTable( keyToCheck& \ 16384& , keyToCheck& and 16383& ) = 0 ) then
print "Invalid key, quiting"
exit do
else
print "Valid key"
end if
loop
 Logged

oship me and i will give you lots of guurrls and beeea
Antoni Gual
Na_th_an

Posts: 1434

 « Reply #57 on: July 29, 2003, 05:11:05 PM »

Aga: Using longs is not bad, only dangerous: there are some entries at Microsoft Knowledge Base about QB45 bugs related to  longs, in particular as parameters to SUBS and FUNCTIONS.

Blitz: When I die my style of coding will be taught in schools  :rotfl:
 Logged

Antoni
Moneo
Na_th_an

Posts: 1971

 « Reply #58 on: July 29, 2003, 07:33:48 PM »

Blitz,
Your late entry is a beautiful piece of tight code.
However, I had the following little problems:
2) I discovered that I needed to compile with /ah switch.
3) It's supposed to exit on -1. However, it says that it's valid.
4) So, I tried some other negative numbers, like -99999 which caused a "subscript out of range" error.
5) You also allow leading, embedded and trailing blanks on the input numbers, which is not good practice. If I entered "1 3" but really meant "123" you accept the input as "13".
6) I also tried some numbers larger than 99999, like 222222, which caused the "subscript out of range" error.

With a few more minutes of work, your last entry could have been a serious contenter. However, we closed off the challenge earlier.

As far as judging is concerned, we don't have any other alternate way of judging challenges. Maybe what I've been doing is not the best way of judging, that is, having the person who posted the challenge be the judge, but it's been working up to now for my posts. Any suggestions?

Regarding my not understanding all the code, it's tough enough to get the programs to compile and then to test them thoroughly. I take a fast look at them to get a fundamental idea of how they work, but without comments of any kind, it's extremely hard to really comprhend the general algorithms of the programs and the logic of their respective functions. If you were test-driving a car, you'd start it up, put in gear and drive around a bit. You woudn't take the engine and transmission apart to see how everything works.
*****
 Logged
Blitz
I hold this place together

Posts: 853

 « Reply #59 on: July 29, 2003, 09:03:21 PM »

Oh yeah, forgot the if ( (keyToCheck& < 0) or ( keyToCheck > 99999) ) then exit do
 Logged

oship me and i will give you lots of guurrls and beeea
 Pages: 1 2 3 [4] 5