Description
This is a short quiz for you to check your understanding of basic python programming. This is an optional, non-graded but recommended assignment as it will help you get used to both Python as well as the online submission platform on Gradescope. To solve these questions please use the provided solution.py le (it has all the required TODOs except for Question 5) and once you are done, follow the instructions at the end of this document to submit on Gradescope.
Question 1
Complete this function to count the number of characters (character frequency) in a string.
def c h a r _ f r e q u e n c y ( i n p u t _ s t r i n g ):
“”” Returns a d i c t i o n a r y c o n t a i n i n g letters as keys ,
and c o r r e s p o n d i n g f r e q u e n c i e s in i n p u t _ s t r i n g as values
Example Input : i n t e l l i g e n c e
Example Output :
{ ’i ’: 2, ’n ’: 2, ’t ’: 1, ’e ’: 3, ’l ’: 2, ’g ’: 1, ’c ’: 1}
“””
# TODO : Your code here
Question 2
Complete this function that takes a sentence as input, and returns a list of unique words in sorted order from the input sentence. Please ensure that your function converts the input string into lower-case entirely before sorting to avoid duplicate words due to di erences in case. When returning words, make sure all words are in lower-case.
1
def u n i q u e _ w o r d s ( sentence ):
“”” Returns a list of unique words in sorted order from sentence . Example Input : The quick brown fox jumped over the quick brown cat Example Output :
[ ’ brown ’, ’cat ’, ’fox ’, ’jumped ’, ’over ’, ’quick ’, ’the ’]
“””
# TODO : Your code here
Question 3
Complete this function that takes 2 lists as input and returns True if there is at least one element in common.
def h a s _ c o m m o n ( list1 , list2 ) :
“”” Returns a True if list1 and list2 have at least 1 common element .
Example Input : [1,2,3,4,5] , [5,6,7,8,9]
Example Output : True
“””
# TODO : Your code here
Question 4
Part a
Complete the TODO in this class’s area() method.
class |
Re ct an gl e : |
||
def |
__init__ ( self , l , w ) : |
||
self . length |
= |
l |
|
self . width |
= |
w |
|
def |
area ( self ): |
||
“”” Returns the |
area of the re ct an gl e “”” |
||
# |
TODO : Your |
code here |
|
Part b
Add a method perimeter(self ) to Rectangle class to return the perimeter of the rectangle.
Part c
Add a method is bigger(self, target) that takes another Rectangle object called target as an argument and returns True if the rectangle is bigger than the target rectangle in area or False if the target rectangle has a bigger area.
2
Create a class called NumberConverter that has the following methods 1:
to roman(num): Converts the integer argument num to a Roman numeral string, and returns the string.
to decimal(input string): Converts the Roman numeral string argument input string to an integer and returns it.
switch format(data): Identi es whether the argument data is an int or a str and if it is an int converts it to a Roman number string, and returns the string. Otherwise, if data is a str, it converts it to an integer assuming the string is a Roman number and returns the integer. (Hint: use the above two methods when de ning switch format)
Submission Instructions
Once you have completed solution.py head over to https://www.gradescope.com/courses/ 58766/assignments/239587 and upload your solution.py le. The autograder will then evaluate your code and gives you a score out of 16. 2
-
Please ensure that you use the exact same names as mentioned here for your methods and class name. Your submission is autograded and the software requires the names to be consistent
2This score doesn’t count towards your grade, its just a measure for you to evaluate your performance on the quiz.
3