Lecture 6 bis : Additional exercices

December, 2021 - François HU

Master of Science - EPITA

This lecture is available here: https://curiousml.github.io/

image.png

Exercices

These exercices are considered "normal-hard" exercices.

Exercice 1: lists

Create a function insert, that produces the following results:

>>> l = [0, 9, 3, 10]
>>> print(insert(l, -1, 1))
[0, -1, 9, 3, 10]
>>> print(insert(l, -1, 2))
[0, 9, -1, 3, 10]
>>> print(insert(l, "epita", 4))
[0, 9, 3, 10, 'epita']
>>> print(insert(l, "epita", 10))
[0, 9, 3, 10, 'epita']
>>> print(l)
[0, 9, 3, 10]

Exercice 2: dictionaries

we have the following list of dictionaries

  1. write a script that add a field Rounded_pop which discard the last 3 digits. For example we transform the value 34576 to 34
  1. write a script that store the values of Rounded_pop into a list named populations
  1. sort by ascending order the list populations

Exercice 3: strings

we have the following list of strings

Write a script that create a variable named text which concatenante the items of list_of_strings with exactly one space between words.

Exercice 4: arrays

  1. Generate the following array without explicit loops and without manually fill in the numbers. We will name it arr1:
[[  0  25 100 225 400]
 [  1  36 121 256 441]
 [  4  49 144 289 484]
 [  9  64 169 324 529]
 [ 16  81 196 361 576]]
  1. Extract the diagonal values of arr1 (do not generate it) and call it diag1
[  0,  36, 144, 324, 576]
  1. From arr1, extract the submatrix:
[[225 100  25]
 [289 144  49]
 [256 121  36]]

and name it subarr1.

Exercice 5: arrays

  1. Generate the following array without explicit loops and without manually fill in the numbers. We will name it arr2:
[[10  9  8  7  6]
 [ 9  8  7  6  5]
 [ 8  7  6  5  4]
 [ 7  6  5  4  3]
 [ 6  5  4  3  2]]
  1. Replace all the even values of arr2 by 0 without explicit loops. You should have:
    [[0 9 0 7 0]
    [9 0 7 0 5]
    [0 7 0 5 0]
    [7 0 5 0 3]
    [0 5 0 3 0]]

Exercice 6: plotting

  1. Generate and (scatter) plot 2000 random points in the space $[−10,0]×[1,2]$ with the color orange. You should have:

image-2.png

  1. color the points inside the space $[−4,-2]×[1.2,1.8]$ or $[−8,-6]×[1.2,1.8]$ by green. You should have: image.png

Exercice 7: data manipulation

  1. Import the Iris and defra_consumptiondatasets as dataframes and name them respectively iris and cons. You should have the following first 5 rows for each dataframe:

image.png

image-2.png

  1. For the dataframe iris (aside from the column species):
    • discard the Cm at the end of each column name
    • convert values to minimeter (instead of centimiter)
  1. For the dataframe cons, convert columns to percentages of the totals. Therefore the first row should be:

image.png