Intro to Programming with Python - Assignment 5
Intro to Programming with Python - Assignment 5
Assignment № 5 Page 1
Problem 1 - Sum array (20pts)
Write a recursive divide-and-conquer function called sum_array(array) for calculating the sum
of the elements in an array. This function calculates the sum of the elements in an array by re-
cursively calling itself with the first and second halves of the array, which are divided by the
middle element of an array, and returning the sum of the two intermediate results.
Imagine you have an array arr = [3,5,2,7,1]. Get the middle position with mid = len(arr)//2.
You can get the sum of this list by repeating the same process with recursion on the two halves
of this list: left_half = arr[:mid], right_half = arr[mid:], and adding their results.
Prompt the user for the list of elements: integers on one line, separated by a space. Print
the result.
Recall from previous week you have to create a for loop to convert a list of strings into a list
of integers, but now we can create the function to_int, that will help converting a list of strings
into integers more convenient!
Define a recursive function called to_int(a_list) to apply the built-in function int to each el-
ement in the array. For example, if the we call the function as:
numlist = to_int(["1","2","3"]), we will get back a list with every element converted to in-
tegers: numlist becomes [1,2,3].
Prompt the user for the list of elements: integers on one line, separated by a space. Print
out the new list of integers after conversion. Assume user input will only be integers.
Attempt to solve this problem by yourself first, if you get stuck, a hint is provided at the
end of this document.
Assignment № 5 Page 2
Sample Input and Expected Output (Test Cases)
Write a program to create a Pascal’s triangle (see lecture note) using the recursive method. For
an integer input n, the script will produce a Pascal triangle with n rows.
Create a Python class to represent a student and their scores on different exams, where the lab
score is worth 40%, the midterm score is worth 30%, and the final score is worth 30% of the
total score. Each score can range from 0 to 100. Specifically, the Student class should include
the following features:
• The __init__ method initializes the object with the required properties, in this case the
student’s name and their scores on the lab, midterm, and final exams.
• The get_total_score method calculates the total score of the student by multiplying each
exam score by its corresponding weight and adding the results.
• The get_grade method calculates the student’s grade based on their total score, using
the following scale: A: 90 or above, B: 80-89, C: 70-79, D: 60-69, F: below 60.
• The print_info method now uses a single print statement to print all the information in a
single line, separated by commas. The total score is formatted to show only two decimal
places using the :.2f formatting specifier:
_, Total score: _, Grade: _
Assignment № 5 Page 3
Figure 1: Student Class template.
3 90
4 85
5 95 @
6 John Smith , Total score : 90.00 , Grade : A
Problem 5 (20pts)
(ii) A constructor that accepts three parameters (son_name, father_name, and mother_name).
Also, increase the class variable count whenever a Family object is created.
(iii) Convert Python object into string by using __str__ to output the family number as show
below (Hint: use the class variable count).
Next, create classes Father and Mother which inherit from the class Family with class
method show_father and show_mother, respectively. The output format are shown in the ex-
ample below.
Lastly, create a class Son that inherits both classes Father and Mother. This class has the
following properties:
(ii) A class method show_parent which calls show_father and show_mother to show the par-
ents info (see example below).
Assignment № 5 Page 4
(iii) A class method show_son to show son’s name and age (see example below).
Prompt user to enter (i) the number of families, and (ii) information for each family: son_name,
son_age, father_name, father_age, mother_name, mother_age, separated by comma (’,’). Then,
create Son instance for each family (assuming there is one son in each family), and printout fam-
ily number info, and call show_son and show_parent to output the info as shown in the following
sample output:
Assignment № 5 Page 5