patternpythonMajor
Producing ordinal numbers
Viewed 0 times
numbersproducingordinal
Problem
Is it possible to write this in fewer lines of code?
If you input an integer it will output it as an ordinal number if it is less than 100. The below code works perfectly, but I'm wondering if it could be written more succinctly.
If you input an integer it will output it as an ordinal number if it is less than 100. The below code works perfectly, but I'm wondering if it could be written more succinctly.
def ordinal(self, num):
"""
Returns ordinal number string from int, e.g. 1, 2, 3 becomes 1st, 2nd, 3rd, etc.
"""
self.num = num
n = int(self.num)
if 4 <= n <= 20:
suffix = 'th'
elif n == 1 or (n % 10) == 1:
suffix = 'st'
elif n == 2 or (n % 10) == 2:
suffix = 'nd'
elif n == 3 or (n % 10) == 3:
suffix = 'rd'
elif n < 100:
suffix = 'th'
ord_num = str(n) + suffix
return ord_numSolution
def ordinal(self, num):
"""
Returns ordinal number string from int, e.g. 1, 2, 3 becomes 1st, 2nd, 3rd, etc.
"""Its suspicious that this seems to be a method rather than a free standing function.
self.num = numWhy are you storing the input here? Given the purpose of this function that seems odd.
n = int(self.num)Its doubtful that this is a good idea. What are you converting from? Converting to int should be really be done closer to whether this number came from.
if 4 <= n <= 20:You've made this case larger than necessary, many of those would be correct even with out this test, and its not clear what so special about the range 4-20.
suffix = 'th'
elif n == 1 or (n % 10) == 1:You don't need the or. If n == 1, then that the second condition will be true anyways.
suffix = 'st'
elif n == 2 or (n % 10) == 2:
suffix = 'nd'
elif n == 3 or (n % 10) == 3:
suffix = 'rd'
elif n < 100:
suffix = 'th'What happens if suffix is >= 100? You'll get an error.
ord_num = str(n) + suffix
return ord_numYou don't need to split this across two lines.
Here is my version:
# much code can be improved by using a datastructe.
SUFFIXES = {1: 'st', 2: 'nd', 3: 'rd'}
def ordinal(num):
# I'm checking for 10-20 because those are the digits that
# don't follow the normal counting scheme.
if 10 <= num % 100 <= 20:
suffix = 'th'
else:
# the second parameter is a default.
suffix = SUFFIXES.get(num % 10, 'th')
return str(num) + suffixCode Snippets
def ordinal(self, num):
"""
Returns ordinal number string from int, e.g. 1, 2, 3 becomes 1st, 2nd, 3rd, etc.
"""self.num = numn = int(self.num)if 4 <= n <= 20:suffix = 'th'
elif n == 1 or (n % 10) == 1:Context
StackExchange Code Review Q#41298, answer score: 29
Revisions (0)
No revisions yet.