Assignment 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CSEN 2304 Introduction to Computer Science

Spring 2024

Assignment 4

Due date: April 22

This assignment consists of five individual sub-assignments that provide practical experience with the
concepts and language constructs that have been covered to date (especially functions, strings, and
lists). To complete the assignment you will need to write five separate Python programs that perform
the tasks specified in the problem descriptions below. Make sure to include appropriate comments in
your source code to document your program. Those comments should include headings at the start of
your code with your name, K-number, and the date submitted. Also remember to follow the standard
Python coding conventions discussed in class. Turn in your solutions by submitting a zip file that
contains your five Python programs via the corresponding Blackboard system link for the assignment.

Remember that as discussed in class, after coding each function you should test it by invoking the
function from the main program part of your Python file. Make sure to test your functions thoroughly
with a wide enough range of inputs to ensure they work with all argument values they might be sent.

1. Write a Python function that accepts a list of strings as an argument (list_1). The function
should process the list of strings to determine which list element is the longest string (made up
of the most characters). The longest string found in the list should be sent back as a return
value to the calling code. For example, if the list [‘Cat’, ‘dog’, ‘Hello Python’, ‘zebra’, ‘abcd’] was
sent as an argument to the function, the string ‘Hello Python’ should be sent back as a return
value to the calling code. Make sure to test your function from the main program of your
Python file and print the result to ensure it works correctly.

2. Write a Python function to accept a list of strings as an argument. The list should be processed
by reversing the characters of each of the individual string values it contains. Once all of the
string elements of the list are reversed, it should be sent back as a return value to the calling
code. For example, if the list [‘abc’, ‘def’, ‘Hello’, ‘xyz’] was sent as an argument to the function,
after processing the following list of reversed string values should be sent back as a return value
to the calling code [‘cba’, ‘fed’, ‘olleH’, ‘zyx’]. Make sure to test your function from the main
program of your Python file and print the result to ensure it works correctly.

3. Write a Python function that accepts two arguments: a list of integer values (list_1) and a single
integer value (x). The function should process the list by checking each of its values in turn to
determine if it is a multiple of the integer value “x”. Each integer value from list_1 that is a
multiple of the integer “x” should be added to a new list (list_2). Once all the values of list_1
have been processed, the new list (list_2) containing all of the multiples of “x” that were found
should be sent as a return value to the calling code in the main program. For example, if the list
[12, 9, 20, 7, 8, 16] and the integer value 4 were sent as arguments to the function, the new list
[12, 20, 8, 16] should be sent back as a return value to the calling code as those are the values of
the original list that are multiples of 4. Make sure to test your function from the main program
of your Python file and print the result to ensure it works correctly.

4. Write a Python function called interleave that accepts two lists as arguments (list_1 and list_2).
The two lists sent as arguments to the function should consist of the same number of elements.
The function should create a new list (list_3) that results from the interleaving of list_1 and
list_2. To interleave list_1 and list_2, the function should set the first element of list_3 equal to
the first element of list_1. Next it should set the second element of list_3 equal to the first
element of list_2, the third element of list_3 equal to the second element of list_1, the fourth
element of list_3 equal to the second element of list_2, etc. So, for example, if the lists received
by the function are: list_1 = [‘a’, ‘c’, ‘e’, ‘g’, ‘i’] and list_2 = [‘b’, ‘d’, ‘f’, ‘h’, ‘j’], they should be
interleaved to form list_3 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’], and list_3 should be sent back as
a return value to the calling code. Make sure to test your function from the main program of
your Python file and print the result to ensure it works correctly. As always, it is a good idea to
run multiple tests to ensure the function works for all lists it might receive as arguments.

5. Write a Python function that accepts a list of integer lists as an argument. The function should
process the list of integer lists by transforming it into a list of sorted integer lists. To do so the
function should sort each of its list elements (the individual integer lists) into ascending order.
Once the individual integer list elements have all been sorted, the list of sorted integer lists
should be sent back as a return value to the calling code. For example, if the list of integer lists
[[4, 9, 2], [10, 5, 3, 9], [22, 11, 19, 5], [1, 2, 3]] was sent as an argument to the function, the list
of sorted integer lists [[2, 4, 9], [3, 5, 9, 10], [5, 11, 19, 22], [1, 2, 3]] should be sent back as a
return value to the calling code. Make sure to test your function from the main program of your
Python file and print the result to ensure it works correctly.

You might also like