Number plate¶
We represent a car registration as a string with four digits followed by a whitespace character and three consonant capital letters.
Save all functions into the same file named numberplate.py.
Write the function
pos_letter()that takes a number plate (string) and another string with one character, and returns the first position of this character within the three letters group of the number plate. If this character is different from all three letters then the function will return -1. Examples:>>> pos_letter('7732 CHW','A') -1 >>> pos_letter('7216 ABS','S') 2 >>> pos_letter('8717 DBW','B') 1
Note
More tests are provided in the
test-numberplate1.txtfile.Write the function
only_digits()that takes a number plate (string), and returnsTrueif the first four characters of the number plate are digits andFalseotherwise. Examples:>>> only_digits('7732 CHW') True >>> only_digits('7FG2 CHW') False >>> only_digits('7652 12W') True >>> only_digits('7652BCD') True
Note
More tests are provided in the
test-numberplate2.txtfile.Write function
is_upper_consonant()that given a string with one character returnsTrueif this character is a uppercase consonant letter andFalseotherwise. Examples:>>> is_upper_consonant('B') True >>> is_upper_consonant('H') True >>> is_upper_consonant('A') False >>> is_upper_consonant('b') False
Note
More tests are provided in the
test-numberplate5.txtfile.Write the function
only_consonants()that takes a number plate (string), and returnsTrueif the last three characters of the number plate are consonant capital letters andFalseotherwise. One way of designing this function is by using functionis_upper_consonant(). Example:>>> only_consonants('7216 XBS') True >>> only_consonants('7TG4 TNT') True >>> only_consonants('7216 ABU') False >>> only_consonants('7216 TnT') False >>> only_consonants('7216 GH6') False >>> only_consonants('7652BCD') True
Note
More tests are provided in the
test-numberplate3.txtfile.Write the function
correct_numberplate()that takes a number plate (string), and returnsTrueif the number plate is correct (see definition at the beginning) andFalseotherwise. This function must call some of the previous functions.See the following examples:
>>> correct_numberplate('7732 CHW') True >>> correct_numberplate('7FG2 CHW') False >>> correct_numberplate('7216 TBS') True >>> correct_numberplate('7216 BB3') False >>> correct_numberplate('7276 OEH') False >>> correct_numberplate('7276 tnt') False >>> correct_numberplate('7276BCD') False
Note
More tests are provided in the
test-numberplate4.txtfile.
Solutions
A solution of these functions is provided in the
numberplate.py file.