PY-03: Sequences and loops
Read the task files below or download the starter files. Use the Canvas assignment to open CodeGrade and submit your work.
Sequences and loops
Complete the functions in the downloaded files for offline practice. Work through the files in the order below. Each file states its inputs, output, rules, and examples.
Table 1 lists the exercises in this practical.
| File | Task |
|---|---|
| sum_even_numbers.py | Add even numbers through a limit. |
| multiples_of_n.py | Produce a sequence of multiples. |
| factorial.py | Build a product through repeated multiplication. |
| fibonacci_sequence.py | Build a sequence from its previous two values. |
| is_sorted.py | Check adjacent values in a list. |
| password_checker.py | Repeat a request until an input passes a rule. |
Replace the NotImplementedError line in each file with your code. Keep the function name and parameters. Use a loop as each task requests.
For graded submissions, open the Canvas assignment and edit the files already supplied in CodeGrade. Match the exercises by their function names and keep the filenames supplied in that editor.
Select Auto Test, then Run AutoTest to check your work. Read the feedback, revise the relevant file, and run it again. AutoTest does not create a submission. Select Hand-in your work to submit your work before the Canvas due date.
Each question counts as complete when all its checks pass. The tests include cases beyond the examples.
Python files
sum_even_numbers.py
Listing 1 shows the contents of sum_even_numbers.py.
sum_even_numbers.py
def sum_even_numbers(limit: int) -> int:
"""
Calculate the sum of even numbers up to a given limit.
Iterate through numbers from 2 to the limit (inclusive) and
add the even numbers to the total. Use a loop to accumulate the sum.
Parameters
----------
limit : int
The upper limit for calculating the sum of even numbers (inclusive).
Returns
-------
int
The sum of even numbers up to the limit, or 0 if the limit is below 2.
Examples
--------
>>> sum_even_numbers(10)
30
>>> sum_even_numbers(3)
2
"""
raise NotImplementedError("Replace this line with your code.")multiples_of_n.py
Listing 2 shows the contents of multiples_of_n.py.
multiples_of_n.py
def get_multiples_of_n(n: int, limit: int) -> str:
"""
Returns a string with multiples of a given number up to a specified limit.
Use a loop to collect positive multiples, starting at n. Separate them
with a comma and one space, without a trailing separator.
Parameters
----------
n : int
The positive number whose multiples are to be collected.
limit : int
The upper limit for the multiples (inclusive).
Returns
-------
str
String with multiples of n, or an empty string if limit is below n.
Examples
--------
>>> get_multiples_of_n(3, 10)
'3, 6, 9'
>>> get_multiples_of_n(2, 4)
'2, 4'
"""
raise NotImplementedError("Replace this line with your code.")factorial.py
Listing 3 shows the contents of factorial.py.
factorial.py
def factorial(n: int) -> int:
"""
Calculate the factorial of a number.
Use a loop to multiply the integers from 1 through n.
Parameters
----------
n : int
The nonnegative number to calculate the factorial of.
Returns
-------
int
The factorial of 'n'. The factorial of 0 is 1.
Examples
--------
>>> factorial(5)
120
>>> factorial(0)
1
"""
raise NotImplementedError("Replace this line with your code.")fibonacci_sequence.py
Download fibonacci_sequence.py.
Listing 4 shows the contents of fibonacci_sequence.py.
fibonacci_sequence.py
def fibonacci_sequence(n: int) -> str:
"""
Generate the first n terms of the Fibonacci sequence.
The sequence begins with 0, 1. Each later term is the sum of the
previous two; use a loop to generate those terms.
Parameters
----------
n : int
The nonnegative number of terms in the Fibonacci sequence to generate.
Returns
-------
str
The terms separated by a comma and one space, or an empty string
when n is 0.
Examples
--------
>>> fibonacci_sequence(5)
'0, 1, 1, 2, 3'
>>> fibonacci_sequence(10)
'0, 1, 1, 2, 3, 5, 8, 13, 21, 34'
>>> fibonacci_sequence(0)
''
>>> fibonacci_sequence(1)
'0'
"""
raise NotImplementedError("Replace this line with your code.")is_sorted.py
Listing 5 shows the contents of is_sorted.py.
is_sorted.py
def is_sorted(lst: list) -> bool:
"""
Check if the list is sorted in ascending order.
Use a loop to compare adjacent items. Equal neighbours are allowed.
Parameters
----------
lst : list
The list to check. Its items must be comparable with <. Do not
change the list.
Returns
-------
bool
True if the list is sorted, False otherwise. Empty and one-item
lists are sorted.
Examples
--------
>>> is_sorted([1, 2, 3])
True
>>> is_sorted([3, 2, 1])
False
>>> is_sorted([1, 2, 2, 3])
True
"""
raise NotImplementedError("Replace this line with your code.")password_checker.py
Listing 6 shows the contents of password_checker.py.
password_checker.py
def password_checker() -> str:
"""
Check password validity.
The function prompts the user to enter a password and checks if it
meets a length requirement (at least 8 characters).
If it does, the function returns the password.
Otherwise, the user is prompted to enter a new password, until a valid
password (i.e., at least 8 characters long) is entered.
Use a while loop and call input('Enter your password: ') each time.
Every character, including spaces, counts toward the length.
Returns
-------
str
The valid password.
Examples
--------
>>> password_checker()
Enter your password: 1234567
Enter your password: 12345678
12345678
"""
raise NotImplementedError("Replace this line with your code.")