Lecture 2 : Application

November, 2021 - updated on April, 2022 - François HU

Master of Science - EPITA

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

image.png

Table of contents

Exercice 1: lists

  1. Create a list list_ which holds the values $[1, 4, 9, 16, 25, \dots, 400]$. Example:

    >>> list_[0]
    1
    >>> list_[10]
    121
    >>> list_[-1]
    400
  2. Create a function inter_in(l, i, j) that swaps the values in position i and j of the list l.

Remark: no need for the function to return an object !

Example:

>>> inter_in(list_, 0, 10)
>>> list_[0]
121
>>> list_[10]
1
>>> inter_in(list_, 0, 10)
>>> list_[0]
1
>>> list_[10]
121
  1. Create a function inter_out(l, i, j) that returns a copy of the list l in which the values in positions i and j are swapped. Example:
    >>> clist = inter_out(list_, 0, 10)
    >>> clist[0]
    121
    >>> clist[10]
    1
    >>> list_[0]
    1
    >>> list_[10]
    121
    Hint: Use the method copy of the built-in object list (lists are mutable).

Exercice 2: dictionaries

We have the following dictionary which contains students grade (don't forget to execute the cell):

The teacher wants to reajust the grades and decides to add 5 points to the grade of each student. For that purpose, follow the following steps:

  1. Append and add by 5 each element of the list students['Grade'] in a new list named ugrade
  1. In the dictionary students, add a new field named Ugrade with ugrade as an item.

Even after the reajustment, some students have less than 10 points so therefore they need to do a rattrapage examination.

  1. Create a new dictionary named retake_examination which contains the Name, Grade and Ugrade of the students who need to retake the examination.

Exercice 3: strings

  1. Create a function signature(text) which returns the string text with " EPITA." at the end. Remark: be careful, we have a space and . in the signature.

Example:

>>> signature('Hello world!')
'Hello world! EPITA.'
>>> signature("Don't be late for the test.")
"Don't be late for the test. EPITA."
  1. Create a function delete_signature(text) which returns the string text with " EPITA." at the end deleted. If the substring " EPITA." does not exist, then do nothing. Example:
    >>> delete_signature('Hello world! EPITA.')
    'Hello world!'
    >>> delete_signature("Don't be late for the test.")
    "Don't be late for the test."
    >>> delete_signature('Hello world! EPITA')
    'Hello world! EPITA'
    Hints: a string is a container where: