nsi:tds:robot_rebondissant
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| nsi:tds:robot_rebondissant [2021/12/05 17:13] – créée goupillwiki | nsi:tds:robot_rebondissant [2024/01/25 13:50] (Version actuelle) – goupillwiki | ||
|---|---|---|---|
| Ligne 41: | Ligne 41: | ||
| La fonction ouvre le fichier '' | La fonction ouvre le fichier '' | ||
| + | ===== Aide ===== | ||
| + | === Ouvrir le fichier === | ||
| + | <code python> | ||
| + | f = open(filename, | ||
| + | content = f.read() | ||
| + | f.close() | ||
| + | lines = content.split(' | ||
| + | </ | ||
| + | Suite à ce code, '' | ||
| + | < | ||
| + | [' | ||
| + | </ | ||
| - | For several years now, in elementary schools, we have seen the emergence of a new educational model, playful programming. The students must program a small robot using assembly blocks. This allows them to get familiar with programming from an early age while exercising their logic and perception of space. | + | Vous remarquez que la dernière ligne est vide. Cela peut arriver mais n'est pas certain. Cela dépend de comment à été écrit le fichier. On peut s' |
| - | You are a student at one such school. The purpose of the exercise is simple: your teacher has crafted a circuit for your robot, told you how many moves n the robot may make, and you must find out the final position of the robot at end of execution. | + | <code python> |
| + | while lines[-1] == '' | ||
| + | lines.pop() | ||
| + | </ | ||
| - | To do this you need to know some principles of robot operation. | + | La fonction `pop`a pour effet d' |
| - | – When the robot encounters an obstacle | + | |
| - | – The robot initially moves upwards. | + | === Accéder à un item === |
| - | – The robot stops after n moves. | + | |
| - | – The top left corner represents the coordinates | + | Maintenant que nous disposons de '' |
| - | – The robot's environment is represented as follows, where O is the robot's initial | + | |
| + | <code python> | ||
| + | >>> | ||
| + | ' | ||
| + | </ | ||
| + | En effet, '' | ||
| + | |||
| + | === Direction === | ||
| + | |||
| + | Il sera plus simple de raisonner en points cardinaux. Le robot peut aller au Nord, au Sud, à l'Est, à l' | ||
| + | |||
| + | Aller vers le Nord, c'est diminuer le numéro de ligne de 1. La colonne ne change pas. On peut raisonner de la même façon pour les autres de sorte que l'on pourra définir : | ||
| + | |||
| + | <code python> | ||
| + | NORD = (-1,0) | ||
| + | SUD = (1,0) | ||
| + | EST = (...) # je vous laisse deviner | ||
| + | OUEST = (...) # idem | ||
| + | </ | ||
| + | |||
| + | Ensuite, on dit que dans certains cas, le robot tourne à droite. Tourner à droite quand on va au Nord, cela fait aller à l'Est. Si on va au Sud, tourner à droite fait aller à l' | ||
| + | |||
| + | C'est bien de placer tout ça dans une fonction : | ||
| + | |||
| + | <code python> | ||
| + | def droite(dir_actuelle): | ||
| + | if dir_actuelle == NORD: | ||
| + | return EST | ||
| + | elif dir_actuelle == SUD: | ||
| + | return ... | ||
| + | | ||
| + | </ | ||
| + | |||
| + | === est-ce un mur ? === | ||
| + | |||
| + | Je souhaite savoir si une certaines position '' | ||
| + | |||
| + | C'est bien de prévoir une fonction pour cela : | ||
| + | |||
| + | <code python> | ||
| + | def is_wall(lines, line, col): | ||
| + | """ | ||
| + | lines: lignes du fichier | ||
| + | line, col: position demandée | ||
| + | renvoie True si la position correspond à un mur | ||
| + | """ | ||
| + | hauteur = ... # nombre de lignes | ||
| + | largeur = ... # nombre de colonnes | ||
| + | if not 0 <= line < hauteur or not 0 <= col < largeur: | ||
| + | # en dehors du cadre donc | ||
| + | return ... | ||
| + | car = lines[line][col] | ||
| + | # en fonction de la valeur de car, on sait si c'est un mur | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | === Fichier de sortie === | ||
| + | |||
| + | On connaît le contenu du fichier d' | ||
| + | |||
| + | Il faut remplacer le caractère | ||
| + | |||
| + | On pourra utiliser la fonction suivante qui remplace le caractère à un certain indice | ||
| + | |||
| + | <code python> | ||
| + | def replace_car_at(chaine, | ||
| + | """ | ||
| + | chaine: chaine de caractère originale | ||
| + | new_car: nouveau caractère | ||
| + | index: position du remplacement | ||
| + | renvoie une copie de chaine où le caractère à la position | ||
| + | index est remplacé par new_car. Si index trop grand, aucun changement | ||
| + | """ | ||
| + | if index >= len(chaine): | ||
| + | return chaine | ||
| + | return chaine[: | ||
| + | </ | ||
| + | |||
| + | Une fois '' | ||
| + | |||
| + | <code python> | ||
| + | content = ' | ||
| + | f = open(filename, | ||
| + | f.write(content) | ||
| + | f.close() | ||
| + | </ | ||
nsi/tds/robot_rebondissant.1638720813.txt.gz · Dernière modification : de goupillwiki
