Python代写 | CMPSC-132: Programming and Computation II

CMPSC-132: Programming and Computation II
Fall 2019
Homework 1
Due Date: 09/20/2019, 11:59PM ET 100 pts
Read the instructions carefully before starting the assignment. Make sure your code follows the stated guidelines to ensure full credit for your work.
– The work in this lab must be completed alone and must be your own.
– Download the starter code file from the HW1 Assignment on Canvas. Do not change
the function names or given started code on your script.
– A doctest is provided as an example of code functionality. Getting the same result as the doctest does not guarantee full credit. 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 use print in your final submission, otherwise your submissions will receive a -1 point deduction)
– Do not include test code outside any function in the upload. Printing unwanted or ill- formatted 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.
– All functions must return the None keyword if the data given to the function is not the expected type. Don’t forget to consider the edge cases.
[20 pts] Write the function isPower(x,y) which takes in two integers x and y. The function checks if y is a power of x. If y is a power of x, return what power it is. Example: x = 3, y = 9. 9 is a power of 3, and three must be raised to the second power, so isPower would return 2. Function would return -1 if y is not a power of x. You are not allowed to use the math module.
isPower(4, 64) will return 3 since 4^3 = 64.
isPower(17, 90) will return -1 since 90 is not a power of 17
[20 pts] 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 before making the translations. If a word in the string is not in the translation dictionary, the word will retain its original form. 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.

>>> 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’
[20 pts] 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
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
[20 pts] 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.
largeFactor(15) will return 5 since the factors of 15 are 1, 3 and 5 largeFactor(7) will return 1 since 7 is prime
[20 pts] The Hailstone sequence of numbers can be generated from a starting positive integer, n
• 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.
hailstone(5) will return [5, 16, 8, 4, 2, 1] hailstone(6) will return [6, 3, 10, 5, 16, 8, 4, 2 , 1]
• Submit your code with the file name to the HW1 GradeScope assignment before the due date. If you were unable to complete a function, comment out the function definition.


本网站支持淘宝 支付宝 微信支付  paypal等等交易。如果不放心可以用淘宝交易!

E-mail: [email protected]  微信:dmxyzl003