Full Version: Challenge: Validate a code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7
I won this challenge.
Both your solutions assumed that the user was only going to enter one code per each running of the program. Actually the whole idea of the challenge is to store the 20,000 valid codes into memory somehow, and then validate each code entered by the user. Sorry if I wasn't more explicit. Blitz and Antoni did assume the user would enter multiple codes. Please enhance your solutions to take this into consideration.

I'm in the process of compiling and testing your solutions.

Regarding your questions about "why all the challenges", no I'm not a teacher nor a student. I just enjoy challenges. You get to learn different ways of doing things from other people.
Moneo, the one i did assumes a binary file. Just so you know.
Here's the results of tests on BLITZ and ANTONI programs.

P.S.: I created the VALID.TXT file with 20,000 records, and each record containing a 5 byte code number, with leading zeros.

1) You had a line up front with OPTIONS EXPLICIT. I had to comment this out since QuickBasic does not like it.
2) You used filename VALID.DAT instead of VALID.TXT. I fixed it.
3) Program compiles, but issues a "Subscript out of range" error on the line:
loadKeyTable "valid.txt"

1) You used filename VALIDATE.TXT. I changed to VALID.TXT.
2) Program compiles, but issues a "Subscript out of range" error on the line:
3) You have a comment about loading QB with /ah. Should I compile your program with any special switches?

Any type of file there is can be treated like a binary file and read as a binary file accordingly.

However, the file VALID.TXT is in fact a text file with 20,000 records. Each record has 5 numeric bytes for each of the codes.
You can go ahead and read it in binary if you like, but it's just a lot more work. However, reading it as a binary file is faster because you can read in a 16k chunk at a time and then deblock the records in memory. Most utility programs read text files in binary this way to speed up the access.
Sure, you can treat a transvestite as a real women too. But that doesn't mean it is :p

My code reads binary files where each 4 bytes represents a long and the next 4 bytes another one etc etc. However, i with little modification it works with ASCII files as well. So here you go. Oh and i thought it was obvious that you have to use the /ah option. Well if it wasn't, now you know.

'' If i had to chose i'd use a number in the range
'' 0 to 32767 for effciency. If the number was
'' bigger then that i wouldn't use qb.
'' Use the /ah option or you won't be able to compile
'' or run.
defint a-z

const KEYMIN&   = 0&
const KEYMAX&   = 99999&
const VALIDKEY  = -1

declare sub loadKeyTable   ( filename as string )
declare function checkKey% ( keyToCheck as long )

dim shared keyTable( KEYMAX \ 16384&, 16383 ) as integer

    '' Entry point
    dim keyToCheck as long    
    '' This is not part of the main program
    '' it's just setup.
    loadKeyTable "valid.txt"
        input "Enter a key or -1 to exit: ", keyToCheck
        if ( checkKey( keyToCheck ) = 0 ) then
            print "Invalid key, quiting"
            exit do
            print "Valid key"
        end if

'' :::::::::
'' name: checkKey
'' desc: Checks if a key is valid
'' :::::::::
defint a-z
function checkKey% ( keyToCheck as long ) static
    dim indxa as integer
    dim indxb as integer    
    '' Check range
    if ( (keyToCheck < KEYMIN) or (keyToCheck > KEYMAX) ) then
        checkKey% = 0
        exit function
    end if
    '' Check key
    indxa = keyToCheck  \  16384&
    indxb = keyToCheck and 16383&
    checkKey% = keyTable( indxa, indxb )
end function

'' :::::::::
'' name: loadKeyTable
'' desc: Loads a bunch of valid keys
'' note: This is to be run BEFORE starting to time
'' :::::::::
defint a-z
sub loadKeyTable ( filename as string ) static
    dim currKey as long
    dim keysRead as long
    dim keysInFile as long
    dim i as integer, j as integer
    dim indxa as integer, indxb as integer
    open filename for input as #1    
    '' Clear table and load keys
    for  i = 0 to KEYMAX \ 16384&
        for  j = 0 to 16383
            keyTable( i, j ) = 0
        next j
    next i    

    while ( not eof( 1 ) )
        input #1, currKey
        if ( (currKey < KEYMIN) or (currKey > KEYMAX) ) then
            print "Error: Invalid key in file..."
        end if
        '' Put key in table
        indxa = currKey  \  16384&
        indxb = currKey and 16383&
        keyTable( indxa, indxb ) = VALIDKEY
    close #1
end sub
Got the same "subscript out of range" error in the same place as before.
P.S.: I'm compiling using BC without any switches. Is that ok?

BTW: I don't understand why you set up a two dimensional array, and why you use the values 16384 and 16383. What are you trying to do?


The idea is not to loop and read in more values. The idea is to read in all the valid codes ONCE, and store them in memory to your liking (and obviously they have to fit). Then for each code that the user inputs, validate against what you have in memory.

Come on, the hard part is stuffing the codes into as little memory as possible. That's the whole idea of the challenge!
Ok listen, both my entry and antonis need to be compiled with the /Ah option. If you don't compile it with that option you will get subscript out of range.
Pages: 1 2 3 4 5 6 7