Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


CMPSC-132: Programming and Computation II

Homework 1


Goal: Revise your Python concepts and using them to solve problems using Python’s built-in data types


General instructions:

● The work in this assignment must be your own original work and be completed alone.

● The instructor and course assistants are available on Teams and with office hours to answer any questions you may have. You may also share testing code on Teams.

● A doctest is provided to ensure basic functionality and may not be representative of the full range of test cases we will be checking. Further testing is your responsibility.

● Debugging code is also your responsibility.

● You may submit more than once before the deadline; only the latest submission will be graded.


Assignment-specific instructions:

● Download the starter code file from Canvas. Do not change the function names or given starter code in your script.

● Each question has different requirements, read them carefully and ask questions if you need clarification. No credit is given for code that does not follow directions.

● If you are unable to complete a function, use the pass statement to avoid syntax errors


Submission format:

● Submit your HW1.py file to the Homework 1 Gradescope assignment before the due date.

● As a reminder, code submitted with syntax errors does not receive credit, please run your file before submitting.


Section 1: Overview of functions


Section 2: Function details

rectangle(perimeter, area)

Returns the longest side of the rectangle with given perimeter and area if and only if both sides are integer lengths. In a rectangle perimeter = 2w + 2h and area = w * h

Hints:

● The built-in round(number) function rounds a number to the nearest integer.

● To do integer division (also known as floor division), use the // operator. Floor division, as opposed to true division, discards any fractional result from the output.

● the built-in float.is_integer() method returns True if the float instance is finite with integral value, and False otherwise


Preconditions:


Examples:


frequency(aString)

Returns a string that describes the most frequent item in aString (‘vowel’ or ‘consonant’) and a dictionary with the frequency count of each alphabet letter (in lowercase) in the aString. When a vowel and a consonant have the same frequency, it returns the description of the first letter with that frequency in aString as the most frequent item. For example, in ‘mama’ we return ‘consonant’ and {‘m’: 2, ‘a’: 2}. You may assume aString is a non-empty string with at least one letter of the alphabet, but you cannot make assumptions about the rest of the contents in the string. You are not allowed to use the count method or any other Python count libraries such as Counter, mode, etc., or the min() and max() methods. You will not get credit if you used them in your code.


Hints:

● The str.isalpha() method returns True if all characters in the string are alphabetic and there is at least one character, False otherwise.

● The str.lower() method returns a copy of the string with all the cased characters converted to lowercase. ‘A’.lower() returns ‘a’


Preconditions:


Example:


successors(file)

Returns a dictionary whose keys are included words (as is), and values are lists of unique successors to those words. The first word of the txt file is always a successor to ".". Be careful with punctuation. You can assume the text in the txt file does not contain any contraction words (isn’t, don’t, I’m, etc) but you cannot make assumptions about the spacing or the presence of non-alphanumerical characters. The starter code already contains the code to read the txt file, just make sure the file is in the same directory as your .py file. You must write your code after the file has been read to process the contents.




Example


Hints:

● The str.isalnum() method returns True if all characters in a string are alphanumeric.

● When x = 'We', the type conversion list(x) produces the list ['W', 'e']


Preconditions:


Example:


getPosition(num, digit)

Returns an integer that represents the position of the first occurrence of digit in num. Returns False if digit does not appear in num. We will define digit positions beginning at 1 and count from the right-most digit of the number. For example, in 264387, the digit 7 is at position 1 and digit 6 is at position 5. You are not allowed to type convert num to a string, str(num) or to add the digits into a list to traverse or process the number.


Hint:

● Using floor division (//) and modulo (%) could be helpful here. Floor division by 10 removes the rightmost digit (456//10 = 45), while modulo 10 returns the rightmost digit (456%10 = 6)


Preconditions:


Example:


hailstone(num)

Returns the hailstone sequence starting at the given number until termination when num is 1.

The hailstone sequence is generated as follows:

● If num is even, then the next number in the sequence is num/2.

● If num is odd, then the next number in the sequence is 3 * num + 1.

● Continue this process until num = 1


Preconditions:


Examples:


largeFactor(num)

Returns the largest positive integer that is smaller than num and evenly divides num.


Preconditions:


Examples: