The forward sortation area code is the first three characters of a Canadian post
ID: 3792285 • Letter: T
Question
The forward sortation area code is the first three characters of a Canadian postal code (e.g., the "K1S" in postal code "K1S 5B6") and it has the format: First Character: a letter between A and Z Middle Character: a number between 0 and 9 including zero and including 9 Last Character: a letter between A and Z You will create a program to generate a subset of the possible sortation area codes. Instead of using the entire range, you must write a program using nested loops that generates all the possible codes that fit constraints specified by the user. Your program should request the following information from the user: The starting letter of the first character The ending letter of the first character The starting digit of the middle character The ending digit of the middle character The starting letter of the last character The ending letter of the last character Your program should then print out all possible combinations that fit these constraints. Each of the ranges should include both the starting and ending value, as well as all values in between. You can assume that the user will only enter valid input (i.e., upper case characters for the letters and 0-9 for the digits). You cannot use Python's lists, dictionary or set functionality to iterate over the letters. Instead, you should use the ord() function to find the integer values of the specified letters (i.e., the ASCII values) and the chr() function to translate from integers back to letters (for more information on these functions, visit After printing out each of the possible codes, your program should print out the total number of codes before ending.Explanation / Answer
Code:
def postalSortingCodes(a1,a2,b1,b2,c1,c2):
a = ord(a1); b = ord(b1); c = ord(c1); i = 0;
while (a >= ord(a1) and a <= ord(a2)):
while (b >= ord(b1) and b <= ord(b2)):
while (c >= ord(c1) and c <= ord(c2)):
i = i + 1;
print(chr(a) + chr(b) + chr(c));
c = c + 1;
b = b + 1; c = ord(c1);
a = a + 1; b = ord(b1);
print ("Total count : " + str(i));
return;
postalSortingCodes("a", "c", str(1), str(3), "a", "c");
Output :
a1a
a1b
a1c
a2a
a2b
a2c
a3a
a3b
a3c
b1a
b1b
b1c
b2a
b2b
b2c
b3a
b3b
b3c
c1a
c1b
c1c
c2a
c2b
c2c
c3a
c3b
c3c
Total count : 27
Comment: The code is self explainable just implemented your logic. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.