Assignment 3: Functionality

Due by: Friday, September 15, 2023 at 11:59 p.m.

Your mission, should you choose to accept it, is to implement twelve functions, using Python features covered in Chapters 2 and 3. All of these functions should be saved in a file called assignment3.py. The file must contain the functions, but it doesn't need to have any testing code included (though you should absolutely should test your code).

Each of these functions returns a value. None of them contain any print() statements.

Specification

Below are the headers for the twelve functions, along with a description of their behavior and sample output.

def cube(x):
Return the value of x cubed (raised to the third power).

Sample Output

>>> cube(4)
64

def area(radius):
Return the area of a circle with radius radius.

Sample Output

>>> area(2.8)
24.630086404143977

def leftovers(cupcakes, people):
Return the number of cupcakes leftover after evenly dividing cupcakes cupcakes between people people.

Sample Output

>>> leftovers(16, 5)
1
>>> leftovers(15, 5)
0
>>> leftovers(3, 6)
3

def parity(number):
Return the string 'Even' if number is even and 'Odd' otherwise.

Sample Output

>>> parity(2)
'Even'
>>> parity(9)
'Odd'
>>> parity(0)
'Even'

def biggest(x, y, z):
Return the biggest value of numbers x, y, and z.

Sample Output

>>> biggest(2, 9, 4)
9
>>> biggest(5, 3, 5)
5
>>> biggest(10, 10, 10)
10

def series(n):
Return the sum of the integers from 1 up to n.

Sample Output

>>> series(5)
15
>>> series(100)
5050
>>> series(241)
29161

def quadrant(x, y):
Return 1 if the point (x, y) is in quadrant 1 (positive x and y), 2 if the point is in quadrant 2 (negative x and positive y), 3 if the point is in quadrant 3 (negative x and y), and 4 if the point is in quadrant 4 (positive x and negative y). Treat the value 0 as if it's positive.

Sample Output

>>> quadrant(1, 5)
1
>>> quadrant(-2, 4)
2
>>> quadrant(-4, -6)
3
>>> quadrant(5, -8)
4

def initials(first, last):
Return a string containing the initials of someone whose first name is first and last name is last. Format the initials with a period after each letter and a space in between.

Sample Output

>>> initials('Tom', 'Hanks')
'T. H.'

def longer(word1, word2):
Return whichever of word1 and word2 is longer. If they're the same length, return word1.

Sample Output

>>> longer('goat', 'diversion')
'diversion'
>>> longer('goat', 'goat')
'goat'
>>> longer('combat', 'wombat')
'combat'

def alphabetNumber(letter):
Return the position in the alphabet of letter letter, which can be either lowercase or uppercase. If letter is not a legal letter, return -1.

Sample Output

>>> alphabetNumber('M')
13
>>> alphabetNumber('p')
16
>>> alphabetNumber('&')
-1

def addExtension(file, extension):
Return the file name given by file with a dot (.) and extension added to the end, but only if file does not already end with a dot and the given extension.

Sample Output

>>> addExtension('data', 'pdf')
'data.pdf'
>>> addExtension('data.pdf', 'pdf')
'data.pdf'
>>> addExtension('data.exe', 'pdf')
'data.exe.pdf'

def interleave(string1, string2):
Return the result of interleaving string1 and string2 together, creating a new string that has letters from the two original strings in alternating order. If one string is shorter than the other, append whatever is left of the longer string after the interleaving is finished.

Sample Output

>>> interleave('help', 'flat')
'hfellapt'
>>> interleave('mango', 'biscuit')
'mbainsgcouit'

Turn In

Upload assignment3.py to Blackboard.

All work must be submitted before Friday, September 15, 2023 at 11:59 p.m. unless you are going to use a grace day.

All work must be done individually. You may discuss general concepts with your classmates, but it is never acceptable for you to look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.

Grading

Your grade will be determined by the following weights:

Category Weight
cube() 5%
area() 5%
leftovers() 5%
parity() 5%
biggest() 10%
series() 10%
quadrant() 10%
initials() 10%
longer() 10%
alphabetNumber() 10%
addExtension() 10%
interleave() 10%

Under no circumstances should any student look at the code written by another student. Tools will be used to detect code similarity automatically.