How to use difflist method in autotest

Best Python code snippet using autotest_python

segunda-entrega.py

Source:segunda-entrega.py Github

copy

Full Screen

1#!/usr/bin/env python2# coding: utf-83# # Segunda entrega4# Esta debería ser la entrega final.5#6# A continuación están las funciones.7# In[ ]:8def read_img(image: str) -> [list,int,int,float]:9 from skimage import io10 import numpy as np11 import cv212 img = io.imread(image)13 X = img.shape[0]14 Y = img.shape[1]15 imgread = [img,X,Y]16 return imgread17# Test18#print(read_img("./Freedo_improved.jpeg")[0])19#print(read_img("./Freedo_improved.jpeg")[1])20#print(read_img("./Freedo_improved.jpeg")[2])21#print(read_img("./Freedo_improved.jpeg")[3])22# In[1]:23def draw_triangles(X,Y,triangles):24 import numpy as np25 import cv226 image = np.ones((X,Y,3), np.uint8)*255 # Blank squared image27 N = len(triangles)28 for i in range(0,N):29 pts = np.array(30 [triangles[i][0],triangles[i][1],triangles[i][2]],31 np.int3232 )33 pts = pts.reshape((-1,1,2))34 cv2.fillPoly(image, [pts], (triangles[i][3]))35 return image36# In[ ]:37def first_gen(N: int, P: int, X: int, Y: int) -> [list,list]:38 import random39 triangles = []40 imagearr = []41 for i in range(0,P):42 vertex = []43 for i in range(0,N):44 triag = [45 [random.randint(0,X),random.randint(0,Y)],46 [random.randint(0,X),random.randint(0,Y)],47 [random.randint(0,X),random.randint(0,Y)],48 [random.randint(0,255),random.randint(0,255),random.randint(0,255)]49 ]50 vertex += [triag]51 imagearr += [draw_triangles(X,Y,vertex)]52 triangles += [vertex]53 return [imagearr,triangles]54# Test55#first_gen(5,5,204,209)[0][0]56# In[ ]:57def fitness(original, image):58 import numpy as np59 from skimage import io60 import cv261 X = original.shape[0]62 Y = original.shape[1]63 difflist = []64 for i in range(0,X):65 for k in range(0,Y):66 origvals = []67 imgvals = []68 for l in range(0,3):69 origvals += [original.item(i,k,l)]70 imgvals += [image.item(i,k,l)]71 if origvals[0] == imgvals[0] and origvals[1] == imgvals[1] and origvals[2] == imgvals[2]:72 difflist += [1]73 else:74 difflist += [0]75 diffsum = sum(difflist)76 diff = sum(difflist)/len(difflist)77 return diff78# Test79#fitness(80# read_img("./Freedo_improved.jpeg")[0],81# read_img("./Freedo_improved_inverted.jpeg")[0],82#)83# In[ ]:84def old_fitness(original, image):85 from skimage.metrics import structural_similarity as ssim86 import numpy as np87 import cv288 (score,diff) = ssim(original, image, full=True, multichannel=True)89 return abs(score)90# In[ ]:91def mutate(triangles,X,Y):92 import random93 N = len(triangles)94 M = random.randint(0,N-1)95 K = random.randint(0,3)96 colorpercent = int(255 * 0.05)97 Xvertpercent = int(X * 0.05)98 Yvertpercent = int(Y * 0.05)99 for i in range (0,M):100 l = random.randint(0,N-1)101 for i in range (0,K):102 k = random.randint(0,3)103 if k != 3:104 triangles[l][k][0] += random.randint(1,Xvertpercent)*random.choice([-1,1])105 triangles[l][k][1] += random.randint(1,Yvertpercent)*random.choice([-1,1])106 if triangles[l][k][0] >= X:107 triangles[l][k][0] -= triangles[l][k][0] - X108 if triangles[l][k][1] >= Y:109 triangles[l][k][1] -= triangles[l][k][1] - Y110 else:111 triangles[l][k][0] += random.randint(1,colorpercent)*random.choice([-1,1])112 triangles[l][k][1] += random.randint(1,colorpercent)*random.choice([-1,1])113 triangles[l][k][2] += random.randint(1,colorpercent)*random.choice([-1,1])114 if triangles[l][k][0] >= 255:115 triangles[l][k][0] -= triangles[l][k][0] - 255116 if triangles[l][k][1] >= 255:117 triangles[l][k][1] -= triangles[l][k][1] - 255118 if triangles[l][k][2] >= 255:119 triangles[l][k][1] -= triangles[l][k][1] - 255120 return triangles121# Test122# triangles = first_gen(5,5,204,209)[1][2]123# print(triangles)124# mutate(triangles,204,209)125# In[35]:126def selection(original,imagearr) -> [int]:127 import random128 N = len(imagearr)129 difflist = []130 for i in range(0,N):131 difflist += [[fitness(original,imagearr[i]),i]]132 difflist = sorted(difflist, reverse=True)133 best = difflist[0]134 selected = []135 diffsum = 0136 problist = []137 for i in range(0,N):138 diffsum += difflist[i][0]139 problist += [[diffsum,difflist[i][1]]]140 while len(selected) < 2:141 end = N-1142 start = 0143 r = random.uniform(0,diffsum)144 while end != start+1:145 mid = (end+start)//2146 if r > problist[mid][0]:147 start = mid148 elif r < problist[mid][0]:149 end = mid150 else:151 end = start+1152 selected += [problist[end][1]]153 return [selected,best]154# Test155#selection(read_img("./Freedo_improved.jpeg")[0],first_gen(12,10,204,209)[0])156# In[38]:157def crossover(parentA:list, parentB:list, X: int, Y: int,):158 import random159 N = len(parentA)160 sonA = []161 sonB = []162 for i in range(0,N):163 if i <= N//2:164 sonA += [parentA[i]]165 sonB += [parentB[i]]166 else:167 sonB += [parentA[i]]168 sonA += [parentB[i]]169 if random.uniform(0,100) <= 7:170 if random.randint(0,1) == 1:171 sonA = mutate(sonA,X,Y)172 else:173 sonB = mutate(sonB,X,Y)174 return [sonA,sonB]175# Test176#crossover(first_gen(15,7,204,209)[1][0],first_gen(15,7,204,209)[1][1],204,209)177# In[ ]:178def next_gen(original,imagearr,triangles):179 import cv2180 img = original181 X = img.shape[0]182 Y = img.shape[1]183 N = len(imagearr)184 nextgentriag = []185 nextgentriag += [triangles[selection(original,imagearr)[1][1]]]186 while len(nextgentriag) < N:187 k = selection(original,imagearr)[0]188 sons = crossover(triangles[k[0]],triangles[k[1]],X,Y)189 nextgentriag += [sons[0]]190 nextgentriag += [sons[1]]191 while N != len(nextgentriag):192 nextgentriag = nextgentriag[:-1]193 nextgenimgarr = []194 for i in range(0,len(nextgentriag)):195 nextgenimgarr += [draw_triangles(X,Y,nextgentriag[i])]196 best = selection(original,nextgenimgarr)[1]197 return [nextgenimgarr,nextgentriag,best]198# Test199#next_gen(200# read_img("./Freedo_improved.jpeg")[0],201# first_gen(6,10,204,209)[0],202# first_gen(6,10,204,209)[1])203# In[48]:204def gen_algo(original: str, N: int, P: int):205 from matplotlib import pyplot as plt206 import cv2207 X = read_img(original)[1]208 Y = read_img(original)[2]209 img = read_img(original)[0]210 firstgen = first_gen(N,P,X,Y)211 nextgen = next_gen(img,firstgen[0],firstgen[1])212 difflist = []213 bestlist = []214 L = 500215 for i in range(0,L):216 nextgen = next_gen(img,nextgen[0],nextgen[1])217 difflist += [nextgen[2][0]]218 if i % 5 == 0:219 print(i)220 if i % 10 == 0:221 cv2.imwrite("./best-images/simple-square"+str(i)+".png",nextgen[0][nextgen[2][1]])222 plt.plot(difflist)223 plt.show()224 #cv2.imshow("window2",nextgen[0][0])225 #cv2.waitKey(0)226 #cv2.destroyWindow('window2')227 #cv2.waitKey(1)228 #return nextgen229# Test230for i in range(0,5):231 gen_algo("./tux-smol.png",30,20)...

Full Screen

Full Screen

home_1065_함수.py

Source:home_1065_함수.py Github

copy

Full Screen

1# 0. 어떤 수에 대해서, 그 수가 한수인지 판별2# n = input()3# diffList = []4# for i in range(len(n)-1):5# diff = int(n[i])-int(n[i+1])6# diffList.append(diff)7# if len(set(diffList)) == 0 or len(set(diffList)) == 1:8# print("한수")9# else:10# print("아니야")11# 1. 어떤 수보다 작은 모든 수에 대해서, 그 수가 한수인지 판별.12# n = input()13# numList = list(range(1, int(n) + 1))14# for num in numList:15# diffList = []16# for i in range(len(str(num))-1):17# diff = int(str(num)[i]) - int(str(num)[i+1])18# diffList.append(diff)19# checkNum = len(set(diffList))20# if checkNum == 0 or checkNum == 1:21# print("한수")22# 2. 어떤 수보다 작은 모든 수에 대해서, 그 수가 한수인지 판별하고, 개수 세기.23# n = input()24# numList = list(range(1, int(n) + 1))25# nCount = 026# for num in numList:27# diffList = []28# for i in range(len(str(num))-1):29# diff = int(str(num)[i]) - int(str(num)[i+1])30# diffList.append(diff)31# checkNum = len(set(diffList))32# if checkNum == 0 or checkNum == 1:33# nCount += 134 35# print(nCount)36# 3. 함수로 구현하기37# def hansu(n):38# numList = list(range(1, n + 1))39# nCount = 040# for num in numList:41# diffList = []42# for i in range(len(str(num))-1):43# diff = int(str(num)[i]) - int(str(num)[i+1])44# diffList.append(diff)45# checkNum = len(set(diffList))46# if checkNum == 0 or checkNum == 1:47# nCount += 148 49# return print(nCount)50# hansu(150)51# 4. 입력값 받으면 돌려주는 애 구하기52def hansu(n):53 numList = list(range(1, n + 1))54 nCount = 055 for num in numList:56 diffList = []57 for i in range(len(str(num))-1):58 diff = int(str(num)[i]) - int(str(num)[i+1])59 diffList.append(diff)60 checkNum = len(set(diffList))61 if checkNum == 0 or checkNum == 1:62 nCount += 163 64 return print(nCount)65x = int(input())...

Full Screen

Full Screen

2981.py

Source:2981.py Github

copy

Full Screen

1from collections import deque as dq2N = int(input())3nlist = [int(input())for __ in range(N)]4nlist = sorted(nlist, reverse=True)5def gcd(a, b):6 a, b = max(a, b), min(a, b)7 while(b != 0):8 r = a % b9 a = b10 b = r11 return a12def cd(a):13 nlist = []14 for i in range(1, int(a**(1/2))+1):15 if a % i == 0:16 nlist.append(i)17 if a//i != i:18 nlist.append(a//i)19 nlist.remove(1)20 return sorted(nlist)21difflist = dq()22for i in range(len(nlist)-1):23 for j in range(i+1, len(nlist)):24 difflist.append(nlist[i]-nlist[j])25for i in range(len(difflist)):26 # print(difflist)27 if(len(difflist)>1):28 a = difflist.popleft()29 b= difflist.popleft()30 difflist.insert(0,gcd(a,b))31for cd in cd(difflist[0]):...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful