This commit is contained in:
parent
2fda876df3
commit
35f194f649
67
README.md
67
README.md
@ -12,24 +12,24 @@ Voici mes réponses pour l'examen 2025 d'_Algorithmes et Structures de données_
|
|||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>But</strong></td>
|
<td><strong>But</strong></td>
|
||||||
<td>...</td>
|
<td>Compter le nombre de valeurs positives différentes ayant leur opposé dans une liste</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Input</strong></td>
|
<td><strong>Input</strong></td>
|
||||||
<td>...</td>
|
<td>La liste des demi-touches (entiers relatifs non-nuls)</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Output</strong></td>
|
<td><strong>Output</strong></td>
|
||||||
<td>...</td>
|
<td>Le nombre de touches distinctes dont les valeurs positives et négatives sont dans la liste</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Signature</strong></td>
|
<td><strong>Signature</strong></td>
|
||||||
<td>
|
<td>
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def function1(
|
def countKey(
|
||||||
*args
|
pieces: list[int]
|
||||||
) -> Any:
|
) -> int:
|
||||||
```
|
```
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
@ -46,24 +46,26 @@ def function1(
|
|||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>But</strong></td>
|
<td><strong>But</strong></td>
|
||||||
<td>...</td>
|
<td>Trouver le chemin le plus court entre deux points, en considérant que certaines routes ne sont praticables que de nuit/jour</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Input</strong></td>
|
<td><strong>Input</strong></td>
|
||||||
<td>...</td>
|
<td>Nœud de départ (index), nœud d'arrivée (index) et liste des routes / arêtes (nœud 1, nœud 2, restriction)</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Output</strong></td>
|
<td><strong>Output</strong></td>
|
||||||
<td>...</td>
|
<td>Liste des nœuds (index) à parcourir</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Signature</strong></td>
|
<td><strong>Signature</strong></td>
|
||||||
<td>
|
<td>
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def function2(
|
def findSafestPath(
|
||||||
*args
|
start: int,
|
||||||
) -> Any:
|
end: int,
|
||||||
|
intersections: list[tuple[int, int, int]]
|
||||||
|
) -> list[int]:
|
||||||
```
|
```
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
@ -80,24 +82,25 @@ def function2(
|
|||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>But</strong></td>
|
<td><strong>But</strong></td>
|
||||||
<td>...</td>
|
<td>Trouver dans un réseau de console un sous-graphe connexe de taille donnée</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Input</strong></td>
|
<td><strong>Input</strong></td>
|
||||||
<td>...</td>
|
<td>Taille n du sous-graphe recherché, liste des connexions</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Output</strong></td>
|
<td><strong>Output</strong></td>
|
||||||
<td>...</td>
|
<td>Liste de n consoles toutes interconnectées</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Signature</strong></td>
|
<td><strong>Signature</strong></td>
|
||||||
<td>
|
<td>
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def function3(
|
def findTightlyLinkedConsoles(
|
||||||
*args
|
n: int,
|
||||||
) -> Any:
|
consoles: list[tuple[int, int]]
|
||||||
|
) -> list[int]:
|
||||||
```
|
```
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
@ -114,24 +117,26 @@ def function3(
|
|||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>But</strong></td>
|
<td><strong>But</strong></td>
|
||||||
<td>...</td>
|
<td>Compter le nombre de séquences de N coups infligeant un total de H dégâts</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Input</strong></td>
|
<td><strong>Input</strong></td>
|
||||||
<td>...</td>
|
<td>Nombre N de coups dans la séquence, nombre C de coups possibles (dégâts 1 à C inclus), total H de dégâts</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Output</strong></td>
|
<td><strong>Output</strong></td>
|
||||||
<td>...</td>
|
<td>Nombre de séquences possibles</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Signature</strong></td>
|
<td><strong>Signature</strong></td>
|
||||||
<td>
|
<td>
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def function4(
|
def computeNbrOfDifferentSequences(
|
||||||
*args
|
N: int,
|
||||||
) -> Any:
|
C: int,
|
||||||
|
H: int
|
||||||
|
) -> int:
|
||||||
```
|
```
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
@ -148,24 +153,26 @@ def function4(
|
|||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>But</strong></td>
|
<td><strong>But</strong></td>
|
||||||
<td>...</td>
|
<td>Trouver les placements de pièces Minitris formant un rectangle plein</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Input</strong></td>
|
<td><strong>Input</strong></td>
|
||||||
<td>...</td>
|
<td>Largeur de la grille, longueur de la grille, liste des pièces à poser</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Output</strong></td>
|
<td><strong>Output</strong></td>
|
||||||
<td>...</td>
|
<td>Liste des positions des pièces</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>Signature</strong></td>
|
<td><strong>Signature</strong></td>
|
||||||
<td>
|
<td>
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def function5(
|
def playMinitrisFastAndWell(
|
||||||
*args
|
board_width: int,
|
||||||
) -> Any:
|
board_height: int,
|
||||||
|
pieces: list[tuple[tuple[int,int], tuple[int,int]]]
|
||||||
|
) -> list[list[int, int]]:
|
||||||
```
|
```
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
|
@ -28,7 +28,7 @@ list = []
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def countKey(pieces: list[int])->int:
|
def countKey(pieces: list[int]) -> int:
|
||||||
pieces = sorted(pieces)
|
pieces = sorted(pieces)
|
||||||
complete: int = 0
|
complete: int = 0
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ point d'arrivée, tel que les routes marquées 1 ne soient parcourues que de jou
|
|||||||
seulement de nuit (index impair). En plus des routes données, nous pouvons aussi
|
seulement de nuit (index impair). En plus des routes données, nous pouvons aussi
|
||||||
rester sur un même nœud pour une nuit.
|
rester sur un même nœud pour une nuit.
|
||||||
Afin de bien gérer les différences d'états entre jour et nuit, nous pouvons intégrer le demi-jour
|
Afin de bien gérer les différences d'états entre jour et nuit, nous pouvons intégrer le demi-jour
|
||||||
associé à chaque visite de nœud. Ainsi, une même intersection peut avoir deux nœud:
|
associé à chaque visite de nœud. Ainsi, une même intersection peut avoir deux nœuds :
|
||||||
un pour une visite de jour, et un de nuit.
|
un pour une visite de jour, et un de nuit.
|
||||||
|
|
||||||
Pour résoudre ce problème, comme nous ne pouvons pas établir d'heuristique mesurant
|
Pour résoudre ce problème, comme nous ne pouvons pas établir d'heuristique mesurant
|
||||||
@ -93,4 +93,4 @@ def findSafestPath(start: int, end: int, intersections: list[tuple[int, int, int
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(findSafestPath(0,2,[(0, 1, -1), (1, 2, 0)]) == [0, 0, 1, 2])
|
print(findSafestPath(0,2,[(0, 1, -1), (1, 2, 0)]) == [0, 0, 1, 2])
|
||||||
print(findSafestPath(0, 5, [(0,1,0), (0,2,1), (2,1,-1), (1,3,-1), (2,4,-1), (3,5,1), (3,4,0), (4,5,-1)]) == [0, 1, 3, 5])
|
print(findSafestPath(0, 5, [(0, 1, 0), (0, 2, 1), (2, 1, -1), (1, 3, -1), (2, 4, -1), (3, 5, 1), (3, 4, 0), (4, 5, -1)]) == [0, 1, 3, 5])
|
||||||
|
14
src/Ex3.py
14
src/Ex3.py
@ -6,7 +6,7 @@ Explications:
|
|||||||
Comme indiqué dans la donnée de l'exercice, il s'agit ici de trouvé un sous-graphe
|
Comme indiqué dans la donnée de l'exercice, il s'agit ici de trouvé un sous-graphe
|
||||||
connexe dans un graphe quelconque de consoles interconnectées
|
connexe dans un graphe quelconque de consoles interconnectées
|
||||||
|
|
||||||
Pour ce faire nous pouvons procéder ainsi:
|
Pour ce faire, nous pouvons procéder ainsi :
|
||||||
Étape 1 :
|
Étape 1 :
|
||||||
- Compter le nombre de voisins de chaque nœud (console)
|
- Compter le nombre de voisins de chaque nœud (console)
|
||||||
- Éliminer ceux ayant moins de voisins que la taille de sous-graphe recherché
|
- Éliminer ceux ayant moins de voisins que la taille de sous-graphe recherché
|
||||||
@ -20,10 +20,10 @@ Pour ce faire nous pouvons procéder ainsi:
|
|||||||
Étape 2 :
|
Étape 2 :
|
||||||
- Pour chaque nœud du graphe :
|
- Pour chaque nœud du graphe :
|
||||||
- Calculer l'ensemble des nœuds communs entre ses voisins (et lui-même),
|
- Calculer l'ensemble des nœuds communs entre ses voisins (et lui-même),
|
||||||
et les voisins des ses voisins (et eux-mêmes)'
|
et les voisins de ses voisins (et eux-mêmes)'
|
||||||
C'est-à-dire, si N1 et un nœuds et nb(N1) = {N2, N3, ...} est l'ensemble de ses voisins,
|
C'est-à-dire, si N1 est un nœud et nb(N1) = {N2, N3, ...} est l'ensemble de ses voisins,
|
||||||
on cherche l'intersection de {N1} U nb(N1), {N2} U nb(N2), {N3} U nb(N3), etc.
|
on cherche l'intersection de {N1} U nb(N1), {N2} U nb(N2), {N3} U nb(N3), etc.
|
||||||
- Si c'est ensemble contient au moins n éléments, il s'agit alors d'un sous-graphe connexe.
|
- Si cet ensemble contient au moins n éléments, il s'agit alors d'un sous-graphe connexe.
|
||||||
On peut ainsi en extraire les n premiers nœuds comme résultat
|
On peut ainsi en extraire les n premiers nœuds comme résultat
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -71,6 +71,6 @@ def findTightlyLinkedConsoles(n: int, consoles: list[tuple[int, int]]) -> list[i
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(findTightlyLinkedConsoles(3,[(0,1),(0,4),(2,1),(3,1),(4,2),(2,3)]) == [1,2,3])
|
print(findTightlyLinkedConsoles(3, [(0, 1), (0, 4), (2, 1), (3, 1), (4, 2), (2, 3)]) == [1, 2, 3])
|
||||||
print(findTightlyLinkedConsoles(4,[(0,1),(0,4),(2,1),(3,1),(4,2),(2,3)]) == [])
|
print(findTightlyLinkedConsoles(4, [(0, 1), (0, 4), (2, 1), (3, 1), (4, 2), (2, 3)]) == [])
|
||||||
print(findTightlyLinkedConsoles(4,[(0,1),(0,4),(2,1),(3,1),(4,2),(2,3),(1,4),(4,3)]) == [1,2,3,4])
|
print(findTightlyLinkedConsoles(4, [(0, 1), (0, 4), (2, 1), (3, 1), (4, 2), (2, 3), (1, 4), (4, 3)]) == [1, 2, 3, 4])
|
||||||
|
@ -14,7 +14,7 @@ le calcul récursif puisque cela évite de calculer les états partagés (sous-b
|
|||||||
identiques)
|
identiques)
|
||||||
|
|
||||||
Bonus :
|
Bonus :
|
||||||
Nous cherchons la valeur N la plus petite telle que:
|
Nous cherchons la valeur N la plus petite telle que :
|
||||||
`computeNbrOfDifferentSequences(N, 5, 100) > 2^16`
|
`computeNbrOfDifferentSequences(N, 5, 100) > 2^16`
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -72,4 +72,4 @@ def bonus():
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(computeNbrOfDifferentSequences(1, 6, 3) == 1)
|
print(computeNbrOfDifferentSequences(1, 6, 3) == 1)
|
||||||
print(computeNbrOfDifferentSequences(2, 6, 7) == 6)
|
print(computeNbrOfDifferentSequences(2, 6, 7) == 6)
|
||||||
print(bonus())
|
#print(bonus())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user