diff --git a/Exercice_1_-_Le_champ_de_Noel/champ.py b/Exercice_1_-_Le_champ_de_Noel/champ.py index ad40737..1300539 100644 --- a/Exercice_1_-_Le_champ_de_Noel/champ.py +++ b/Exercice_1_-_Le_champ_de_Noel/champ.py @@ -1 +1,53 @@ -# Met ton code ici +#!/usr/bin/env python3 + +from random import randrange + +def main(): + # Columns = 7 Lines = 5 + TREE = [ + " ^", + " ^ ^", + " ( o )", + "( o o )", + " U" + ] + NB_TREES = 6 + + NB_COLUMN = 60 # Min 7+2+2 = 11 (sans les bordures (+2)) + NB_LINES = 40 # Min 5+3 = 8 (sans les bordures (+2)) + + VERTICAL_EDGE = (NB_COLUMN + 2) * '#' + LINE_WO_TREE = '#' + ' ' * (NB_COLUMN) + '#' + + TREE_COORD = [(randrange(4, NB_COLUMN + 1 - 3),randrange(5, NB_LINES + 1)) for tree in range(NB_TREES)] + + TREE_COORD = sorted(TREE_COORD, key=lambda y: y[1]) + X_COORD = [TREE_COORD[tree][0] for tree in range(NB_TREES)] + Y_COORD = [TREE_COORD[tree][1] for tree in range(NB_TREES)] + + print(TREE_COORD) + print(X_COORD) + print(Y_COORD, "\n") + + # J'ai descendu tout ce qui concernait la forest ici... c'est plus clair + forest = [VERTICAL_EDGE] + [LINE_WO_TREE for i in range(NB_LINES)] + [VERTICAL_EDGE] + for y in range(NB_LINES+2): + forest[y] = list(forest[y].strip()) + + # La nouvelle double boucle + for i in range(NB_TREES): + TREE_HEIGHT = len(TREE) + X_OFFSET = 3 + for tree_line in range(TREE_HEIGHT): + Y = Y_COORD[i] - (TREE_HEIGHT - tree_line - 1) + X_START = X_COORD[i] - X_OFFSET + X_END = X_COORD[i] - X_OFFSET + len(TREE[tree_line]) + + forest[Y][X_START:X_END] = TREE[tree_line] + + for i in range(len(forest)): + print(''.join(forest[i])) + print("\n") + +if __name__ == '__main__': + main() diff --git a/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py b/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py index eb26a67..c0ca1bf 100644 --- a/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py +++ b/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py @@ -81,39 +81,57 @@ ']' """ +import string def do_cipher(plain_text: str, key: int) -> str: - # Met ton code ici - return "Il faudrait retourner un vrai truc ;)" - + LIM_MIN_DOWN = ord('A') + LIM_MIN_UP = ord('Z') + ALPHABET_LOOP = len(string.ascii_uppercase) + + cipher_text = [] + + plain_text = plain_text.upper() + + for i in plain_text: + if i in string.ascii_uppercase: + ascii_value_plain = ord(i) + ascii_value_cipher = ord(i) - key + if key < 0: + if ascii_value_cipher > LIM_MIN_UP: + ascii_value_cipher -= ALPHABET_LOOP + else : + if ascii_value_cipher < LIM_MIN_DOWN: + ascii_value_cipher += ALPHABET_LOOP + cipher_text.append(chr(ascii_value_cipher)) + else: + cipher_text.append(i) + cipher_text_string = "".join(cipher_text) + return cipher_text_string def do_decipher(cipher_text: str, key: int) -> str: - # Met ton code ici - return "Il faudrait retourner un vrai truc ;)" - + plain_text_string = do_cipher(cipher_text,-key) + return plain_text_string def main(): PLAIN_TEXT = "Ave Caesar morituri te salutant" - KEY = 3 + KEY = -10 print("************ Caesar's cypher ************\n\n") print("Plain text :", PLAIN_TEXT) print("key :", KEY) - print() - print("Let's cipher now!") + print("\nLet's cipher now!") CIPHER_TEXT = do_cipher(PLAIN_TEXT, KEY) print("Cipher text :", CIPHER_TEXT) print() - print("Let's decipher (will it work?!)") - DECIPHER_TEXT = do_decipher(PLAIN_TEXT, KEY) + print("\nLet's decipher (will it work?!)") + DECIPHER_TEXT = do_decipher(CIPHER_TEXT, KEY) print("Decipher text:", DECIPHER_TEXT) print() - + SUCCESS = PLAIN_TEXT.upper() == DECIPHER_TEXT print("Did it worked?", "OK :)" if SUCCESS else "Nope :(") - - + if __name__ == "__main__": main()