Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Intro to Programming with Python - Assignment 5

This document outlines Assignment 5 for BANA3020 Introduction to Programming, due on December 18, 2024, which includes multiple programming problems involving recursion, class creation, and data manipulation. Students are instructed to submit their work in specified formats and are reminded of academic honesty. The assignment consists of five problems, each with specific requirements and expected outputs, including functions for summing arrays, converting strings to integers, creating Pascal's triangle, and developing a student grading system.

Uploaded by

Thanh Doãn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Intro to Programming with Python - Assignment 5

This document outlines Assignment 5 for BANA3020 Introduction to Programming, due on December 18, 2024, which includes multiple programming problems involving recursion, class creation, and data manipulation. Students are instructed to submit their work in specified formats and are reminded of academic honesty. The assignment consists of five problems, each with specific requirements and expected outputs, including functions for summing arrays, converting strings to integers, creating Pascal's triangle, and developing a student grading system.

Uploaded by

Thanh Doãn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 5

BANA3020 Introduction to Programming Fall 2024 – Assignment 5

Assignment Submission Instructions:

• Due date: Dec 18th, 2024


• Your program should work correctly on all inputs. If there are any specifications about
how the program should be written (or how the output should appear), those specifications
should be followed.
• Your code and functions/modules should be appropriately commented. However, try to
avoid making your code overly busy (e.g., include a comment on every line).
• Variables and functions should have meaningful names, and code should be organized
into functions/methods where appropriate.
• Academic honesty is required in all work you submit to be graded. You MUST NOT copy
or share your code with other students to avoid plagiarism issues.
• Use the template provided to prepare your solutions.
• Upload your work to Canvas as a .py file.
• Submit separate .py file for each problem with the following naming format: Assign-
ment1_Q1.py. Failure to submit a .py file for lab or assignment will result in a 0. Note: If
you are working on Jupiter Notebook, you need to download/convert it to Python .py file
for submission.
• Late submission of an assignment without an approved extension is NOT allowed.

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.

sum(A) = sum(Aleft half ) + sum(Aright half )

Prompt the user for the list of elements: integers on one line, separated by a space. Print
the result.

Sample Input and Expected Output (Test Cases)

1 ^ > > > python Lab10_Q3 . py ^


2 @1 2 3 4 5 @
3 15

1 ^ > > > python Lab10_Q3 . py ^


2 @1 3 5 7 9 11 @
3 36

Problem 2 - Converting list to integers with recursion (20pts)

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)

1 ^ > > > python Lab10_Q4 . py ^


2 @1 2 3 4 @
3 [1 ,2 ,3 ,4]

1 ^ > > > python Lab10_Q4 . py ^


2 @ -1 -2 3 4 5 -6 @
3 [ -1 , -2 , 3 , 4 , 5 , -6]

Problem 3 - Pascal Triangle (20pts)

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.

Problem 4 - Course Grade Information

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: _

Sample Input and Expected Output (Test Cases)

1 ^ > > > python Lab11_Q3 . py ^


2 @John Smith

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)

First, create a class named Family with the following properties:

(i) A class variable count and set to 0 by default.

(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:

(i) A constructor that accepts 06 parameters (son_name, son_age, father_name, father_age,


mother_name, mother_age). Invoke the base class constructor and set the attributes for
son_age, father_age, and mother_age.

(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

You might also like