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.
Advertisements
In Python
from collections import defaultdict class license_plates: # assumes plates of format ABC-123 def __init__(self): self.collection = defaultdict(lambda : defaultdict(lambda : defaultdict(list))) def __str__(self): result = "List of plates in license_plate collection:" for l1 in self.collection: for l2 in self.collection[l1]: for l3 in self.collection[l1][l2]: for n in self.collection[l1][l2][l3]: result+= "\n"+ l1+l2+l3+"-"+n return result def add_plate(self, plate): if len(plate) != 7 and all(plate[i] in string.letters for i in range(3)): raise else: self.collection[plate[0]][plate[1]][plate[2]].append(plate[4:7]) def print_plates_starting_with(self, start_letters): if start_letters and len(start_letters) < 4: d = self.collection for l in start_letters: d = d[l] print("Plates starting with:", start_letters) self.pretty_print_dict(start_letters, d) else: raise def pretty_print_dict(self, prefix, d): for key in d: # print('\t' * indent + str(key)) if isinstance(d[key], dict): self.pretty_print_dict(prefix+key, d[key]) else: for n in d[key]: print(prefix + key + '-' + n) lp = license_plates() lp.add_plate('ABC-123') lp.add_plate('ABH-666') lp.add_plate('AQC-987') lp.add_plate('DEF-000') print(lp) lp.print_plates_starting_with('A') lp.print_plates_starting_with('AB') lp.print_plates_starting_with('D') lp.print_plates_starting_with('X') # output: # List of plates in license_plate collection: # DEF-000 # AQC-987 # ABH-666 # ABC-123 # Plates starting with: A # AQC-987 # ABH-666 # ABC-123 # Plates starting with: AB # ABH-666 # ABC-123 # Plates starting with: D # DEF-000 # Plates starting with: X # [Finished in 0.2s]MUMPS V1
LICPLATE ;New routine N CT,I,PLATES ; Add plates to array ; F I="PLB-123","PLB-666","PLB-987","PLC-000" D . S PLATES(I)="" ; Is PLB-123 member of the array? W !,"PLB is ",$S($D(PLATES("PLB-123")):"",1:"not"),"a member of the array." ; ; How many license plates begin with 'PLB'? ; S I=$O(PLATES("PLB"),-1) ; Start immediately before entries starting with 'PLB' F CT=0:1 S I=$O(PLATES(I)) Q:I'?1"PLB".E W !!,CT," plate",$S(CT'=1:"s",1:"")," begin with 'PLB'." ; ; What is the list of license plates that begin with the 'PLB' ; W !!,"List of license plates that begin with 'PLB':" S I=$O(PLATES("PLB"),-1) ; Start immediately before entries starting with 'PLB' F S I=$O(PLATES(I)) Q:I'?1"PLB".E W !,I Q --- D ^LICPLATE PLB is a member of the array. 3 plates begin with 'PLB'. List of license plates that begin with 'PLB': PLB-123 PLB-666 PLB-987MUMPS V1
Correction LICPLATE ;New routine N CT,I,PLATES ; Add plates to array ; F I="PLB-123","PLB-666","PLB-987","PLC-000" D . S PLATES(I)="" ; Is PLB-123 member of the array? W !,"PLB-123 is ",$S($D(PLATES("PLB-123")):"",1:"not"),"a member of the array." ; ; How many license plates begin with 'PLB'? ; S I=$O(PLATES("PLB"),-1) ; Start immediately before entries starting with 'PLB' F CT=0:1 S I=$O(PLATES(I)) Q:I'?1"PLB".E W !!,CT," plate",$S(CT'=1:"s",1:"")," begin with 'PLB'." ; ; What is the list of license plates that begin with the 'PLB' ; W !!,"List of license plates that begin with 'PLB':" S I=$O(PLATES("PLB"),-1) ; Start immediately before entries starting with 'PLB' F S I=$O(PLATES(I)) Q:I'?1"PLB".E W !,I Q --- D ^LICPLATE PLB is a member of the array. 3 plates begin with 'PLB'. List of license plates that begin with 'PLB': PLB-123 PLB-666 PLB-987Using SQLite.
import sqlite3 as db import random import string def create_random_plates(): 'fill the database with some license plates' with db.connect("license2.db") as DB: cur = DB.cursor() cur.execute('CREATE TABLE LicensePlates(Id INT, Chr TEXT)') for i in range(100000): c = "".join(random.sample(string.ascii_uppercase, 3)) n = "".join(random.sample(string.digits, 3)) cur.execute("""INSERT INTO LicensePlates Values({}, "{}-{}")""".format(i+1, c, n)) def find_pattern(pattern): 'pattern for SQL LIKE' with db.connect("license2.db") as DB: cur = DB.cursor() cur.execute("""SELECT * FROM LicensePlates WHERE Chr LIKE '{}' ORDER BY Chr""".format(pattern)) return cur.fetchall() create_random_plates() # find all plates starting with PLB and with 5 in middle of number for plate in find_pattern("PLB-_5_"): print(plate[1]) """ PLB-451 PLB-652 PLB-756 PLB-956 """