Session
Session
Jean-Bernard Hayet
CIMAT, A.C.
1/ 51
Preparing the programming admission exam
2/ 51
The programming exam
3/ 51
The programming exam: Topics
https://pcc.cimat.mx/es/maestria-pcc/admision
4/ 51
Advice: preparation
5/ 51
Advice: exam
Be pragmatic.
6/ 51
Resources
https://www.hackerrank.com/
https://leetcode.com/
7/ 51
Handling matrices/arrays
8/ 51
Arrays/matrices
9/ 51
Arrays
10/ 51
Arrays/matrices
11/ 51
Arrays/matrices: index-based access
int A[]={1,3,4};
for ( int i =0;i<3;i++)
cout << A[i] << endl;
vector <int> B;
for ( int i =0;i<B.size (); i ++)
cout << B[i] << endl;
In Python:
A=[1,3,4]
for i in range( len (A)):
print (A[ i ])
12/ 51
Arrays/matrices: range-based access
In C++:
In Python:
for elem in A:
print (elem)
13/ 51
Arrays/matrices: multiple dimensions
14/ 51
Arrays/matrices: multiple dimensions
In C:
int A[ROWS][COLS];
for ( int i =0;i<ROWS;i++) {
for ( int j =0;j<COLS;j++)
print ("%d ",A[i ][ j ]);
print ("\n");
}
In Python:
A = [[1,2,3],[4,5,6]]
for i in range (2):
for j in range (3):
print (A[ i ][ j ])
15/ 51
Arrays/matrices: linear search
16/ 51
Practice: 2019 exam
Se suele representar una imagen numéricamente por una matriz de valores I tal que
I [x][y ] indique el valor de nivel de gris en la posición indicada por los índices x y y . Se
supondrá aquí que estos valores son enteros positivos encodicados en 8 bits (entre 0
y 255). Escribir una función que tome de entrada una matriz-imagen I cuadrada de
tamaño m × m y que le apliqué una rotación de 90 grados hacia la derecha (como
cuando uno rota la imagen en su celular).
Ejemplo: ante la ejecución de la función con la siguiente imagen
10 50 70 0
200 200 170 200
I=
0 45 120 156
2 78 89 20
la función debe devolver
2 0 200 10
78 45 200 50
I′ =
89 120 170 70
20 156 200 0
17/ 51
Algorithms
18/ 51
Algorithmic choices
19/ 51
Algorithmic choices
20/ 51
Algorithmic choices
21/ 51
Algorithmic choices: Example
Problem
Given the Mexican peso coins set (1,2,5,10), design an algo-
rithm to give the minimal number of coins for any amount of
change.
22/ 51
Algorithmic choices: Example
23/ 51
Algorithmic choices: Example
Be sure to:
24/ 51
Algorithmic choices: Example
Correctness
Since c1 = 1, you are sure there is always a solution.
We scan the coin values from the largest to the smallest and lls the
change with as much as possible of this coin.
Proof of correctness: You can check the property for N < 10 exhaustively
and prove that n1 ≤ 1, n2 ≤ 2, n5 ≤ 1, n1 + n2 ≤ 2 (by contradiction). Sup-
pose N ≥ 10. By contradiction, if we were not to use a 10 coin, then we
should be able to ll the change optimally with n1 , n2 , n5 . This is impossible
for N ≥ 10 because the maximum sum with n1 , n2 , n5 is 9.
Important: This argument works for this set of coins but not for any set of
coins (ex: {1, 5, 12} and N = 15).
25/ 51
Algorithmic choices: Example
Eciency
N N mod 10 N mod 5
+ + +N mod 2
10 5 2
(Simplication?)
26/ 51
Gentle Review of Data structures
27/ 51
Algorithms and data structures
28/ 51
Linear structures: Linked lists/arrays
29/ 51
Linear structures: Insertion/deletions
30/ 51
Linear structures: Memory
31/ 51
Linear structures: Memory
32/ 51
Simply linked lists
33/ 51
Simply linked lists: Nodes
In Python:
class NodeStruct:
def __init__(self, data):
self . data = data
self . next = None
34/ 51
Simply linked lists
In Python:
head = NodeStruct(0)
tail = head
for i in range (1,20):
insert ( tail , i )
tail = tail . next
35/ 51
Simply linked lists: Visit the list
In Python:
n = head
while n is not None:
print (n.data)
n = n.next
n.next = n.next.next
36/ 51
Simply linked lists: deletion
In C:
In Python:
37/ 51
Simply linked lists
[See code]
38/ 51
Abstract data types
39/ 51
Abstract data types: examples
Stack, queue.
Priority queue.
Set, multi-set.
Tree, Graph.
40/ 51
The stack collection
Linear data collection that works under the Last In First Out
(LIFO) principle.
41/ 51
The stack collection
push()
top
pop()
42/ 51
The stack collection
43/ 51
Linked list implementation
44/ 51
Linked list implementation
Linear data collection that works under the First In First Out
(FIFO) principle.
46/ 51
The queue collection
enqueue()
rear front
dequeue()
47/ 51
The queue collection
48/ 51
Fixed-capacity array implementation
# Stack size
def size ( self ) => int:
return self . rear = self . front +1
[See code]
50/ 51
Contact
51/ 51