Best Python code snippet using localstack_python
Bataille_Navale.py
Source:Bataille_Navale.py
1# Plateau de 10x10 contre l'IA2# Les colonnes numérotées entre A-J, les lignes 0-93# On place aléatoire les bateaux de taille (une taille de 5, une taille de 4, deux taille de 3, une taille de 2 et une taille de 1)4# On demande le ou le joueur souhaite tiré (A9)5# S'il touche, on marque la case6# S'il rate, on le notifie7# A chaque tour on lui montre son plateau8# Le vainqueur quand il n'y a pu de bateau !9# Un tableau de 10x1010# Placer des bateaux dessus (6, de taille 5,4, 2*3, 2 , 1)11# Tu prends une case au hasard, tu prend une orientation au hasard, taille -> Si ça sort tu recommences, sinon tu places1213import random14i=015j=016fin=017check=118check2=119direction_bateau= {0: "d", 1: "b", 2: "g", 3: "h"}20index_par_chiffre = {"A": 0, "B":1, "C":2, "D":3, "E":4, "F":5, "G":6, "H":7, "I":8, "J":9}21def initialisation_tableau():22 taille = 1023 plateau = []24 for x in range(taille):25 ligne = []26 for y in range(taille):27 ligne.append("~")28 plateau.append(ligne)29 return plateau3031def afficher(tableau):32 index = 033 index_par_letter = {0: "A", 1: "B", 2: "C", 3: "D", 4: "E", 5: "F", 6: "G", 7: "H", 8: "I", 9: "J"}34 coordonneesX = " "35 while index < len(tableau):36 coordonneesX = coordonneesX + index_par_letter[index] + " "37 index = index + 138 print(coordonneesX)39 index = 040 for ligne in tableau:41 ligne_str = str(index) + " "42 for col in ligne:43 ligne_str = ligne_str + col + " "44 print(ligne_str)45 index = index + 14647def placement_bateau(taille):48 continuer=149 while continuer==1:50 in_range=151 while in_range == 1 :52 x=153 i=random.randint(0,9)54 j=random.randint(0,9)55 direction_chiffre=random.randint(0,3)56 direction=direction_bateau[direction_chiffre]57 if direction == "d":58 if j+taille<10 and j+taille>=0:59 in_range = 060 for x in range(taille):61 if tableau[i][j + x] == "b":62 in_range=163 if direction == "b":64 if i+taille<10 and i+taille>=0:65 in_range = 066 for x in range(taille):67 if tableau[i + x][j] == "b":68 in_range=169 if direction == "g":70 if j-taille<10 and j-taille>=0:71 in_range = 072 for x in range(taille):73 if tableau[i][j - x] == "b":74 in_range=175 if direction == "h":76 if i-taille<10 and i-taille>=0:77 in_range = 078 for x in range(taille):79 if tableau[i - x][j] == "b":80 in_range = 181 if in_range == 0:82 tableau[i][j] = "b"83 if direction == "d" :84 for x in range (taille):85 tableau[i][j+x]="b"86 if direction == "b" :87 for x in range (taille):88 tableau[i+x][j]="b"89 if direction == "g" :90 for x in range (taille):91 tableau[i][j-x]="b"92 if direction == "h" :93 for x in range (taille):94 tableau[i-x][j]="b"95 continuer=09697def placement_bateau_adverse(taille):98 continuer=199 while continuer==1:100 in_range=1101 while in_range == 1 :102 x=1103 i=random.randint(0,9)104 j=random.randint(0,9)105 direction_chiffre=random.randint(0,3)106 direction=direction_bateau[direction_chiffre]107 if direction == "d":108 if j+taille<10 and j+taille>=0:109 in_range = 0110 for x in range(taille):111 if tableau2[i][j + x] == "b":112 in_range=1113 if direction == "b":114 if i+taille<10 and i+taille>=0:115 in_range = 0116 for x in range(taille):117 if tableau2[i + x][j] == "b":118 in_range=1119 if direction == "g":120 if j-taille<10 and j-taille>=0:121 in_range = 0122 for x in range(taille):123 if tableau2[i][j - x] == "b":124 in_range=1125 if direction == "h":126 if i-taille<10 and i-taille>=0:127 in_range = 0128 for x in range(taille):129 if tableau2[i - x][j] == "b":130 in_range = 1131 if in_range == 0:132 tableau2[i][j] = "b"133 if direction == "d" :134 for x in range (taille):135 tableau2[i][j+x]="b"136 if direction == "b" :137 for x in range (taille):138 tableau2[i+x][j]="b"139 if direction == "g" :140 for x in range (taille):141 tableau2[i][j-x]="b"142 if direction == "h" :143 for x in range (taille):144 tableau2[i-x][j]="b"145 continuer=0146147print("Bienvenue au jeu de la bataille navale. Objectif: Battre l'IA ! ")148149150tableau = initialisation_tableau()151placement_bateau(5)152placement_bateau(4)153placement_bateau(3)154placement_bateau(3)155placement_bateau(2)156placement_bateau(1)157tableau2 = initialisation_tableau()158placement_bateau_adverse(5)159placement_bateau_adverse(4)160placement_bateau_adverse(3)161placement_bateau_adverse(3)162placement_bateau_adverse(2)163placement_bateau_adverse(1)164tableau3 = initialisation_tableau()165def afficher_planche():166 print("Votre planche avec vos navires.")167 afficher(tableau)168 print("Planche adversaire.")169 afficher(tableau3)170171def tir_allier():172 a = str(input("Entrez l'abscisse de la case où vous souhaitez effectuer votre tir."))173 b = int(input("Entrez l'ordonnée de la case où vous voulez effectuer votre tir."))174 c = int(index_par_chiffre[a])175 if (tableau2[b][c])=="b":176 tableau3[b][c]="X"177 tableau2[b][c] = "X"178 print("Vous avez touché un navire adverse !")179 if (tableau2[b][c])=="~":180 tableau3[b][c]="0"181 print("Vous avez tiré... Dans l'eau !")182183def tir_adverse():184 b = random.randint(0,9)185 c = random.randint(0,9)186 if (tableau[b][c])=="b":187 tableau[b][c]="X"188 print("L'adversaire a touché un de vos navires. Ne perdez pas courage !")189 if (tableau[b][c])=="~":190 tableau[b][c]="0"191 print("L'adversaire a loupé ! A vous d'attaquer ")192193def check_victoire():194 check=0195 check2=0196 fin=0197 for i in range(10):198 for j in range (10):199 if tableau[i][j]=="b":200 check2=check2+1201 if check2 < 1:202 fin = 2203 for k in range (10):204 for l in range (10):205 if tableau2[k][l]=="b":206 check=check+1207 if check < 1:208 fin = 1209 print("gagné")210afficher_planche()211fin=0212while fin==0:213 if fin==0 :214 check=0215 check2=0216 tir_allier()217 tir_adverse()218 afficher_planche()219 check_victoire()220if fin==1:221 print("La partie est terminée, le joueur victorieux est ",fin)
...
test_time_utils.py
Source:test_time_utils.py
...8 when = datetime.time(12, 0)9 start = datetime.datetime(2019, 1, 1, 11, 0)10 end = datetime.datetime(2019, 1, 1, 11, 0)11 with raises(ValueError):12 time_appears_in_range(when, start, end)13def test_rejects_negative_range() -> None:14 when = datetime.time(12, 0)15 start = datetime.datetime(2019, 1, 1, 12, 0)16 end = datetime.datetime(2019, 1, 1, 11, 0)17 with raises(ValueError):18 time_appears_in_range(when, start, end)19def test_time_appears_in_same_day_range() -> None:20 when = datetime.time(12, 0)21 start = datetime.datetime(2019, 1, 1, 11, 0)22 end = datetime.datetime(2019, 1, 1, 13, 0)23 in_range = time_appears_in_range(when, start, end)24 assert in_range is True25def test_time_doesnt_appear_in_same_day_range_when_before() -> None:26 when = datetime.time(2, 0)27 start = datetime.datetime(2019, 1, 1, 11, 0)28 end = datetime.datetime(2019, 1, 1, 13, 0)29 in_range = time_appears_in_range(when, start, end)30 assert in_range is False31def test_time_doesnt_appear_in_same_day_range_when_after() -> None:32 when = datetime.time(16, 0)33 start = datetime.datetime(2019, 1, 1, 11, 0)34 end = datetime.datetime(2019, 1, 1, 13, 0)35 in_range = time_appears_in_range(when, start, end)36 assert in_range is False37def test_time_appears_in_cross_day_range_later_first_day() -> None:38 when = datetime.time(23, 0)39 start = datetime.datetime(2019, 1, 1, 22, 0)40 end = datetime.datetime(2019, 1, 2, 2, 0)41 in_range = time_appears_in_range(when, start, end)42 assert in_range is True43def test_time_appears_in_cross_day_range_early_second_day() -> None:44 when = datetime.time(1, 0)45 start = datetime.datetime(2019, 1, 1, 22, 0)46 end = datetime.datetime(2019, 1, 2, 2, 0)47 in_range = time_appears_in_range(when, start, end)48 assert in_range is True49def test_time_doesnt_appear_in_cross_day_range() -> None:50 when = datetime.time(12, 0)51 start = datetime.datetime(2019, 1, 1, 22, 0)52 end = datetime.datetime(2019, 1, 2, 2, 0)53 in_range = time_appears_in_range(when, start, end)54 assert in_range is False55def test_time_appears_in_multi_day_range_later_first_day() -> None:56 when = datetime.time(23, 0)57 start = datetime.datetime(2019, 1, 1, 22, 0)58 end = datetime.datetime(2019, 1, 3, 2, 0)59 in_range = time_appears_in_range(when, start, end)60 assert in_range is True61def test_time_appears_in_multi_day_range_early_last_day() -> None:62 when = datetime.time(1, 0)63 start = datetime.datetime(2019, 1, 1, 22, 0)64 end = datetime.datetime(2019, 1, 3, 2, 0)65 in_range = time_appears_in_range(when, start, end)66 assert in_range is True67def test_time_appears_in_multi_day_range_when_appears_outside() -> None:68 when = datetime.time(12, 0)69 start = datetime.datetime(2019, 1, 1, 22, 0)70 end = datetime.datetime(2019, 1, 3, 2, 0)71 in_range = time_appears_in_range(when, start, end)72 assert in_range is True73def test_time_appears_in_same_day_range_different_timezones() -> None:74 when = datetime.time(12, 30, tzinfo=Paris)75 start = datetime.datetime(2019, 1, 1, 11, 20, tzinfo=London)76 end = datetime.datetime(2019, 1, 1, 11, 40, tzinfo=London)77 in_range = time_appears_in_range(when, start, end)78 assert in_range is True79def test_time_doesnt_appear_in_same_day_range_when_before_different_timezones() -> None:80 when = datetime.time(12, 0, tzinfo=Paris)81 start = datetime.datetime(2019, 1, 1, 11, 20, tzinfo=London)82 end = datetime.datetime(2019, 1, 1, 11, 40, tzinfo=London)83 in_range = time_appears_in_range(when, start, end)84 assert in_range is False85def test_time_doesnt_appear_in_same_day_range_when_after_different_timezones() -> None:86 when = datetime.time(12, 50, tzinfo=Paris)87 start = datetime.datetime(2019, 1, 1, 11, 20, tzinfo=London)88 end = datetime.datetime(2019, 1, 1, 11, 40, tzinfo=London)89 in_range = time_appears_in_range(when, start, end)...
summary_ranges.py
Source:summary_ranges.py
1#!/usr/local/bin/python32def summary_ranges(in_range):3 '''4 Given a sorted integer array without duplicates, 5 return the summary of its ranges.6 Example 1:7 Input: [0,1,2,4,5,7]8 Output: ["0->2","4->5","7"]9 Explanation: 0,1,2 form a continuous range; 10 4,5 form a continuous range.11 12 Example 2:13 Input: [0,2,3,4,6,8,9]14 Output: ["0","2->4","6","8->9"]15 Explanation: 2,3,4 form a continuous range; 16 8,9 form a continuous range.17 https://leetcode.com/problems/summary-ranges/description/18 Time : O(N)19 Space: O(N)20 Note :21 1. start index, end index, curr index22 2. progress, if curr != end + 1:23 if start == end => "start"24 else => "start->end"25 3. else: end = curr26 '''27 28 if in_range == None or len(in_range) < 2:29 return [str(in_range[0])]30 start, end = 0, 031 range_arr = []32 for curr in range(1, len(in_range)):33 if in_range[curr] != in_range[end] + 1:34 if start != end:35 range_arr.append("%d->%d" % (in_range[start], in_range[end])) 36 else:37 range_arr.append("%d" % in_range[start])38 start, end = curr, curr39 else:40 end = curr41 if start != end:42 range_arr.append("%d->%d" % (in_range[start], in_range[end])) 43 else:44 range_arr.append("%d" % in_range[start])45 return range_arr46 47def run():48 in_range = [0,1,2,4,5,7]49 print(summary_ranges(in_range))50 in_range = [0,2,3,4,6,8,9]51 print(summary_ranges(in_range))52 '''53 ["0->2","4->5","7"]54 ['0->2', '4->5', '7']55 ["0","2->4","6","8->9"]56 ['0', '2->4', '6', '8->9']57 '''58if __name__ == '__main__':...
auto_orientation.py
Source:auto_orientation.py
...4raw = sense.get_accelerometer_raw()5x=raw['x']6y=raw['y']7z=raw['z']8def in_range(input, mid_num):9 max=mid_num + .510 min=mid_num - .511 if input >=min and input <=max:12 return 113 else:14 return 015def get_orientation():16 if in_range(x, 0) and in_range(y, 1) and in_range(z, 0):17 orientation = 018 elif in_range(x, -1) and in_range(y, 0) and in_range(z, 0):19 orientation = 9020 elif in_range(x, 0) and in_range(y, -1) and in_range(z, 0):21 orientation = 18022 elif in_range(x, 1) and in_range(y, 0) and in_range(z, 0):23 orientation = 27024 else:25 orientation=18026 pass27 return orientation28pass29sense.rotation = get_orientation()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!