Question

In: Computer Science

(IN PYTHON 3) Complete the following two tasks: 1. Words like first, second, and third are...

(IN PYTHON 3) Complete the following two tasks: 1. Words like first, second, and third are referred to as ordinal numbers. In this exercise, you will write a function that takes an integer as its only parameter and returns a string containing the appropriate English ordinal number as its only result. Your function must handle the integers between 1 and 12 (inclusive). It should return an empty string if a value outside of this range is provided as a parameter. 2. The Twelve Days of Christmas is a repetitive song that describes an increasingly long list of gifts sent to one’s true love on each of 12 days. A single gift is sent on the first day. A new gift is added to the collection on each additional day, and then the complete collection is sent. The first two verses of the song are shown below. The complete lyrics are available online. On the first day of Christmas My true love sent to me: A partridge in a pear tree. On the second day of Christmas My true love sent to me: Two turtle doves, And a partridge in a pear tree. Your task is to write a program that displays the complete lyrics for the 12 Days of Christmas. Write a function that takes the verse number as its parameter and displays the specified verse of the song. Then call that function 12 times with integers that increase from 1 to 12. Each item that is sent to the recipient in the song should only appear once in your program, with the possible exception of the partridge. It may appear twice if that helps you handle the difference between “A partridge in a pear tree” in the first verse and “And a partridge in a pear tree” in the subsequent verses. Import the function from task one to assist.

Solutions

Expert Solution

Part 1: Convert integer to english ordinary number.

Part 2 : Print the Twelve Days of Christmas Song

#-------------PART 1-----------------------------
#Convert integer to ordinary number
def words(number):
  #word list contains ordinary numbers
  word=["First", "second ", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth", "Eleventh","Twelfth"]
  #Check the range
  if 1 <= number <= 12:
    #Return the ordinary number 
    return word[number-1]
  else:
    #return empty string
    return ""


#------ PART 2--------------------------------------
#Display Twelve Days of Christmas Song
def verses(number):
  #Store verse in a list 
  verse=['A partridge in a pear tree','Two turtle doves, and','Three French hens ,','Four calling birds,','Five Gold Rings,','Six geese a laying,','Seven swans a swimming,','Eights maids a milking,','Nine ladies dancing,','Ten lords a leaping,','Eleven pipers piping,','Twelve drummers drumming']
  #Call method words() to get the ordinary number 
  print(f"On the {words(number)} day of Christmas my true love gave to me")
  #Display verses till that days
  print("\n".join(verse[number-1::-1]))
  print()

#Call verses 12 times using range()
for i in range(1,13):
  verses(i)

Output:

Summary:

Part 1: Function to convert integer number into english ordinary number.

Steps:

  • Store ordinary number from first to twelfth into a list word from index 0 to 11.
  • Check the number passed to the method is within range (1<=number<=12) both 1 and 12 inclusive.
    • If within range return the ordinary number with 1 index less word[number-1]
    • Else return empty string
  • Output words(2)
    • number = 2
    • Ordinary word is in 1 Index less => word[2-1] =>word[1]
    • Return word[1] => second

Part 2: Print the Twelve Days of Christmas lyrics - takes parameter number(int)

The lyrics starts with On the {first} day of Christmas my true love gave to me . The first varies by the number passed into the method.

Steps:

  • Store the verse for the lyrics in verse list for each gift in the twelve days.
  • Print the first line of the lyrics by calling method words(number ) to get the ordinary number.
  • Convert the list into string till the number of days (number) using join()
  • Print the verse

Call the function verses twelve times for twelve days and pass the day number .

The function uses range(start,end) to generate sequence of number from start to end (end exclusive).


Related Solutions

A stock provides dividend of $1 at the end of the first, second, and third year,...
A stock provides dividend of $1 at the end of the first, second, and third year, its spot price is $30 and the risk-free rate for all maturities (with continuous compounding) is 10%. What is the four year forward price? What is the value of this forward contract two years later if the forward price at that time is $40?
1. triple integral... first form 0 to pi, second from 0 to pi/3, third to sec(phi)...
1. triple integral... first form 0 to pi, second from 0 to pi/3, third to sec(phi) to 2 of p^2*sin(phi) dp d(phi) d(theta) 2. triple integral... first form 0 to pi, second from -sqrt(3) to sqrt(3), third to 1 to sqrt(4-r^2) of r dz dr d(theta) 3. triple integral... first form 0 to pi/2, second from 0 to sqrt(3), third to 1 to sqrt(4-r^2) of r dz dr d(theta) 4. triple integral... first form 0 to pi, second from 0...
(x2+ 3x -1) / (x3+ 2x2+ 2x + 1) show the first, second, third and fourth...
(x2+ 3x -1) / (x3+ 2x2+ 2x + 1) show the first, second, third and fourth derivative
1. Describe in depth the three phases of the first stage of delivery, second stage, third...
1. Describe in depth the three phases of the first stage of delivery, second stage, third stage, and fourth stage of the delivery?
Which generation of quinolones are broad spectrum? First Second Third
Which generation of quinolones are broad spectrum? First Second Third
Describe direct, first, second, and third order control system.
Describe direct, first, second, and third order control system.
Read the following scenario and complete the questions and tasks below. Marcy recently started her first...
Read the following scenario and complete the questions and tasks below. Marcy recently started her first job after graduating from business school with her accounting degree. She is very excited to be working in her field and although she knows she still has a lot to learn, she already feels like she is getting a good feel for what it takes to be successful. She has been assigned to work under one of the senior accountants, Mrs. Bradlee, and so...
Determine the first, second, and third quartiles of the following data. 10.5   14.7   15.3   17.7   15.9  ...
Determine the first, second, and third quartiles of the following data. 10.5   14.7   15.3   17.7   15.9   12.2   10.0 14.1   13.9   18.5   13.9   15.1   14.7 This is a question about a Business Statistics. Please explain how we get the first, second, and third percentile from these numbers.
how to find the first, second, third quantile of a column in a dataset with R?...
how to find the first, second, third quantile of a column in a dataset with R? the dataset is like this: columns values a. 1. 2. 3. 5. 6. 7. 4. 7. 8. ......... b 5. 6. 7. 3. 8. 0. 4. 7. 4. 7. ......... c    1. 2. 3. 5. 6. 7. 4. 7. 8. ......... d 6. 3. 1. 0. 8. 3. 6. 6. 3.........
PYTHON PLEASE-------- For the first module, write the pseudocode to process these tasks: (Note: lines beginning...
PYTHON PLEASE-------- For the first module, write the pseudocode to process these tasks: (Note: lines beginning with # are comments with tips for you) From the random module import randint to roll each die randomly # in pseudocode, import a random function # the name is helpful for the next M5-2 assignment Define a class called Dice In Python, the syntax has a colon after it: class Dice(): In pseudocode, you can specify it generally or be specific Under the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT