|
23 | 23 | 5. assume it can be more restrictive but one can have a extra check where the
|
24 | 24 | postal code is always 6 digits long as it is always for Banglore location.
|
25 | 25 |
|
26 |
| -
|
| 26 | +6. Final assumption is that assertions will always be turned on while using also |
27 | 27 | ########################################################################
|
28 | 28 | approach to the problem
|
29 | 29 |
|
@@ -65,8 +65,8 @@ def write_outfile(ranks, outputfile_name="topthree.txt"):
|
65 | 65 | 'No of Parcels Delivered', '\n'))
|
66 | 66 | for pin_code, no_delivery in ranks:
|
67 | 67 | count += 1
|
68 |
| -opfile.write(template.format(str(count)+".", pin_code, str(count), |
69 |
| - str(no_delivery), '\n')) |
| 68 | +opfile.write(template.format(str(count) + ".", |
| 69 | +pin_code, str(count), str(no_delivery), '\n')) |
70 | 70 | except IOError:
|
71 | 71 | return "IO failed"
|
72 | 72 | except AssertionError:
|
@@ -90,12 +90,15 @@ def ranksetter(ranklist, no_delivery, postalcode):
|
90 | 90 | ranksetter([('456098', 3), ('567789', 2), ('560089', 1)], 5, "234561")
|
91 | 91 | """
|
92 | 92 | try:
|
| 93 | +#input type assertion checks |
93 | 94 | assert(isinstance(ranklist, list))
|
94 | 95 | assert(isinstance(no_delivery, int))
|
95 | 96 | assert(isinstance(postalcode, str))
|
96 | 97 | assert(postalcode.isdigit())
|
| 98 | +#allowd length checks |
97 | 99 | assert(len(ranklist) == 3)
|
98 | 100 | assert(len(postalcode) == 6)
|
| 101 | +#delivery must be non negative and integral |
99 | 102 | assert(no_delivery >= 0)
|
100 | 103 | check_list = [val[1] for val in ranklist]
|
101 | 104 | exp_list = check_list[:]
|
@@ -147,7 +150,7 @@ def matcher_and_extractor(constraint_string, input_sub_string,
|
147 | 150 | assert(input_sub_string[constraint_length].isdigit())
|
148 | 151 | # is the postal code what we think is digits only and note always we
|
149 | 152 | #consider it only 6 digit length
|
150 |
| -postal_code = input_sub_string[constraint_length: constraint_length+6] |
| 153 | +postal_code = input_sub_string[constraint_length: constraint_length + 6] |
151 | 154 | assert(postal_code.isdigit())
|
152 | 155 | return postal_code
|
153 | 156 | except AssertionError:
|
@@ -187,7 +190,6 @@ def toppostalcodes(input_filename):
|
187 | 190 | for p_code in postalcodedict:
|
188 | 191 | ranker = ranksetter(ranker, postalcodedict[p_code], p_code)
|
189 | 192 | assert(ranker is not None)
|
190 |
| -print ranker |
191 | 193 | return write_outfile(ranker)
|
192 | 194 | except AssertionError:
|
193 | 195 | return
|
|
0 commit comments