COMP1007 Introduction to Python and Its Applications (Python代写,香港浸会大学代写,COMP1007代写,香港程序代写)

Introduction to Python and Its Applications

联系我们
微信: biyeprodaixie 欢迎联系咨询

本次CS代写的主要涉及如下领域: Python代写,香港浸会大学代写,COMP1007代写,香港程序代写

Answer ALL FIVE questions! – Question 5 has Part (a) & Part (b) You have 1.5 hours and you should work on your own until 11:00am. Submit your answers in one .py file through the submission box at the Course Moodle Page.

Question # Because of the outbreak of the Coronavirus (COVID-19), visitors from infested countries and regions have to be quarantined for 14 days which is 14 X 24 X 60 = 20160 minutes!

Write a function Min2DHM(minutes) that takes the number of minutes as a parameter and converts that number of minutes into a specific format below:

Day(s): Hour(s): Minutes(s)

Note: You can assume the number of minutes is between 0 and 20160 and here is the expected output of your function.

Test Runs for Question #1:

Min2DHM(0) Zero minute!

Min2DHM(1) 1 Minute = 1 Minute

Min2DHM(5) 5 Minutes = 5 Minutes

Min2DHM(20160) 20160 Minutes = 14 Days

Min2DHM(20159) 20159 Minutes = 13 Days 23 Hours 59 Minutes

Min2DHM(10000) 10000 Minutes = 6 Days 22 Hours 40 Minutes

Min2DHM(1000) 1000 Minutes = 16 Hours 40 Minutes

Min2DHM(4321) 4321 Minutes = 3 Days 1 Minute

Question # 2 Write a function Summation(n) that takes a number n as a parameter and print out the integers from 1 to n and also print out the calculated summation as follows:

Test Runs for Question #2:

Summation(1) 1 = 1

Summation(3) 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6

Summation(10) 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 1 + 2 + 3 + 4 + 5 = 15 1 + 2 + 3 + 4 + 5 + 6 = 21 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

Question # 3 Write a function Median(list) that takes an unsorted list of n numbers as a parameter and return the median. You can assume the number of items in the list is between 2 and 9. To find the median from a list of items, you have to sort the list and find out the value of the item in the middle.

Note: If the list has an odd number of items, the median is the value of the middle item. If the list has an even number of items, the median is the average of the two middle items.

Test Runs for Question #3:

List1 = [9, 3, 6, 4, 5, 2, 7, 8, 1] Median(List1) 5

List2 = [5, 3, 1, 6] Median(List2) 4.

List3 = [4, 3] Median(List3) 3.

Question # 4 Write a function DiveScore(degree_of_difficulty, list_of_7) that takes an unsorted list of scores from the seven judges, together with the degree of difficulty as parameters, the function is going to return the overall score for this dive. Here is how an individual diving score is calculated according to the US Diving system:

  1. Each of the 7 judges awards a score to a diver for his or her dive. ( For Example, 6, 5, 5, 5, 5, 5, 4)
  2. The two highest and two lowest scores are eliminated. (6 and 4 and two of the 5s are eliminated)
  3. The remaining scores are totaled. (5 + 5 + 5 = 15)
  4. This total (15) is then multiplied by the degree of difficulty rating to calculate the overall score. These degree of difficulty ratings range from 1.2 to 4.1, in one-tenth increments. Hence, the overall score for a degree of difficulty rating of 3.1 for our example is 15 × 3.1 = 46.

Test Runs for Question #4:

Dive1 = [9, 7.5, 8, 7.5, 9.5, 8, 8.5] DiveScore(3.0, Dive1) 73.

Dive2 = [8, 8.5, 7, 7.5, 9, 9, 9] DiveScore(3.0, Dive2) 76.

Question #5a Given a file “Q5.txt” that contains the following lines of text: Mom! Do Geese see God? Does Goose see God? Taco Cat Taco cats Was it a car or a cat I saw? Race Car Borrow or rob? A man, a plan, a canal: Panama

Write a function Q5a(filename) that open the file according to the filename and read one line of text at a time until the end. For each line of text,

  1. store it in a variable called pstring
  2. remove all whitespaces from pstring
  3. remove all punctuations from pstring
  4. turn all the characters in pstring to lowercase
  5. print the modified pstring out

Test Runs for Question #5a:

Q5a("Q5.txt") mom dogeeseseegod doesgooseseegod tacocat tacocats wasitacaroracatisaw racecar borroworrob amanaplanacanalpanama

Question #5b Palindromes are words or phrases that read the same backward and forward, letter for letter, and number for number. While “mom” and “otto” are palindromes, “Yo, banana boy!”, and “Madam, in Eden, I’m Adam.” are palindromes too after you remove all the whitespaces, punctuations, and turn every letter into lowercase (Just like what you have done to pstring in Question #5a).

Hence, for Question #5b, write a function Q5b(filename) that open the file according to the filename and read one line of text at a time until the end. For each line of text,

  1. store it in a variable called pstring
  2. remove all whitespaces from pstring
  3. remove all punctuations from pstring
  4. turn all the characters in pstring to lowercase
  5. check pstring and print whether it is a palindrome or not

Hints: You can base on the function Q5a(filename) to write your function Q5b(filename). Just write a function is_palindrome(s) that checks whether the string s is a palindrome or not and use it at the end of Q5a(filename).

Test Runs for Question #5b:

Q5b ("Q5.txt") mom is a palindrome! dogeeseseegod is a palindrome! doesgooseseegod is NOT a palindrome tacocat is a palindrome! tacocats is NOT a palindrome wasitacaroracatisaw is a palindrome! racecar is a palindrome! borroworrob is a palindrome! amanaplanacanalpanama is a palindrome!

If your Q5a(filename) is not working properly, you can still write a function is_palindorme(s) and test it with the following strings: P1 = "Mom!" P2 = "Do Geese see God?" P3 = "Does Goose see God?" P4 = "Taco Cat" P5 = "Taco cats" P6 = "Was it a car or a cat I saw?" P7 = "Race Car" P8 = "Borrow or rob?" P9 = "A man, a plan, a canal: Panama"

End of Practical Test