DSEB65 Homework2
DSEB65 Homework2
Note: The bonus point questions might require some knowledge that has not been
introduced in class. You are not required to do them, so don’t spend too much time on
them.
Create a list containing the names of five countries you would like to visit. Print this list.
a) Add the name of a new country to the end of the list created in Exercise 1. Then
print the updated list.
b) Change the name of the first country in the list to another country.
c) Remove the last country from the list. Print the update list
d) Sort the list of countries in alphabetical order. Then reverse the order of the list.
Create a nested list where each sublist contains a country and its corresponding cities (at
least three cities per country). Write a program that prints each country and its cities in
the following format:
Exercise 4 (2 point):
a) Concatenate Two Lists Index-Wise Given two lists: list1 = ["M", "na", "i", "Ke"];
list2 = ["y", "me", "s", "lly"]. Create a new list by concatenating elements from
both lists based on their indices.
20 300
30 200
40 100
c) Insert an Item into Nested List Given a nested list: list1 = [10, 20, [300, 400,
[5000, 6000], 500], 30, 40]. Insert the number 7000 after 6000 in the nested list.
Expected output: [10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40]
d) Extend Nested List Given a nested list: list1 = ["a", "b", ["c", ["d", "e", ["f", "g"],
"k"], "l"], "m", "n"]. Extend the list with the sub-list ["h", "i", "j"] so that it looks
like the given output.
Expected output: ['a', 'b', ['c', ['d', 'e', ['f', 'g', 'h', 'i', 'j'], 'k'], 'l'], 'm', 'n']
e) Remove All Occurrences of x from List Given a list: list1 = [5, 20, 15, 20, 25, 50,
20]. Remove all occurrences of the value 20 from the list.
∫ 1x dx
2
Exercise 7* (1 point): You are given an equilateral triangular array of numbers. Starting
from the top, you need to find the path with the minimum sum to reach the bottom. In
each move, you can only either move to the left or right adjacent number of the row
below. Formally, you can only move from arr[i][j] to arr[i+1][j] or arr[i+1][j+1]. Return
the minimum sum of your chosen path. (Bonus 0.5 point if you can print the chosen path)
Example:
Input: [[2],
[3, 4],
[6, 5, 8]],
Output: Minimum path: 2 –> 3 –> 5, Sum = 10
Exercise 8 (1 point): You are given two 2D arrays (matrices) A and B, where A is of
size m x n and B is of size n x p, perform matrix multiplication and return the resulting
matrix C of size m x p. (You are not allowed to use numpy) (Bonus 1 point if you can do
the multiplication in parallel).
See matrix multiplication rule (https://en.wikipedia.org/wiki/Matrix_multiplication)
A matrix can be presented in a nested list
A (2x3) = [[1,2,3],
[4,5,6]]