Python代写 | COMPS209F Data Structures, Algorithms, and Problem Solving

这个作业是用Python完成几个数据结构相关的练习
COMPS209F
Data Structures, Algorithms, and Problem Solving
Preamble
This assignment has replaced the Practical Test (15%) for this presentation of COMPS209F, as
an exceptional measure for the suspension of on-site teaching and learning activities for the
Spring 2020 term.
Submission
All answers should be submitted in files. Each question specifies the files (i.e.the names of the
files) to be submitted. All the files should be combined into a ZIP file. The file name should be
OUID-Surname.zip. For example, if you OUID is 12345678 and your surname is Chan, then
the file name should be 12345678-Chan.zip.
Please submit the ZIP file to the OLE – COMPS209F – Assigment 1
Grading of Programming Exercises
• A program header, containing at least your name, OUID, date, and the purpose of the
program, should be included in every program file. Failure to do so will be penalized.
• Submitted programs that are fail to run may get 0 mark.
• Programs with poor styles such as redundant logic and statements and poor chosen
variable names may be penalized up to 20% of the allocated mark.
• Input validation is not required, unless it is explicitly required by the question.
• Your solution must be coming from your own independent work. Academic
dishonesty will result in severe penalty.
Submission Summary
The following table shows the files that may be submitted. The submitted files will be
computer-processed. Failure to use the correct file names may earn you zero mark. Skeleton
files are provided for most questions for your convenience.
Filename Format Question
q1a.py Python Source Question 1(a)
q1b.py Python Source Question 1(b)
q1c.py Python Source Question 1(c)
q1d.py Python Source Question 1(d)
q1e.py Python Source Question 1(e)
q1f.py Python Source Question 1(f)
q1g.py Python Source Question 1(g)
q1h.py Python Source Question 1(h)
q1i.txt Text File Question 1(i)
q1j.py Python Source Question 1(j)
q2a.py Python Source Question 2(a)
COMPS209F Assignment (Spring 2020)
10:04 AM 4/9/20 3
Question 1 Programming Exercises (76%)
(a) One kilometer is equivalent to 0.621371 miles. Complete a following Python function for
converting a length in kilometers to miles.
def convert2mile(kilometer):
The parameter holds the kilometer to be converted. A similar function convert2inches is
given to you as a reference Submit your solution in q1a.py. [12 marks]
(b) Chris believes that an English word is lucky if it contains the sequence “uck”. Complete a
Python function to return a different value according to the parameter word, which is
assumed always a string.
• Return True if word is lucky
• Return False if word is unlucky
def isLuckyWord(word):
Hint: There is a function in the Python string library for finding the sequence “uck” easily.
Submit your solution in q1b.py. [12 marks]
(c) Complete the following function that takes in a list of numbers.
def isIncreaseList(numlist):
The function should return different values according to the parameter numlist.
• Return True if the numbers in numlist are in strictly increasing (ascending) order.
• Return False if otherwise.
Assume the parameter numlist is always a list of numbers. Some examples are shown below.
Parameter numlist Return value
[1, 2, 3, 4, 5, 6, 7, 8, 9] True (because the numbers are strictly increasing)
[1, 1, 3, 4, 5, 6, 7, 8, 9] False (1 following 1 is not strictly increasing)
[1, 2, 3, 4, 5, 6, 7, 6, 9] False
[1] True
[] True (empty list is considered to be True)
Submit your solution in q1c.py. [8 marks]
(d) Based on your solution for the above part, add parameter checking to the function.
The function should also raise errors according to the following conditions.
• Raise ValueError with the message “None parameter” if numlist is None.
• Raise TypeError with the message “Not a list” if numlist is not a Python list.
Submit your solution in q1d.py. [8 marks]
COMPS209F Assignment (Spring 2020)
10:04 AM 4/9/20 4
(e) A function named rollDices is given to you that returns the sum of rolling two 6-sided
dices. A 6-sided die is a random generator of integers from 1 to 6. So the sum of two 6-sided
dices is a random integer from 2 to 12.
Modify the function parameter list and the function body definition so that the following
function calls will all work as described.
rollDices() Returns the sum of randomly rolling two 6-sided dices
rollDices(8) Returns the sum of randomly rolling two 8-sided dices
rollDices(8, 3) Returns the sum of randomly rolling three 8-sided dices
rollDices(numDices=3) Returns the sum of randomly rolling three 6-sided dices
rollDices(numDices=4, sided=12) Returns the sum of randomly rolling four 12-sided dices
Submit your solution in q1e.py. [6 marks]
(f) Complete the program q1f.py. The program contains a statement that creates a list of 10
random integers in the range from -100 to 100 (inclusive). Add code to filter the list after the
statement so that any number ended with digit 9 and any number between 13 and 31
(inclusive) should be removed from the list.
Your solution must make use of the filter function with a suitable lambda function as the
filter.
Submit your solution in q1f.py. [6 marks]
(g) Complete the following function called strange that returns the sum of a number sequence.
The strange function must be implemented using recursion.
def strange(N):
The function is defined as follows.
�������(�) = 3 + 6 + ⋯ + 03 ∗ (� − 1)4 + (3 ∗ �)
�������(0) = 0
Some examples are given for illustration.
�������(1) = 3
�������(2) = 9
�������(9) = 135
Submit your solution in q1g.py. [6 marks]
COMPS209F Assignment (Spring 2020)
10:04 AM 4/9/20 5
(h) The file q1h.py contains the defintion of a class Account that represents a bank account. The
data model of the current version consists of two attributes (class variables), namely name
(the account owner) and balance (the balance of the account).
(i) The current definition does not support credit. Credit is defined as the lowest
level that the account balance can reach. The account balance is allowed to be
negative, meaning that money is borrowed from the bank. In the __init__
function, create an attribute named credit and initialize it to 0.
Note: the value of the attribute credit means how much the balance can go below
0. For example, if credit is -5000, then the balance is allowed to go as low as
-5000.
(ii) Complete the class function called setCredit. The function should assign the
parameter creditlevel to the class attribute credit.
def setCredit(self, creditlevel):
(iii) Modify the class function withdraw so that if the final balance after the
withdraw is below the credit, the withdraw is not allowed and a ValueError
should be raised with the message “Invalid Withdraw”.
Submit your solution in q1h.py. [6 marks]
(i) Apply selection sort to the following word list based on these properties by hand.
• Ascending order (largest at the right-most). Use largest first progress order
• Each word is one unit.
• The smallest string is the one with the shortest word length (number of characters),
and if the word length is equal, the alphabetical order is followed (the case is
ignored).
• Show the word list after putting each word in the correct location. Use a vertical bar
(|) to separate sorted part and unsorted part of the list.
Lion Tiger Kangaroo Panda Owl Giraffe Parrot Whale |
Submit your solution in q1i.txt. [6 marks]
(j) An implemnetation of stack is given to you in stack.py with the class named Stack. Write a
complete program that reads in a string from the keyboard, reverse the string with a stack,
store the reversed string in a variable called reversed, and finally print out the reversed
string.
Submit your solution in q1j.py. [6 marks]
COMPS209F Assignment (Spring 2020)
10:04 AM 4/9/20 6
Question 2 Mini-Project (24%)
(a) Two data files are given to you.
The file europe_city.csv contains the geo-location (longitude and latitude) and average
temperature (in degree Celcius) of cities in Europe. The first line of the file contains the
header information, that should explain the data format.
The file europe_country.csv contains the population, EU membership of countries in
Europe and whether the country is coastal. The first line of the file contains the header
information, that should explain the data format.
Write a complete program that satisfies the following requirements.
(i) Load each of the two files, europe_city.csv and europe_country.csv,
into a list of dictionaries. There should be two lists after loading the data. Store
them into variables cities and countries respectively.
(ii) Add code to sort the list of dictionaries of countries according to the country
name alphabetically and ascendingly. Print out the sorted country list, with each
line contains the country name and the population.
(iii) Add code to search the two lists of dictionaries and print out the findings in
sections.
• Result 1: Print out the number of cities and the number of countries in the
two lists.
• Result 2: Print out the name and temperature of the city with the lowest
temperature.
• Result 3: Print out the name and temperature of the southernmost city (i.e.
with the minimal Latitude)
• Result 4: Print out the name and temperature of the northernmost city (i.e.
with the maximum Latitude) in “Austria”.
• Result 5: [Challenging] Print out the name, country and the temperature of
the westernmost city (i.e. with the minimum Longitude) of which the
country is landlock (i.e. Coastal is False).
Submit your solution in q2a.py. [24 marks]