Question

In: Computer Science

Write a function named int2ordinal that takes an integer as its only parameter and returns the...

Write a function named int2ordinal that takes an integer as its only parameter and returns the number with its appropriate suffix as its result (stored in a string). For example, if your function is passed the integer 1 then it should return the string "1st". If it is passed the integer 12 then it should return the string "12th". If it is passed 2003 then it should return the string "2003rd". Your function must not print anything on the screen.

You can use the remainder operator to extract the last digit of an integer by computing the remainder when the integer is divided by 10. Similarly, you can extract the last two digits of an integer by computing the remainder when the integer is divided by 100. For example 29 % 10 is 9 while 1911 % 100 is 11. Then you can construct the string that needs to be returned by your function by converting the integer parameter into a string by calling the str function, and concatenating the appropriate suffix using the + operator.

Solutions

Expert Solution

REQUIRED CODE IS :

def int2ordinal(num):
    num = str(num)
    if len(num) > 2:  # Will be a year, not necessarily consisting of four digits. Could be 333
        end_digits = int(num) % 100
    else:   # Will be month / day
        end_digits = int(num) % 10
    if end_digits == 1: return (num + "st")
    if end_digits == 2: return (num + "nd")
    if end_digits == 3: return (num + "rd")
    else: return (num + "th")


def main():

  day = input("Enter a day between 1 and 31: ")
  month = input("Enter a month between 1 and 12: ")
  year = input("Enter a year between 1 and 2100: ")

  print("On the", int2ordinal(day), "day of the", int2ordinal(month), \
        "month of the", int2ordinal(year), "year, I got my degree")

main()

CUSTOM INPUT :

1

3

2013

OUTPUT :

'On the', '1st', 'day of the', '3rd', 'month of the', '2013th', 'year, I got my degree'



Related Solutions

Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
In MATLAB define a function named primes that takes a non-negative integer, ?, as its only...
In MATLAB define a function named primes that takes a non-negative integer, ?, as its only argument and returns a row vector containing the first ? prime numbers in order. Assume that the first prime number is 2.  Other than the zeros function, the ones function, and the colon (:) operator, you may not use any Matlab built-in array functions.
(C++) Write a function that takes as an input parameter an integer that will represent the...
(C++) Write a function that takes as an input parameter an integer that will represent the length of the array and a second integer that represents the range of numbers the random number should generate (in other words, if the number is 50, the random number generator should generate numbers between 0 and 49 including 49. The function then sorts the list by traversing the list, locating the smallest number, and printing out that smallest number, then replacing that number...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and...
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and use as stack to convert the integer to a its corresponding binary representation. Hint: divide the integer by 2. 2. A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward—assuming that you ignore spaces, punctuation, and case. For example, Race car is a palindrome. So is A man, a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT