From c28aaee37b4a3fb021c8a6f284c56bb5e5f97c38 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Martin Date: Sun, 22 Nov 2020 12:27:31 +0100 Subject: [PATCH 1/9] Exercice1: Premier jet. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pas optimisé, avec un seul arbre. --- Exercice_1_-_Le_champ_de_Noel/champ.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Exercice_1_-_Le_champ_de_Noel/champ.py b/Exercice_1_-_Le_champ_de_Noel/champ.py index ad40737..900a7be 100644 --- a/Exercice_1_-_Le_champ_de_Noel/champ.py +++ b/Exercice_1_-_Le_champ_de_Noel/champ.py @@ -1 +1,22 @@ -# Met ton code ici +from random import * + +column = 20 #sans les bordures (+2) +line = 9 #sans les bordures (+2) + +rand_column = randrange(1,column+1) +rand_line = randrange(1,line+1) + +list_sans_arbre = ['#'] + [' ']*(column) + ['#'] +list_arbre = ['#'] + [' ']*(column-1) + ['#'] + + +print((column+2)*"#") +for i in range(line): + + if i != rand_line : + print ("".join(list_sans_arbre)) + else: + + list_arbre.insert(rand_column,'o') + print ("".join(list_arbre)) +print((column+2)*"#") From db17154ef7d5f34c7541a0a08cfa8f4cc7572f74 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Martin Date: Sun, 22 Nov 2020 12:31:42 +0100 Subject: [PATCH 2/9] =?UTF-8?q?Exercice=201:=20Petites=20am=C3=A9lioration?= =?UTF-8?q?s.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Le champ est plus petit. --- Exercice_1_-_Le_champ_de_Noel/champ.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Exercice_1_-_Le_champ_de_Noel/champ.py b/Exercice_1_-_Le_champ_de_Noel/champ.py index 900a7be..4e08013 100644 --- a/Exercice_1_-_Le_champ_de_Noel/champ.py +++ b/Exercice_1_-_Le_champ_de_Noel/champ.py @@ -1,17 +1,21 @@ from random import * -column = 20 #sans les bordures (+2) -line = 9 #sans les bordures (+2) +column = 5 #sans les bordures (+2) +line = 5 #sans les bordures (+2) -rand_column = randrange(1,column+1) + + +rand_column = randrange(1,column+1) rand_line = randrange(1,line+1) +print(rand_column) +print(rand_line) list_sans_arbre = ['#'] + [' ']*(column) + ['#'] list_arbre = ['#'] + [' ']*(column-1) + ['#'] print((column+2)*"#") -for i in range(line): +for i in range(1,line+1): if i != rand_line : print ("".join(list_sans_arbre)) From 186fcee4948f605622b3b111c75983265d11148c Mon Sep 17 00:00:00 2001 From: Pierre-Yves Martin Date: Sun, 22 Nov 2020 13:08:23 +0100 Subject: [PATCH 3/9] Exercice 1: Correction suite aux remarques sur le dernier commit. --- Exercice_1_-_Le_champ_de_Noel/champ.py | 36 ++++++++++++-------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/Exercice_1_-_Le_champ_de_Noel/champ.py b/Exercice_1_-_Le_champ_de_Noel/champ.py index 4e08013..b5be465 100644 --- a/Exercice_1_-_Le_champ_de_Noel/champ.py +++ b/Exercice_1_-_Le_champ_de_Noel/champ.py @@ -1,26 +1,24 @@ -from random import * +from random import randrange -column = 5 #sans les bordures (+2) -line = 5 #sans les bordures (+2) +COLUMN = 10 # Sans les bordures (+2) +LINE = 5 # Sans les bordures (+2) +RAND_COLUMN = randrange(1, COLUMN+1) +RAND_LINE = randrange(1, LINE+1) +BORDURE_VERTICALE = (COLUMN + 2) * '#' +print(RAND_COLUMN) +print(RAND_LINE) -rand_column = randrange(1,column+1) -rand_line = randrange(1,line+1) -print(rand_column) -print(rand_line) +ligne_sans_arbre = '#' + ' ' * (COLUMN) + '#' +ligne_avec_arbres = '#' + ' ' * (COLUMN-1) + '#' -list_sans_arbre = ['#'] + [' ']*(column) + ['#'] -list_arbre = ['#'] + [' ']*(column-1) + ['#'] - - -print((column+2)*"#") -for i in range(1,line+1): +print(BORDURE_VERTICALE) +for i in range(1, LINE + 1): - if i != rand_line : - print ("".join(list_sans_arbre)) + if i == RAND_LINE : + print (ligne_avec_arbres[:RAND_COLUMN] + + 'o' + ligne_avec_arbres[RAND_COLUMN:]) else: - - list_arbre.insert(rand_column,'o') - print ("".join(list_arbre)) -print((column+2)*"#") + print(ligne_sans_arbre) +print(BORDURE_VERTICALE) From c82ca4eadb28a7ced4a65eb2685f10de0d00bccd Mon Sep 17 00:00:00 2001 From: Pierre-Yves Martin Date: Sun, 22 Nov 2020 13:28:53 +0100 Subject: [PATCH 4/9] Exercice 1: Maintenant il y a plusieurs arbres ! --- Exercice_1_-_Le_champ_de_Noel/champ.py | 55 ++++++++++++++++++-------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/Exercice_1_-_Le_champ_de_Noel/champ.py b/Exercice_1_-_Le_champ_de_Noel/champ.py index b5be465..7df82b1 100644 --- a/Exercice_1_-_Le_champ_de_Noel/champ.py +++ b/Exercice_1_-_Le_champ_de_Noel/champ.py @@ -1,24 +1,45 @@ +#!/usr/bin/env python3 + from random import randrange -COLUMN = 10 # Sans les bordures (+2) -LINE = 5 # Sans les bordures (+2) +def main(): + NB_COLUMN = 10 # Sans les bordures (+2) + NB_LINES = 5 # Sans les bordures (+2) + VERTICAL_EDGE = (NB_COLUMN + 2) * '#' + NB_TREES = 5 + + LINE_WO_TREE = '#' + ' ' * (NB_COLUMN) + '#' + line_w_trees = ['#'] + [' '] * (NB_COLUMN) + ['#'] + + TREE_COORD = [] + X_COORD = [] + Y_COORD = [] -RAND_COLUMN = randrange(1, COLUMN+1) -RAND_LINE = randrange(1, LINE+1) -BORDURE_VERTICALE = (COLUMN + 2) * '#' + for tree in range(NB_TREES): + TREE_COORD.append((randrange(1, NB_COLUMN + 1),randrange(1, NB_LINES + 1))) + X_COORD.append(TREE_COORD[tree][0]) + Y_COORD.append(TREE_COORD[tree][1]) -print(RAND_COLUMN) -print(RAND_LINE) + print(TREE_COORD,"\n") + print(X_COORD) + print(Y_COORD, "\n") -ligne_sans_arbre = '#' + ' ' * (COLUMN) + '#' -ligne_avec_arbres = '#' + ' ' * (COLUMN-1) + '#' -print(BORDURE_VERTICALE) -for i in range(1, LINE + 1): + print(VERTICAL_EDGE) + for i in range(1, NB_LINES + 1): + if i in Y_COORD: + for (x,y) in TREE_COORD: + if i == y: + line_w_trees.remove(' ') + line_w_trees.insert(x,'o') + print(''.join(line_w_trees)) + for o in line_w_trees: + if o == 'o': + line_w_trees.remove('o') + line_w_trees.insert(x,' ') + else: + print(LINE_WO_TREE) + print(VERTICAL_EDGE) - if i == RAND_LINE : - print (ligne_avec_arbres[:RAND_COLUMN] - + 'o' + ligne_avec_arbres[RAND_COLUMN:]) - else: - print(ligne_sans_arbre) -print(BORDURE_VERTICALE) +if __name__ == '__main__': + main() From d6ee6001dc732fa71c5fedf387ba6cbda681ca94 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Martin Date: Sun, 22 Nov 2020 13:43:56 +0100 Subject: [PATCH 5/9] =?UTF-8?q?Exercice=201:=20Ma=20version=20avec=20les?= =?UTF-8?q?=20arbres=20de=20no=C3=ABl.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Exercice_1_-_Le_champ_de_Noel/champ.py | 62 ++++++++++++-------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/Exercice_1_-_Le_champ_de_Noel/champ.py b/Exercice_1_-_Le_champ_de_Noel/champ.py index 7df82b1..65932ab 100644 --- a/Exercice_1_-_Le_champ_de_Noel/champ.py +++ b/Exercice_1_-_Le_champ_de_Noel/champ.py @@ -3,43 +3,39 @@ from random import randrange def main(): - NB_COLUMN = 10 # Sans les bordures (+2) - NB_LINES = 5 # Sans les bordures (+2) + TREE = [" ^"," ^ ^"," ( o )","( o o )"," U"] # Columns = 7 Lines = 5 + NB_TREES = 2 + + NB_COLUMN = 20 # Min 7+2+2 = 11 (sans les bordures (+2)) + NB_LINES = 12 # Min 5+3 = 8 (sans les bordures (+2)) + VERTICAL_EDGE = (NB_COLUMN + 2) * '#' - NB_TREES = 5 - LINE_WO_TREE = '#' + ' ' * (NB_COLUMN) + '#' - line_w_trees = ['#'] + [' '] * (NB_COLUMN) + ['#'] - - TREE_COORD = [] - X_COORD = [] - Y_COORD = [] - - for tree in range(NB_TREES): - TREE_COORD.append((randrange(1, NB_COLUMN + 1),randrange(1, NB_LINES + 1))) - X_COORD.append(TREE_COORD[tree][0]) - Y_COORD.append(TREE_COORD[tree][1]) + forest = [VERTICAL_EDGE] + [LINE_WO_TREE for i in range(NB_LINES)] + [VERTICAL_EDGE] + + TREE_COORD = [(randrange(4, NB_COLUMN + 1 - 3),randrange(5, NB_LINES + 1)) for tree in range(NB_TREES)] - print(TREE_COORD,"\n") + 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") - - - print(VERTICAL_EDGE) - for i in range(1, NB_LINES + 1): - if i in Y_COORD: - for (x,y) in TREE_COORD: - if i == y: - line_w_trees.remove(' ') - line_w_trees.insert(x,'o') - print(''.join(line_w_trees)) - for o in line_w_trees: - if o == 'o': - line_w_trees.remove('o') - line_w_trees.insert(x,' ') - else: - print(LINE_WO_TREE) - print(VERTICAL_EDGE) + print(Y_COORD, "\n") + + for y in range(NB_LINES+2): + forest[y] = list(forest[y].strip()) + + for i in range(NB_TREES): + forest[Y_COORD[i]-4][X_COORD[i]-3:(X_COORD[i]-3+len(TREE[0]))] = TREE[0] + forest[Y_COORD[i]-3][X_COORD[i]-3:(X_COORD[i]-3+len(TREE[1]))] = TREE[1] + forest[Y_COORD[i]-2][X_COORD[i]-3:(X_COORD[i]-3+len(TREE[2]))] = TREE[2] + forest[Y_COORD[i]-1][X_COORD[i]-3:(X_COORD[i]-3+len(TREE[3]))] = TREE[3] + forest[Y_COORD[i]-0][X_COORD[i]-3:(X_COORD[i]-3+len(TREE[4]))] = TREE[4] + + for i in range(len(forest)): + print(''.join(forest[i])) + print("\n") if __name__ == '__main__': main() From 2c5f1c0ab828aa109856751575c76438e0d00096 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Martin Date: Sun, 22 Nov 2020 13:57:33 +0100 Subject: [PATCH 6/9] =?UTF-8?q?Exercice=201:=20la=20version=20corrig=C3=A9?= =?UTF-8?q?e=20par=20PYM.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Exercice_1_-_Le_champ_de_Noel/champ.py | 60 +++++++++++++++----------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/Exercice_1_-_Le_champ_de_Noel/champ.py b/Exercice_1_-_Le_champ_de_Noel/champ.py index 65932ab..1300539 100644 --- a/Exercice_1_-_Le_champ_de_Noel/champ.py +++ b/Exercice_1_-_Le_champ_de_Noel/champ.py @@ -3,39 +3,51 @@ from random import randrange def main(): - TREE = [" ^"," ^ ^"," ( o )","( o o )"," U"] # Columns = 7 Lines = 5 - NB_TREES = 2 - - NB_COLUMN = 20 # Min 7+2+2 = 11 (sans les bordures (+2)) - NB_LINES = 12 # Min 5+3 = 8 (sans les bordures (+2)) - + # 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) + '#' - forest = [VERTICAL_EDGE] + [LINE_WO_TREE for i in range(NB_LINES)] + [VERTICAL_EDGE] - + 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)] - + 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()) - - for i in range(NB_TREES): - forest[Y_COORD[i]-4][X_COORD[i]-3:(X_COORD[i]-3+len(TREE[0]))] = TREE[0] - forest[Y_COORD[i]-3][X_COORD[i]-3:(X_COORD[i]-3+len(TREE[1]))] = TREE[1] - forest[Y_COORD[i]-2][X_COORD[i]-3:(X_COORD[i]-3+len(TREE[2]))] = TREE[2] - forest[Y_COORD[i]-1][X_COORD[i]-3:(X_COORD[i]-3+len(TREE[3]))] = TREE[3] - forest[Y_COORD[i]-0][X_COORD[i]-3:(X_COORD[i]-3+len(TREE[4]))] = TREE[4] - + + # 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") - + print(''.join(forest[i])) + print("\n") + if __name__ == '__main__': main() From c78476e1806b8ad7f19599515d65d4d14d6045db Mon Sep 17 00:00:00 2001 From: Yoshimi Chauvel Date: Sun, 6 Dec 2020 18:15:34 +0100 Subject: [PATCH 7/9] Solution de l'exo 2 --- Exercice_2_-_Le_chiffre_de_Cesar/caesar.py | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py b/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py index eb26a67..dd5b17d 100644 --- a/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py +++ b/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py @@ -115,5 +115,48 @@ def main(): print("Did it worked?", "OK :)" if SUCCESS else "Nope :(") + KEY = 3 + MAJ_KEY = 32 + LIM_MAJ = 96 + LIM_MIN_DOWN = 65 + LIM_MIN_UP = 90 + ALPHABET_LOOP = 26 + + SIGN = "-" + + PLAIN_TEXT = "ave!CAesar_morituri'te SaluTant" + cipher_text = "" + maj_text = "" + + #UPPERCASE CONVERSION + for i in PLAIN_TEXT: + if ord(i) > LIM_MAJ: + ascii_value_maj = ord(i) - MAJ_KEY + maj_text += chr(ascii_value_maj) + else : + maj_text += i + print(maj_text, "\n") + + #DECIPHERING + for i in maj_text: + if ord(i) in range(65,91): + ascii_value_plain = ord(i) + + if SIGN == "-": + ascii_value_cipher = ord(i) + KEY + if ascii_value_cipher > LIM_MIN_UP: + ascii_value_cipher -= ALPHABET_LOOP + else : + ascii_value_cipher = ord(i) - KEY + if ascii_value_cipher < LIM_MIN_DOWN: + ascii_value_cipher += ALPHABET_LOOP + + cipher_text += chr(ascii_value_cipher) + else: + cipher_text += i + + print(cipher_text) + + if __name__ == "__main__": main() From aab43b729ef7f3a3a51c14aca92fb515b9f12e24 Mon Sep 17 00:00:00 2001 From: Yoshimi Chauvel Date: Sun, 6 Dec 2020 21:22:09 +0100 Subject: [PATCH 8/9] =?UTF-8?q?Corrections=20exo=202=20(n=C2=B01)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Exercice_2_-_Le_chiffre_de_Cesar/caesar.py | 103 ++++++++++----------- 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py b/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py index dd5b17d..aabb259 100644 --- a/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py +++ b/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py @@ -80,83 +80,78 @@ >>> chr(90+3) ']' """ - +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 ord(i) in range(ord('A'),ord('Z')+1): + 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 += chr(ascii_value_cipher) + else: + cipher_text += i + return cipher_text def do_decipher(cipher_text: str, key: int) -> str: - # Met ton code ici + LIM_MIN_DOWN = ord('A') + LIM_MIN_UP = ord('Z') + ALPHABET_LOOP = len(string.ascii_uppercase) + + plain_text = "" + + for i in cipher_text: + if ord(i) in range(ord('A'),ord('Z')+1): + 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 + plain_text += chr(ascii_value_cipher) + else: + plain_text += i + return plain_text return "Il faudrait retourner un vrai truc ;)" + def main(): - PLAIN_TEXT = "Ave Caesar morituri te salutant" - KEY = 3 + PLAIN_TEXT = "Ave Caesar! morituri_ te salutant" + 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) + 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 :(") - - - KEY = 3 - MAJ_KEY = 32 - LIM_MAJ = 96 - LIM_MIN_DOWN = 65 - LIM_MIN_UP = 90 - ALPHABET_LOOP = 26 - - SIGN = "-" - - PLAIN_TEXT = "ave!CAesar_morituri'te SaluTant" - cipher_text = "" - maj_text = "" - - #UPPERCASE CONVERSION - for i in PLAIN_TEXT: - if ord(i) > LIM_MAJ: - ascii_value_maj = ord(i) - MAJ_KEY - maj_text += chr(ascii_value_maj) - else : - maj_text += i - print(maj_text, "\n") - - #DECIPHERING - for i in maj_text: - if ord(i) in range(65,91): - ascii_value_plain = ord(i) - - if SIGN == "-": - ascii_value_cipher = ord(i) + KEY - if ascii_value_cipher > LIM_MIN_UP: - ascii_value_cipher -= ALPHABET_LOOP - else : - ascii_value_cipher = ord(i) - KEY - if ascii_value_cipher < LIM_MIN_DOWN: - ascii_value_cipher += ALPHABET_LOOP - - cipher_text += chr(ascii_value_cipher) - else: - cipher_text += i - - print(cipher_text) - if __name__ == "__main__": main() From 3a4f184f2c2d31d3a88e6a2334719404ca165c02 Mon Sep 17 00:00:00 2001 From: Yoshimi Chauvel Date: Sun, 13 Dec 2020 18:14:46 +0100 Subject: [PATCH 9/9] =?UTF-8?q?Correction=20num=C3=A9ro=202=20exo=20cesar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Exercice_2_-_Le_chiffre_de_Cesar/caesar.py | 44 ++++++---------------- 1 file changed, 12 insertions(+), 32 deletions(-) diff --git a/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py b/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py index aabb259..c0ca1bf 100644 --- a/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py +++ b/Exercice_2_-_Le_chiffre_de_Cesar/caesar.py @@ -80,6 +80,7 @@ >>> chr(90+3) ']' """ + import string def do_cipher(plain_text: str, key: int) -> str: @@ -87,12 +88,12 @@ def do_cipher(plain_text: str, key: int) -> str: LIM_MIN_UP = ord('Z') ALPHABET_LOOP = len(string.ascii_uppercase) - cipher_text = "" + cipher_text = [] plain_text = plain_text.upper() for i in plain_text: - if ord(i) in range(ord('A'),ord('Z')+1): + if i in string.ascii_uppercase: ascii_value_plain = ord(i) ascii_value_cipher = ord(i) - key if key < 0: @@ -101,38 +102,18 @@ def do_cipher(plain_text: str, key: int) -> str: else : if ascii_value_cipher < LIM_MIN_DOWN: ascii_value_cipher += ALPHABET_LOOP - cipher_text += chr(ascii_value_cipher) + cipher_text.append(chr(ascii_value_cipher)) else: - cipher_text += i - return cipher_text + cipher_text.append(i) + cipher_text_string = "".join(cipher_text) + return cipher_text_string def do_decipher(cipher_text: str, key: int) -> str: - LIM_MIN_DOWN = ord('A') - LIM_MIN_UP = ord('Z') - ALPHABET_LOOP = len(string.ascii_uppercase) - - plain_text = "" - - for i in cipher_text: - if ord(i) in range(ord('A'),ord('Z')+1): - 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 - plain_text += chr(ascii_value_cipher) - else: - plain_text += i - return plain_text - 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" + PLAIN_TEXT = "Ave Caesar morituri te salutant" KEY = -10 print("************ Caesar's cypher ************\n\n") @@ -144,12 +125,11 @@ def main(): print("Cipher text :", CIPHER_TEXT) print() - print("Let's decipher (will it work?!)") + 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 :(")