Python代写 | CMT115 Python for Computation
本次Python代写是完成编程小练习集合
CMT115 Python for Computation
Assignment
To complete this coursework, you must complete a set of programming challenges in Python.
Each challenge can be awarded a maximum of 7 marks. You cannot get
more than 100 marks.
The challenges are described in detail below, and you are also provided
with a set of test cases that will check whether your code produces the
required output or not. You should make sure that your submitted code
passes the supplied tests to ensure it functions correctly. However, please
note that your code may be tested against further/different test cases. You
should therefore ensure that you try to cover all corner cases and additional
input values and that your code still functions correctly.
You will find template code for the assignment on Learning Central in a
compressed file. Inside the compressed file you will find a template.py
file, in which you should complete your solutions. You will also find a
test_template.py file containing the test cases that will check your code’s
functionality, along with a folder of test data required for some of the tests.
You are also supplied with a Readme.md file containing detailed instructions
on how to run the test cases to check your code.
In the template, the functions’ interfaces are given but the functions’
bodies are empty. Solve the exercises by correctly filling in the functions’
bodies. It is forbidden to change the functions’ interfaces. However, new
functions can be defined to support the solution of the exercises. These
functions must have names that are different from those already present in
the templates.
In all the exercises, you can assume that the inputs are provided in the
appropriate format. Therefore, error-checking is not needed.
You will be given marks for solving each problem. Further marks will
be awarded for solution style/quality and for solution analysis. The mark
scheme is described in further detail later.
Exercise 1: Smallest Fraction Terms
Complete the function ‘reduceFraction’ that takes two positive integers as
its parameters. The parameters represent the numerator and denominator
of a fraction. The function reduces the fraction to lowest terms and then
returns both the numerator and the denominator of the reduced fraction as
integers. The numerator and denominator should be returned as a tuple.
Example: reduceFraction(12,15) returns (4, 5).
Exercise 2: Magical Dates
A magic date is a date where the day multiplied by the month is equal to
the last two digits of the year.
Example: 10/6/1960 is a magic date because 6 times 10 is 60, which is
equal to the last two digits of 1960.
Complete the function ‘isMagicDate’ that takes three positive integers
as its parameters, day, month, and year, respectively, and returns True if
the date is a magic date, or False otherwise.
Exercise 3: Find All Sublists
A sublist is a list that makes up part of a larger list. A sublist may be a list
containing a single element, multiple elements, or even no elements at all.
Example: [1], [2], [3] and [4] are all sublists of [1, 2, 3, 4]. The
list [2, 3] is also a sublist of [1, 2, 3, 4], but [2, 4] is not a sublist of
[1, 2, 3, 4] because the elements 2 and 4 are not adjacent in the longer
list. The empty list is a sublist of any list. As a result, [] is a sublist of
[1, 2, 3, 4]. A list is a sublist of itself, meaning that [1, 2, 3, 4] is
also a sublist of [1, 2, 3, 4].
Using the above definition of a sublist, complete the function ‘sublist’
that takes a list as its only parameter, and returns a list containing every
possible sublist of the input list.
Example: Given the input
[’a’, 2, (0,”zero”)]
the function should return
[[], [’a’], [2], [(0,”zero”)], [’a’, 2], [2, (0,”zero”)],
[’a’, 2, (0,”zero”)]]|
The order of the elements of the list returned is not important. However,
the order of the elements inside of each list should reflect the order in the
original list. For example, [’a’, 2] is correct, while [2, ’a’] is not.
Exercise 4: English to Pig Latin Translator
Pig Latin is a language game or argot in which English words are altered,
usually by adding a fabricated suffix or by moving the onset or initial consonant or consonant cluster of a word to the end of the word and adding a
vocalic syllable to create such a suffix (Wikipedia).
The following rules are used to translate English into Pig Latin:
• If the word begins with a consonant (including ‘y’), then all letters at
the beginning of the word, up to the first vowel (excluding ‘y’), are
removed and then added to the end of the word, followed by ‘ay’.
Example: ‘computer’ becomes ‘omputercay’ and ‘think’ becomes ‘inkthay’.
• If the word begins with a vowel (not including ‘y’), then ‘way’ is added
to the end of the word.
Example: ‘algorithm’ becomes ‘algorithmway’ and ‘office’ becomes
‘officeway’.
Complete the function ‘pigLatin’ that takes a string as the only parameter and return a string representing its Pig Latin translation.
The function should correctly handle uppercase letters and punctuation
marks such as commas, periods, question marks and exclamation marks.
You can assume that only the first letter can be uppercase and that punctuation marks can only be at the end of the word.
Example: if an English word begins with an uppercase letter, then
its Pig Latin representation should also begin with an uppercase letter and
the uppercase letter moved to the end of the word should be changed to
lowercase. For example, ‘Computer’ should become ‘Omputercay’. If a
word ends in a punctuation mark, then the punctuation mark should remain
at the end of the word after the transformation has been performed. For
example, ‘Science!’ should become ‘Iencescay!’.
Exercise 5: Morse Code Encoder
Morse code is a method used in telecommunication to encode text characters
as standardized sequences of two different signal durations, called dots and
dashes (or dits and dahs). Morse code is named after Samuel Morse, an
inventor of the telegraph (Wikipedia).
Complete the function ‘morseCode’ that takes a string of letters and
numbers as the only parameter and returns a string with its Morse code
representation.
Use a period ‘.’ to represent a dot, and a minus sign ‘-’ to represent a
dash. The mapping from letters and numbers to dashes and dots is illustrated in the following figure.
The function should leave a single space between each sequence of dashes
and dots and it should ignore any characters that are not letters or numbers.
Example: The Morse code for ‘Hello, World!’ is shown below:
…. . .-.. .-.. — .– — .-. .-.. -..
Hint: use dictionaries to represent the Morse code table.
Exercise 6: Spelling Out Numbers
Complete the function ‘int2Text’ that takes an integer between 0 and 999
as its only parameter, and returns a string containing the English words for
that number.
Example: if the parameter to the function is 142 then ‘int2Text’ should
return “one hundred forty two”.
Hint: Use one or more dictionaries to implement your solution rather
than large if/elif/else constructs.