License Plates
May 16, 2017
A license plate number has form ABC-123, three letters followed by three digits. You are to store the set of license plate numbers, assume you will have about a hundred thousand of them, and be able to answer queries like:
* Is license plate PLB-123 a member of the set?
* How many license plates begin with the letters PLB?
* What is the list of license plates that begin with the letters PLB?
Your task is to write programs to store and query a list of license plate numbers. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
In Python
MUMPS V1
MUMPS V1
Using SQLite.
I used this shell program to generate a list of license plate numbers:
#!/bin/bash
NUM_TIMES=100000
for ((i == 0 ; i < NUM_TIMES ; i++)); do
HEAD=$(head -c 3 < /dev/urandom | tr --complement 'A-Z' 'A-ZA-ZA-ZA-ZA-ZA-ZA-ZA-Z')
TAIL=$(head -c 3 < /dev/urandom | tr --complement '0-9' '0-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-9')
echo $HEAD-$TAIL
done
Then I piped it through
sort
anduniq
to make a list with no duplicates intocleaned.txt
.Then I can use this shell program to query in the 3 ways required (membership, prefix count, and prefix listing):
#!/bin/bash
case "$1" in
(member) # membership query #
<cleaned.txt grep --silent '^'"$2"'$'
if test "$?" -eq 0
then
echo "'$2' is a member of this set."
else
echo "'$2' is not a member of this set."
exit 1
fi
;;
(prefix) # prefix count #
COUNT=$(<cleaned.txt grep --count '^'"$2")
if test "$COUNT" -eq 0
then
echo "No license plates begin with '$2'."
exit 1
elif test "$COUNT" -eq 1
then
echo "1 license plate begins with '$2'."
else
echo "$COUNT license plates begin with '$2'."
fi
;;
(list) # prefix list #
<cleaned.txt grep '^'"$2"
if test "$?" -ne 0
then
echo "No license plates begin with '$2'."
exit 1
fi
;;
(*)
echo "unknown command - use 'member', 'prefix', or 'list'."
exit 1
;;
esac
edit of above comment:
#!/bin/bash
NUM_TIMES=100000
for ((i == 0 ; i < NUM_TIMES ; i++)); do
HEAD=$(head -c 3 < /dev/urandom | tr --complement 'A-Z' 'A-ZA-ZA-ZA-ZA-ZA-ZA-ZA-Z')
TAIL=$(head -c 3 < /dev/urandom | tr --complement '0-9' '0-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-9')
echo $HEAD-$TAIL
done
—
#!/bin/bash
case "$1" in
(member) # membership query #
<cleaned.txt grep --silent '^'"$2"'$'
if test "$?" -eq 0
then
echo "'$2' is a member of this set."
else
echo "'$2' is not a member of this set."
exit 1
fi
;;
(prefix) # prefix count #
COUNT=$(<cleaned.txt grep --count '^'"$2")
if test "$COUNT" -eq 0
then
echo "No license plates begin with '$2'."
exit 1
elif test "$COUNT" -eq 1
then
echo "1 license plate begins with '$2'."
else
echo "$COUNT license plates begin with '$2'."
fi
;;
(list) # prefix list #
<cleaned.txt grep '^'"$2"
if test "$?" -ne 0
then
echo "No license plates begin with '$2'."
exit 1
fi
;;
(*)
>&2 echo "unknown command - use 'member', 'prefix', or 'list'."
exit 1
;;
esac