0% found this document useful (0 votes)
27 views

assignment3_Advanced Python_UHasselt

The assignment requires students to implement two recursive functions in Python: one to generate a sequence based on a natural number n that converges to 1, and another to compute n raised to the power of m efficiently. Submissions must include a Python file named assignment3.py and a separate PDF detailing the time complexity for the second question, if applicable. Plagiarism and cooperation are strictly prohibited, and all submissions must be made individually through Blackboard by the due date of April 20, 2025.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

assignment3_Advanced Python_UHasselt

The assignment requires students to implement two recursive functions in Python: one to generate a sequence based on a natural number n that converges to 1, and another to compute n raised to the power of m efficiently. Submissions must include a Python file named assignment3.py and a separate PDF detailing the time complexity for the second question, if applicable. Plagiarism and cooperation are strictly prohibited, and all submissions must be made individually through Blackboard by the due date of April 20, 2025.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Advanced Programming in Python

Assignment 3: Recursion

Due date is 20/04/2025 at 23:59

The assignment is strictly individual. Plagiarism and cooperation will be


thoroughly checked by hand, and by specialised software. Not only copying
other peoples code, but also giving your source code to other students will
be considered as fraud. Using solutions from the internet, even very small
portions are considered as plagiarism.
When we detect fraud, conform with the exam regulations (OER), we
will inform the exam comity, which will start a fraud process (see the OER).
Unfortunately, every academic year students get caught; we hope that this
will not be the case this year.

Questions
Implement the following programs in python:
1. Write a recursive function generate sequence, such that given a natural number n, it
returns the list [n1 , ..., nm ], where:

ˆ n1 = n,
ˆ nm = 1,
ˆ for every i < m, we have ni ̸= 1,
ˆ if ni is even, then ni+1 = ni /2, and
ˆ if ni is odd, then ni+1 = 3 ∗ ni + 1.

Thus, given n, if we keep updating n as follows: n = n/2 if n is even and n = 3 ∗ n + 1 for


n is odd. Our number will eventually converge to 1 irrespective of the choice of n. The
output function is the sequence of numbers generated until we converge to 1.

Example
Let n = n1 = 7. Since n1 is odd, we get n2 = 3 ∗ n1 + 1 = 22. Since n2 is even, we get
n3 = n2 /2 = 11. This should be repeated till we get 1.
Hence, generate sequence(n) returns [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1].

2. Write a recursive function compute_power that takes two non-negative integers n and m
as inputs. The function computes the result of raising n to the power of m (i.e. n^m) in
an efficient way. The number of multiplications used in your implementation should be
in less than linear amount of multiplications. In other words, the time complexity of your
implementation should be strictly less than O(m).

1
Hint: n6 = n3 × n3 .
Note: You have to argue why your implementation has a time complexity of less than
O(m). Also, you are not allowed to use any predefined power operators.

Submission and Grading Criteria


First of all, do not solve the assignment in multiple python files. We expect from you
one pdf file (check second paragraph) and one python file that contains all the solutions of the
assignment’s questions. In case you are not solving all the questions, then make sure that you
also include the functions of the unsolved questions in your submission with pass in the body.
Moreover, your solution file must have the name assignment3.py; otherwise, the tests will not
run. Also, make sure that file begins with
"""
author : [ firstname lastname ]
studentnumber : [ your studentnumber ]
"""

Before the deadline make sure to submit both files through Blackboard. You should not submit
both files in a zip file, rather you have to upload both files individually through Blackboard.

Question 2 Time Complexity As mentioned in the second question, you have to determine
the time complexity of your implementation, and you need to argue why it is better than linear.
You have to do that in separate pdf file that you submit with the python file. The name of
the pdf file must be Question2Complexity.pdf. In case you are not solving the part about the
complexity, you do not have to submit the pdf file.

Helper Functions You can define as many additional functions as you want. Help functions
should start with an underscore. But all solutions should not exceed 500 lines. In fact, 500 lines
is extremely generous bound. You need much less than that.

Grading The assignments are graded by both hand and unit tests. That means the results
of the functions should be precisely as expected by the given unit tests. Check the test file
test assignment3.py on Blackboard. Do not edit that file except for the imports if needed.
In grading we do not only look at functionality, but we also take code quality (including comments
in the code) into account.

You might also like