I'm currently a 3rd year computer science student and lecturers have never checked the readability and maintainability of code. We were only marked on our outputs and as such I've written some truly horrible looking code in the past. I'm trying to work on that and one thing I've learned is that it's a good idea to put things in functions to make testing and maintaining code easier.
I completed the following question:
Write the function that parses the mileage number input, and returns a
2
if the number is "interesting" (see below), a1
if an interesting number occurs within the next two miles, or a0
if the number is not interesting."Interesting" Numbers
Interesting numbers are 3-or-more digit numbers that meet one or more of the following criteria:
- Any digit followed by all zeros:
100
,90000
- Every digit is the same number:
1111
- The digits are sequential, incementing†:
1234
- The digits are sequential, decrementing‡:
4321
- The digits are a palindrome:
1221
or73837
- The digits match one of the values in the
awesome_phrases
array† For incrementing sequences,
0
should come after9
, and not before1
, as in7890
.
‡ For decrementing sequences,0
should come after1
, and not before9
, as in3210
.
My code is:
def number_followed_by_zeroes(number):
#divides a number until there are no more trailing zeroes
while(number%10 == 0):
number = number/10
if(number>10):
return False
return True
def repeating_digit(number):
#stores all digits in a set(duplicates not allowed in sets)
#converts to list of digits then stores in set
unique_digits = set( list( str( number ) ) )
#All digits were the same
if(len(unique_digits) == 1):
return True
return False
def increasing_sequential(number):
#store all digits in a list
digits = list( str( number ) )
#Sorted function doesn't alter original list
sorted_list = sorted(digits)
#While loop ensures all zeros are at the end of sorted list
#As specified by question
zero_edge = sorted_list[0]
while(zero_edge == "0"):
sorted_list.append(sorted_list[0])
del sorted_list[0]
zero_edge = sorted_list[0]
#Make sure it's incrementally increasing
#consec_num stands for consecutive_number
consec_num = int(sorted_list[0])
for i in sorted_list:
if consec_num != int(i):
return False
consec_num += 1
if i == "9":
consec_num = 0
#if sorted and original list are the same then
#number was sorted
if(sorted_list == digits):
return True
return False
def decreasing_sequential(numbers):
#similar to increasing_sequential but in reverse
#No need to alter normal sorting in regards to zero
digits = list( str( numbers ) )
sorted_list = sorted( digits )
#Make sure it's incrementally increasing
#consec_num stands for consecutive_number
consec_num = int(sorted_list[0])
for i in sorted_list:
if consec_num != int(i):
return False
consec_num += 1
sorted_list.reverse()
if(sorted_list == digits):
return True
return False
def palindrome(numbers):
digits = list( str( numbers ) )
#Splits the digits into half storing each in their own list
first_half = digits[0:(int(len(digits) /2))]
second_half = digits[int(len(digits)/2):]
#Runs if there were an odd number of digits, deletes the middle number
if(len(first_half) != len(second_half)):
del second_half[0]
#Since a second half of string would be reverse copy of first in a palindrome
second_half.reverse()
if(second_half == first_half):
return True
return False
def is_interesting(number, awesome_phrases):
if(number<98):
return 0
if number in awesome_phrases:
return 2
#if the original number is awesome
if(number > 99 and (number_followed_by_zeroes(number) or repeating_digit(number) or
increasing_sequential(number) or decreasing_sequential(number) or
palindrome(number)) ):
return 2
#if the next 2 numbers are awesome
for i in range(1,3):
number += 1
if number in awesome_phrases:
return 1
if(number_followed_by_zeroes(number) or repeating_digit(number) or
increasing_sequential(number) or decreasing_sequential(number) or
palindrome(number) ):
return 1
return 0
pass
Would I be fine if I coded this at a business or have I created too many functions or taken too rudimentary approaches to each function?
One thing I did consider is to add another function to make the number is above 99
def great_enough(number):
if number>99:
return True