Best Python code snippet using hypothesis
Dubspanier.py
Source:Dubspanier.py
1# -*- coding: utf-8 -*-2"""3Algorithme de Dubins-Spanier4"""5import pylab as pl6from math import *7import numpy as np8import matplotlib as mp9import random as rnd10couleurs = ['r','b','g','y','gray']11def calcul_integrale(f,i,j,pas):12 integrale=013 x=i14 while x<j:15 integrale+=f(x)*pas16 x+=pas17 return integrale18def dubspanier_rec(vals, inter, epsilon, position, n):19 a = position20 if len(vals) == 0:21 return []22 prop= 1./n23 m = len(vals)24 current_int = [0. for i in range(n)]25 while a<1.:26 a+=inter27 for i in range(m):28 current_int[i]+=vals[i][0](a)*inter29 if max(current_int)>prop-epsilon: 30 player = vals[np.argmax(current_int)][1]31 vals.pop(np.argmax(current_int))32 v = [(player,position,a,max(current_int))]33 w = dubspanier_rec(vals,inter,epsilon,a,n)34 return v+w35 return None36#Première amélioration.37def dubspanier_rec2(vals, inter, epsilon, position, n):38 a = position39 if len(vals) == 0:40 return []41 #On réajuste la proportionnalité au fur et à mesure pour remplir le gâteau.42 prop= (1.-position)/(len(vals))43 m = len(vals)44 current_int = [0. for i in range(n)]45 while a<1.:46 a+=inter47 for i in range(m):48 current_int[i]+=vals[i][0](a)*inter49 if max(current_int)>prop-epsilon: 50 player = vals[np.argmax(current_int)][1]51 vals.pop(np.argmax(current_int))52 v = [(player,position,a,max(current_int))]53 w = dubspanier_rec2(vals,inter,epsilon,a,n)54 return v+w55 return None56 57#Seconde amélioration.58def dubspanier(vals, inter, epsilon, n):59 prop = 1./n60 vals2 = list(vals)61 l = dubspanier_rec3(vals2, inter, epsilon, 0., prop, n)62 print "BA",l63 m = l[len(l)-1][2]64 while 1.-m>0.03:65 vals2 = list(vals)66 prop += (1.-m)/n67 print "here"68 l = dubspanier_rec3(vals2, inter, epsilon, 0., prop, n)69 print "here" 70 m = l[len(l)-1][2]71 return l72def dubspanier_rec3(vals, inter, epsilon, position, prop, n):73 a = position74 if len(vals) == 0:75 return []76 m = len(vals)77 current_int = [0. for i in range(n)]78 while a<1.:79 a+=inter80 for i in range(m):81 current_int[i]+=vals[i][0](a)*inter82 if max(current_int)>prop-epsilon: 83 player = vals[np.argmax(current_int)][1]84 vals.pop(np.argmax(current_int))85 v = [(player,position,a,max(current_int))]86 w = dubspanier_rec3(vals,inter,epsilon,a,prop,n)87 return v+w88 return None89#print(dubspanier([[v1,1],[v2,2],[v3,3],[v4,4],[v5,5]],0.001,0.000001,5))90def v1(x):91 return 2.*x92def v2(x):93 return 1.94 95I3 = calcul_integrale(lambda x : np.sin(20*x) + 1.,0.,1.,0.000001)96def v3(x):97 global I398 return (np.sin(20*x) + 1)/I399I4 = calcul_integrale(lambda x : np.exp(x**2),0.,1.,0.00001)100def v4(x):101 global I4102 return np.exp(x**2)/I4103 104I5 = calcul_integrale(lambda x : -((x-.5)**2),0.,1.,0.00001)105def v5(x):106 global I5107 return -((x-.5)**2)/I5108 109p=3110#I4 = calcul_integrale(lambda x : v2(x)**(p+1)/(v1(x)**p+v2(x)**p+v3(x)**p+v4(x)**p+v5(x)**p),0.,1.,0.00001)111#print I4, "I4"112def dubspanier_plot(vals2,inter,epsilon,n):113 vals = list(vals2)114 l = dubspanier(vals,inter,epsilon,n)115 print(l)116 x = pl.linspace(0.,1.,100)117 fig = mp.pyplot.figure()118 ax = fig.add_subplot(111)119 global couleurs120 for e in enumerate(l):121 (player,debut,fin,val) = e[1]122 tab = list(np.linspace(debut,fin,10))123 data = [vals2[player-1][0](m) for m in tab]124 z = [(debut,0.)] + zip(tab,data) + [(fin,0.)]125 c = couleurs[e[0]]126 mp.pyplot.plot(x,[vals2[player-1][0](i) for i in x],c)127 poly = mp.patches.Polygon(z,facecolor = c)128 ax.add_patch(poly)129 130def calcul_beta(vals2, inter, epsilon, n):131 recherche = 1./n132 b = True133 while b:134 vals = list(vals2)135 try: 136 l = dubspanier_rec3(vals,inter,epsilon,0.,recherche,n)137 except:138 b = False139 recherche += 0.00005140 return recherche141 142#print calcul_beta([[v1,1],[v2,2],[v3,3],[v4,4],[v5,5]],0.001,0.000001,5)143print calcul_beta([[v1,1],[v4,2]],0.001,0.000001,2) 144def integrale_sup(vals2, n):145 I = 0146 actuel = 0.147 inter = 1./n148 m = len(vals2)149 for i in range(n):150 I+=inter * max([vals2[j][0](actuel) for j in range(m)])151 actuel += inter152 return I153 154def integrale_sup2(vals2, n, p):155 I = 0.156 actuel = 0.157 inter = 1./n158 for i in range(n):159 I+=inter * (vals2[p][0](actuel)**2)/(sum([v[0](actuel) for v in vals2]))160 actuel += inter161 return I162 163#print (.2)*integrale_sup([[v1,1],[v2,2],[v3,3],[v4,4],[v5,5]],10000)164print .5*integrale_sup([[v3,0],[v4,1]],10000)165 166dubspanier_plot([[v1,1],[v2,2],[v3,3],[v4,4],[v5,5]],0.001,0.000001,5)167#dubspanier_plot([[v1,1],[v2,2],[v3,3]],0.001,0.000001,4)168def rand_function(n,nu):169 t = np.linspace(0.,1.,n)170 y = [rnd.random()+.1]171 for i in range(1,n):172 a = rnd.randint(0,1)173 if a == 0 or y[i-1]-nu<0:174 y.append(y[i-1]+(rnd.random()*nu))175 else:176 y.append(y[i-1]-(rnd.random()*nu))177 return t, np.array(y)178 179def discrete_int(t,y):180 I = t[1]*y[0]181 for i in range(1,len(t)):182 I+= (t[i]-t[i-1])*y[i]183 return I184 185def disc_dubspanier(vals, inter, epsilon, position, n):186 a = position187 if len(vals) == 0:188 return []189 prop= 1./n190 m = len(vals)191 current_int = [0. for i in range(n)]192 while a<1.:193 a+=inter194 for i in range(m):195 current_int[i]+=vals[i][0][a*300]*inter196 if max(current_int)>prop-epsilon: 197 player = vals[np.argmax(current_int)][1]198 vals.pop(np.argmax(current_int))199 v = [(player,position,a,max(current_int))]200 w = disc_dubspanier(vals,inter,epsilon,a,n)201 return v+w202 return None203 204def disc_dubspanier2(vals, inter, epsilon, position, n):205 a = position206 if len(vals) == 0:207 return []208 prop= (1.-position)/len(vals)209 m = len(vals)210 current_int = [0. for i in range(n)]211 while a<1.:212 a+=inter213 for i in range(m):214 current_int[i]+=vals[i][0][a*300]*inter215 if max(current_int)>prop-epsilon: 216 player = vals[np.argmax(current_int)][1]217 vals.pop(np.argmax(current_int))218 v = [(player,position,a,max(current_int))]219 w = disc_dubspanier2(vals,inter,epsilon,a,n)220 return v+w221 return None222 223def disc_dubspanier_opti(vals, inter, epsilon, n):224 prop = 1./n225 vals2 = list(vals)226 l = disc_dubspanier3(vals2, inter, epsilon, 0., prop, n)227 m = l[len(l)-1][2]228 while 1.-m>0.03:229 vals2 = list(vals)230 prop += (1.-m)/n231 l = disc_dubspanier3(vals2, inter, epsilon, 0., prop, n) 232 m = l[len(l)-1][2]233 return l234def disc_dubspanier3(vals, inter, epsilon, position, prop, n):235 a = position236 if len(vals) == 0:237 return []238 m = len(vals)239 current_int = [0. for i in range(n)]240 while a<1.:241 a+=inter242 for i in range(m):243 current_int[i]+=vals[i][0][a*300]*inter244 if max(current_int)>prop-epsilon: 245 player = vals[np.argmax(current_int)][1]246 vals.pop(np.argmax(current_int))247 v = [(player,position,a,max(current_int))]248 w = disc_dubspanier3(vals,inter,epsilon,a,prop,n)249 return v+w250 return None251def dubspanier_plot_disc(vals2,inter,epsilon,n):252 vals = list(vals2)253 l = disc_dubspanier2(vals,inter,epsilon,0.,n)254 print l255 x = pl.linspace(0.,1.,300)256 fig = mp.pyplot.figure()257 ax = fig.add_subplot(111)258 global couleurs259 for e in enumerate(l):260 (player,debut,fin,val) = e[1]261 tab = list(np.linspace(debut,fin,100))262 data = [vals2[player-1][0][m*300] for m in tab]263 z = [(debut,0.)] + zip(tab,data) + [(fin,0.)]264 c = couleurs[e[0]]265 mp.pyplot.plot(x,vals2[player-1][0],c) 266 poly = mp.patches.Polygon(z,facecolor = c)267 ax.add_patch(poly)268 269l=[]270for i in range(5):271 t,y = rand_function(300, 0.05)272 I = discrete_int(t,y)273 y = y/I274 l.append((y,i+1))275 276#dubspanier_plot_disc(l,0.01,0.01,5)277 278"""279print discrete_int(np.linspace(0.,1.,300), sup([e[0] for e in l])), "Max"280S = 0281a = disc_dubspanier_opti(l, 0.01, 0.01, 5)282for e in a:283 S+=e[3]284print S, "Valuation totale"285print a...
open_lock.py
Source:open_lock.py
1"""2You're given a lockbox with a starting state (combination),3a goal state, and a set of forbidden states.4e.g. start=4156, bad_states=[3912, 4157], goal=99995Each state can only be rotated one digit at a time,6e.g. 4156 -> 4157 or 4156 -> 3156.7Write a function that returns a valid sequence of8steps from start to goal.9"""10from typing import List11def open_lockbox(start: str, bad_states: List[str], goal: str) -> List[str]:12 result = []13 assert len(start) == len(14 goal15 ), "Different lengths of start and goal states"16 current = start17 length_string = len(start)18 current_string_index = 019 # loop until we change the last char in the lock20 while current_string_index <= (length_string - 1):21 current_char = start[current_string_index]22 current_int = int(current_char)23 desired_int = int(goal[current_string_index])24 # determine incremenet/decrement25 moving_forward = True26 end_range = 1027 if current_int < desired_int:28 if (desired_int - current_int) > (29 current_int + (end_range - desired_int)30 ):31 moving_forward = False32 else:33 if (current_int - desired_int) < (34 desired_int + (end_range - current_int)35 ):36 moving_forward = False37 # move the current char(int) to the desired state38 while current_int != desired_int:39 if moving_forward:40 current_int = (current_int + 1) % 1041 else:42 current_int = (current_int - 1) % 1043 # recreate temporary state44 current_state = ""45 for l in range(length_string):46 if l == current_string_index:47 current_state += str(current_int)48 else:49 current_state += current[l]50 # check for conflicts and add to valid states51 # if no conflicts52 if current_state in bad_states:53 if moving_forward:54 current_int = (current_int - 1) % 1055 else:56 current_int = (current_int + 1) % 1057 moving_forward = not moving_forward58 else:59 result.append(current_state)60 current_string_index += 161 current = current_state62 return result63if __name__ == "__main__":64 print(open_lockbox(start="4156", bad_states=["3912", "4157"], goal="9999"))...
main.py
Source:main.py
1def solution(a, b):2 if len(a) < len(b):3 print(a+b+a)4 else:5 print(b+a+b)6solution("1", "22")7def name_shuffler(str_):8 name = str_.split(" ")9 print(name[1], name[0])10name_shuffler("Olivia Walker")11def between(a,b):12 13 current_int = a14 results = []15 16 while current_int <= b:17 results.append(current_int)18 current_int = current_int + 1 # 219 20 21 return results 22 23 def unusual_five():24 return len("apple")25def name_shuffler(str_):26 name = str_.split(" ")...
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!!