0% found this document useful (0 votes)
8 views3 pages

DSEB65 Homework2

Python for DS

Uploaded by

Hoà Ngọc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

DSEB65 Homework2

Python for DS

Uploaded by

Hoà Ngọc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Homework 2

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.

Exercise 1 (1 point): Create a List

Create a list containing the names of five countries you would like to visit. Print this list.

Exercise 2 (2 point): Add, remove and modify

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.

Exercise 3 (1 point): Nested Lists and For Loops

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:

Country: Japan Cities: Tokyo, Osaka, Kyoto

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.

Expected output: ['My', 'name', 'is', 'Kelly']


b) Iterate Two Lists in Reverse Order Given two lists: list1 = [10, 20, 30, 40]; list2 =
[100, 200, 300, 400]. Print each element from list1 in its original order and each
element from list2 in reverse order.

Expected output: 10 400

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.

Exercise 5 (1 point): Approximate π Using Infinite Series The value of π can be


approximated using the following infinite series:

Write a program that displays 15 approximations of π using this series. Each


approximation should include one more term in the series, making it a better
approximation than the previous one.
Exercise 6 (1 point): Approximate Integral Using Numerical Methods Approximating
the following integral:
1

∫ 1x dx
2

a) By The Trapezoidal Rule (see https://en.wikipedia.org/wiki/Trapezoidal_rule).

b) By Simpson's rule (see https://en.wikipedia.org/wiki/Simpson%27s_rule).

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]]

You might also like