Lecture 2 : Application

November, 2021 - François HU

Master of Science in Artificial Intelligence Systems - EPITA

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

image.png

Table of contents

Lecture 1: correction and remarks

Graded project

Correction and feedback

You will find bellow the correction of the exercices of the first lecture.

Exercice 1.

Create the following list with loops:

[['car', 0, 1, 4, 9, 16],
 ['bus', 1, 4, 9, 16, 25],
 ['train', 4, 9, 16, 25, 36],
 ['boat', 9, 16, 25, 36, 49]]

Create a script that prints this list in the following manner:

car      0    1    4    9   16
bus      1    4    9   16   25
train    4    9   16   25   36
boat     9    16  25   36   49

Common mistake 1

Instead of generating the list with for loops, many of you gave me the answer:

Common mistake 2

some of you gave me four independant lists instead of a single list (of lists)

Common mistake 3

Solution proposed by a student 1

Solution proposed by a student 2

Suggested correction

Another way to print

Exercice 2.

For the following list of dictionaries, write a script that add a field registrations which is twice the number of accepted papers.

Common mistake 1

confs needs some clarification . This is a list of dictionaries. More precisely it is one list containing four dictionaries and each dictionary contains four fields (or keys) : "Name", "Date", "Location" and "acc_papers" -> Here in each dictionary of the list we want to add fifth field named registration which the value is twice the number of accepted paper.

Suggested correction

Exercice 3.

Write a function include, that produces the following results:

>>> l = [0]
>>> include(l, 2)
>>> print(l)
[0, 2]
>>> include(l, "-1")
>>> print(l)
[0, 2, '-1']
>>> include(l)
>>> print(l)
[]

Let us clarify this exercice: Our objective is to create a function that:

Common mistake 1

Common mistake 2

Common mistake 3

Suggested correction

Or alternatively

Exercice 4.

Create a module, that contains a function fibonacci(n) computing the $n^{th}$ Fibonacci number. Then create a script using this function.

Common mistake 1

Functions of a module should be in a python file (extension .py) not in a ipython notebook file (extension .ipynb)

image-3.png

Common mistake 2

Common mistake 3

Suggested correction

Exercice 5.

Change the class Point (the toy example bellow) in order to take account a 3rd coordinate z.

class Point:
    def __init__(self, x, y):          
        self.x = x                     
        self.y = y                     
        self.norm = self.compute_norm()

    def compute_norm(self):
        norm = (self.x**2 + self.y**2)**(0.5) 
        return norm

    def __add__(self, another_point) :
         return Point(self.x + another_point.x, self.y + another_point.y)

Then execute the following script.

p1 = Point(1, -1, 1)
p2 = Point(1, 1, 3)
p3 = p1 + p2

print(f"p1 = ({p1.x}, {p1.y}, {p1.z})")
print(f"p2 = ({p2.x}, {p2.y}, {p2.z})")
print(f"p3 = ({p3.x}, {p3.y}, {p3.z})")

Suggested correction

Project: sudoku

Objective : solve a problem with nested functions. The project this year is a sudoku game. Citation wikipedia about the sudoku game

the objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 subgrids that compose the grid (a.k.a. "boxes", "blocks", or "regions") contain all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a single solution.

Your 9x9 sudoku will have the following form (0 corresponds to an empty cell)

step 1.

Define a function row_unused_digit(grid, valid_digit, i) that updates the list valid_digit so that the function checks for each column k in row i of the sudoku grid whether the digit grid[i][k] is not zero. valid_digit[k] == 0 means that k+1 is already taken in row i.

step 2.

Define a function column_unused_digit(grid, valid_digit, j) that updates the list valid_digit so that the function checks for each row k in column j of the sudoku grid whether the digit grid[k][j] is not zero. valid_digit[k] == 0 means that k+1 is already taken in column j.

step 3.

Define a function block_unused_digit(grid, valid_digit, i, j) that updates the list valid_digit so that the function checks whether for each digit in the block including row i and column j is not zero. valid_digit[k] == 0 means that k+1 is already taken in the block containing (i, j).

step 4.

Define a function possible_number(grid, i, j) that returns, for a certain position grid[i][j], the list of possible numbers

step 5.

Define a function best_cell(grid) that looks at all the empty cells of the sudoku grid and chooses the cell with the least number of options. You should have the following results:

>>> best_cell(grid_wiki)
(4, 4, [0, 0, 0, 0, 1, 0, 0, 0, 0])

step 6.

define a function solve_sudoku(grid) which solves the sudoku. This function returns 0 if the sudoku is impossible to solve (1 otherwise). it should do the following steps:

step 7.

Define a function sudoku2str(grid) that converts a sudoku grid into a string. Desired result for the sudoku grid_wiki:

>>> print(sudoku2str(grid_wiki))
>>>
------------------------------
 | _ _ _  | 3 _ _  | 8 _ _  |
 | _ _ 7  | 9 _ 8  | _ _ 5  |
 | _ _ _  | 2 _ 4  | 1 _ _  |
------------------------------
 | _ 9 _  | 8 1 _  | _ 4 7  |
 | _ 4 _  | _ _ _  | _ _ 6  |
 | _ _ _  | _ _ _  | _ _ _  |
------------------------------
 | _ 1 _  | _ _ 5  | _ 2 _  |
 | 5 3 4  | _ _ _  | _ _ _  |
 | _ _ _  | 7 _ _  | _ _ _  |
------------------------------

step 8.

Solve the soduku grid_wiki and print the solution grid. You should have th following results

>>> solve_sudoku(grid_wiki)
>>> print(sudoku2str(grid_wiki))
------------------------------
 | 5 3 4  | 6 7 8  | 9 1 2  |
 | 6 7 2  | 1 9 5  | 3 4 8  |
 | 1 9 8  | 3 4 2  | 5 6 7  |
------------------------------
 | 8 5 9  | 7 6 1  | 4 2 3  |
 | 4 2 6  | 8 5 3  | 7 9 1  |
 | 7 1 3  | 9 2 4  | 8 5 6  |
------------------------------
 | 9 6 1  | 5 3 7  | 2 8 4  |
 | 2 8 7  | 4 1 9  | 6 3 5  |
 | 3 4 5  | 2 8 6  | 1 7 9  |
------------------------------