## Question

- The work in this lab must be completed alone and must be your own.

- A doctest is provided as an example of code functionality. You are responsible for debugging and testing your code with enough data, you can share ideas and testing code during your recitation class. As a reminder, Gradescope should not be used to debug and test code!

- Each function must return the output

- Do not include test code outside any function in the upload. Printing unwanted or illformatted data to output will cause the test cases to fail. Remove all your testing code before uploading your file (You can also remove the doctest). Do not include the input() function in your submission.

- Examples of functionality are given in the starter code. Your code should return None when parameters don’t follow the given specifications.

Goal:

Write the function rectangle(perimeter, area), which takes two positive integers, perimeter and area. It returns an integer which is the length of the longest side of a rectangle with integer side lengths w and h which has the given perimeter and area. If no such rectangle exists, it returns None. As a reminder the perimeter of a rectangle with sides w and h is 2w + 2h. The area is w * h. Hint: The built-in function round takes a number as its argument and returns the nearest integer. For example, round(4.0) returns 4, round(4.3) returns 4, and round(4.7) returns 5.

Similarly, to do integer division and get an integer result, discarding any fractional result, there is

another operator, //, which performs floor division

Example:

rectangle(14, 10) will return 5 because a 2 x 5 rectangle 5 is the longest side of a rectangle with

perimeter 14 and area 10

rectangle(25, 25) will return None because a 2.5 x 10 rectangle does not have integer side lengths

Write the method translate(translationDict, txt) that takes in a dictionary and a string. The dictionary contains keys of strings that have a value of another string. You will be translating the input string using the key-value pairs in the dictionary to a new string that has replaced all the words in the input string with the words' value in the dictionary. Your function should convert txt to all lowercase letters and remove punctuation before making the translations. If a word in the string is not in the translation dictionary, the word will retain its original form. You can assume that all the keys in the dictionary are lowercase strings. Hint: str.lower() method returns a new string with all lowercase letters, str.split() splits a string into a list, default separator is any whitespace, and str.isalnum() returns True if all characters in a string are alphanumeric.

Example:

>>> myDict = {'up': 'down', 'down': 'up', 'left': 'right', 'right': 'left', '1': 'one'}

>>> text = 'UP down left right forward 1 time'

>>> translate(myDict, text)

'down up right left forward one time'

Write the function onlyTwo(x, y, z) which takes in three positive integers x, y and z. The function returns an integer with the sum of the squares of the two largest numbers. Hint: the max() method could be useful

Example:

onlyTwo(1, 2, 3) will return 13 since 2*2 + 3*3 = 13

onlyTwo(3, 3, 2) will return 18 since 3*3 + 3*3 = 18

Write the function sumDigits(n) which takes in a positive integer and sums its digits.

Hint: Using floor division (//) and modulo(%) could be helpful here.

Example:

sumDigits(1001) will return 2 since 1+0+0+1 = 2

sumDigits(59872) will return 31 since 5+9+8+7+2 = 31

Write the function largeFactor(num) which takes in an integer num that is greater than 1. The function returns the largest integer that is smaller than num and evenly divides num.

Example:

largeFactor(15) will return 5 since the factors of 15 are 1, 3 and 5

largeFactor(7) will return 1 since 7 is prime

The Hailstone sequence of numbers can be generated from a starting positive integer n by:

• If n is even then the next n of the sequence = n/2

• If n is odd then the next n of the sequence = (3 * n) + 1

• Continue this process until n is 1.

The (unproven) Collatz conjecture is that the hailstone sequence for any starting number always terminates. Write the function hailstone(num) that takes in an integer num and returns a list with the hailstone sequence starting at num.

Example:

hailstone(5) will return [5, 16, 8, 4, 2, 1]

hailstone(6) will return [6, 3, 10, 5, 16, 8, 4, 2 , 1]

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#HW#Due Date:

########################################

#

# Name:

# Collaboration Statement:

#

########################################

# Don't forget to remove testing code that is outside of the functions, including any input() calls

from math import sqrt

def rectangle(perimeter,area):

"""

>>> rectangle(14, 10) # From a 2x5 rectangle

5

>>> rectangle(12, 5) # From a 1x5 rectangle

5

>>> rectangle(25, 25) # A 2.5x10, but one side is not an integer...

By purchasing this solution you'll be able to access the following files:

Solution.zip.