Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code a function called daysuffix that takes a single parameter. The parameter is

ID: 3803370 • Letter: C

Question

Code a function called daysuffix that takes a single parameter. The parameter is checked to ensure it's a number (see isNaN()) and then converted to the nearest integer. A check is made that the integer is in the range l to 31 inclusive. If either of the checks fail return the value null. Finally the integer should be returned with the appropriate day of the month suffix (e.g., "1st", "2nd", "3rd", "27th", etc.). Use only one further return statement (for a total of three). Next, test that your function has the correct behaviour. Use a loop to test all valid integer parameter values (1-31). Also individually test the results returned for non-numeric input and numeric, out-of-range and floating point input. Display the results of your testing.

Explanation / Answer

Python 2.7 code:

def daysuffics(n):
   s = str(n)
   ss = unicode(s, 'utf-8')
   if(ss.isnumeric()):
       d = int(n)
       if(d > 31 or d <= 0):
           print n , "is invalid number (Out of range)!"
           return None
       else:
           mydict = {1 : "1st", 2 : "2nd" , 3 : "3rd", 21: "21st", 22: "22nd", 23:"23rd", 31:"31st"}
           if(d in mydict):
               return mydict[d]
           else:
               return(str(d) + "th")
   else:
       return None
for i in range(1,32):
   print daysuffics(i)
print daysuffics(-1)
print daysuffics("SNCDN")

Sample Output:

1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
25th
26th
27th
28th
29th
30th
31st
None
None

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote