Best Python code snippet using slash
perceptron.py
Source:perceptron.py
1# -*- coding: utf-8 -*-2"""3Created on Thu Mar 14 04:57:25 20194@author: Yunyung5"""6import numpy as np7# AND gate8def AND(x1, x2):9 x = np.array([x1, x2])10 w = np.array([0.5, 0.5])11 b = -0.712 tmp = np.sum(x * w) + b13 if tmp <= 0:14 return 015 else:16 return 117 18# NAND19def NAND(x1, x2):20 x = np.array([x1, x2])21 w = np.array([-0.5, -0.5])22 b = 0.723 tmp = np.sum(x * w) + b24 if tmp <= 0:25 return 026 else:27 return 128 29# OR30def OR(x1, x2):31 x = np.array([x1, x2])32 w = np.array([0.5, 0.5])33 b = -0.234 tmp = np.sum(x * w) + b35 if tmp <= 0:36 return 037 else:38 return 139 40# XOR41def XOR(x1, x2):42 s1 = OR(x1, x2)43 s2 = NAND(x1, x2)44 y = AND(s1, s2)45 if (y <= 0):46 return 047 else:48 return 149 50# half Adder51def halfAdder(x1, x2):52 c = AND(x1, x2)53 s = XOR(x1, x2)54 return s, c # return sum carryout55# full Adder56def fullAdder(x1, x2, ci): # input1, input2, carryIn57 s1, c1 = halfAdder(x1, x2)58 s, c2 = halfAdder(s1, ci)59 co = OR(c1, c2)60 return s, co # return sum, carryout61# two bit full Adder62def twoBitFullAdder(y1, y2, x1, x2, ci): 63 s1, c1 = fullAdder(x1, x2, ci)64 s2, co = fullAdder(y1, y2, c1)65 return co, s2, s1 66 # return carryout,sum2, sum1, tranlate binary to Decimal for easy check(è½æåé²ä½æ¹ä¾¿è§å¯çµæ)67def BinaryToDecimal(arr):68 return " " + str(arr[0] * 4 + arr[1] * 2 + arr[2])69print("Half adder:")70print(" (s, c)")71print("input(0, 0) -> " + str(halfAdder(0, 0)))72print("input(0, 1) -> " + str(halfAdder(0, 1)))73print("input(1, 0) -> " + str(halfAdder(1, 0)))74print("input(1, 1) -> " + str(halfAdder(1, 1)))75print("--------------------------------------------------")76print("One bit Full adder:")77print(" (x1,x2,c) (s, c)")78print("input(0, 0, 0) -> " + str(fullAdder(0, 0, 0)))79print("input(0, 0, 1) -> " + str(fullAdder(0, 0, 1)))80print("input(0, 1, 0) -> " + str(fullAdder(0, 1, 0)))81print("input(0, 1, 1) -> " + str(fullAdder(0, 1, 1)))82print("input(1, 0, 0) -> " + str(fullAdder(1, 0, 0)))83print("input(1, 0, 1) -> " + str(fullAdder(1, 0, 1)))84print("input(1, 1, 0) -> " + str(fullAdder(1, 1, 0)))85print("input(1, 1, 1) -> " + str(fullAdder(1, 1, 1)))86print("--------------------------------------------------")87print("Two bit Full adder:")88print(" (y1,y2,x1,x2,ci) (co,s2,s1) -> Decimal Value)")89print("input(0, 0, 0, 0, 0) -> " + str(twoBitFullAdder(0, 0, 0, 0, 0)) + BinaryToDecimal(twoBitFullAdder(0, 0, 0, 0, 0)) )90print("input(0, 0, 0, 0, 1) -> " + str(twoBitFullAdder(0, 0, 0, 0, 1)) + BinaryToDecimal(twoBitFullAdder(0, 0, 0, 0, 1)) )91print("input(0, 0, 0, 1, 0) -> " + str(twoBitFullAdder(0, 0, 0, 1, 0)) + BinaryToDecimal(twoBitFullAdder(0, 0, 0, 1, 0)) )92print("input(0, 0, 0, 1, 1) -> " + str(twoBitFullAdder(0, 0, 0, 1, 1)) + BinaryToDecimal(twoBitFullAdder(0, 0, 0, 1, 1)) )93print("input(0, 0, 1, 0, 0) -> " + str(twoBitFullAdder(0, 0, 1, 0, 0)) + BinaryToDecimal(twoBitFullAdder(0, 0, 1, 0, 0)) )94print("input(0, 0, 1, 0, 1) -> " + str(twoBitFullAdder(0, 0, 1, 0, 1)) + BinaryToDecimal(twoBitFullAdder(0, 0, 1, 0, 1)) )95print("input(0, 0, 1, 1, 0) -> " + str(twoBitFullAdder(0, 0, 1, 1, 0)) + BinaryToDecimal(twoBitFullAdder(0, 0, 1, 1, 0)) )96print("input(0, 0, 1, 1, 1) -> " + str(twoBitFullAdder(0, 0, 1, 1, 1)) + BinaryToDecimal(twoBitFullAdder(0, 0, 1, 1, 1)) )97print("input(0, 1, 0, 0, 0) -> " + str(twoBitFullAdder(0, 1, 0, 0, 0)) + BinaryToDecimal(twoBitFullAdder(0, 1, 0, 0, 0)) )98print("input(0, 1, 0, 0, 1) -> " + str(twoBitFullAdder(0, 1, 0, 0, 1)) + BinaryToDecimal(twoBitFullAdder(0, 1, 0, 0, 1)) )99print("input(0, 1, 0, 1, 0) -> " + str(twoBitFullAdder(0, 1, 0, 1, 0)) + BinaryToDecimal(twoBitFullAdder(0, 1, 0, 1, 0)) )100print("input(0, 1, 0, 1, 1) -> " + str(twoBitFullAdder(0, 1, 0, 1, 1)) + BinaryToDecimal(twoBitFullAdder(0, 1, 0, 1, 1)) )101print("input(0, 1, 1, 0, 0) -> " + str(twoBitFullAdder(0, 1, 1, 0, 0)) + BinaryToDecimal(twoBitFullAdder(0, 1, 1, 0, 0)) )102print("input(0, 1, 1, 0, 1) -> " + str(twoBitFullAdder(0, 1, 1, 0, 1)) + BinaryToDecimal(twoBitFullAdder(0, 1, 1, 0, 1)) )103print("input(0, 1, 1, 1, 0) -> " + str(twoBitFullAdder(0, 1, 1, 1, 0)) + BinaryToDecimal(twoBitFullAdder(0, 1, 1, 1, 0)) )104print("input(0, 1, 1, 1, 1) -> " + str(twoBitFullAdder(0, 1, 1, 1, 1)) + BinaryToDecimal(twoBitFullAdder(0, 1, 1, 1, 1)) )105print("input(1, 0, 0, 0, 0) -> " + str(twoBitFullAdder(1, 0, 0, 0, 0)) + BinaryToDecimal(twoBitFullAdder(1, 0, 0, 0, 0)) )106print("input(1, 0, 0, 0, 1) -> " + str(twoBitFullAdder(1, 0, 0, 0, 1)) + BinaryToDecimal(twoBitFullAdder(1, 0, 0, 0, 1)) )107print("input(1, 0, 0, 1, 0) -> " + str(twoBitFullAdder(1, 0, 0, 1, 0)) + BinaryToDecimal(twoBitFullAdder(1, 0, 0, 1, 0)) )108print("input(1, 0, 0, 1, 1) -> " + str(twoBitFullAdder(1, 0, 0, 1, 1)) + BinaryToDecimal(twoBitFullAdder(1, 0, 0, 1, 1)) )109print("input(1, 0, 1, 0, 0) -> " + str(twoBitFullAdder(1, 0, 1, 0, 0)) + BinaryToDecimal(twoBitFullAdder(1, 0, 1, 0, 0)) )110print("input(1, 0, 1, 0, 1) -> " + str(twoBitFullAdder(1, 0, 1, 0, 1)) + BinaryToDecimal(twoBitFullAdder(1, 0, 1, 0, 1)) )111print("input(1, 0, 1, 1, 0) -> " + str(twoBitFullAdder(1, 0, 1, 1, 0)) + BinaryToDecimal(twoBitFullAdder(1, 0, 1, 1, 0)) )112print("input(1, 0, 1, 1, 1) -> " + str(twoBitFullAdder(1, 0, 1, 1, 1)) + BinaryToDecimal(twoBitFullAdder(1, 0, 1, 1, 1)) )113print("input(1, 1, 0, 0, 0) -> " + str(twoBitFullAdder(1, 1, 0, 0, 0)) + BinaryToDecimal(twoBitFullAdder(1, 1, 0, 0, 0)) )114print("input(1, 1, 0, 0, 1) -> " + str(twoBitFullAdder(1, 1, 0, 0, 1)) + BinaryToDecimal(twoBitFullAdder(1, 1, 0, 0, 1)) )115print("input(1, 1, 0, 1, 0) -> " + str(twoBitFullAdder(1, 1, 0, 1, 0)) + BinaryToDecimal(twoBitFullAdder(1, 1, 0, 1, 0)) )116print("input(1, 1, 0, 1, 1) -> " + str(twoBitFullAdder(1, 1, 0, 1, 1)) + BinaryToDecimal(twoBitFullAdder(1, 1, 0, 1, 1)) )117print("input(1, 1, 1, 0, 0) -> " + str(twoBitFullAdder(1, 1, 1, 0, 0)) + BinaryToDecimal(twoBitFullAdder(1, 1, 1, 0, 0)) )118print("input(1, 1, 1, 0, 1) -> " + str(twoBitFullAdder(1, 1, 1, 0, 1)) + BinaryToDecimal(twoBitFullAdder(1, 1, 1, 0, 1)) )119print("input(1, 1, 1, 1, 0) -> " + str(twoBitFullAdder(1, 1, 1, 1, 0)) + BinaryToDecimal(twoBitFullAdder(1, 1, 1, 1, 0)) )...
hof-debugging-quiz.py
Source:hof-debugging-quiz.py
...15 'hidden': False,16 'locked': False,17 'question': r"""18 The following code will throw an error when run---what's wrong with it?19 >>> def make_adder(n1):20 ... def adder(n2):21 ... return n1 + n222 ...23 >>> adder = make_adder(3)24 >>> adder(4)25 """26 },27 {28 'answer': "TypeError: ... 'NoneType' object is not ...",29 'choices': [30 'SyntaxError',31 'IndentationError',32 "TypeError: ... 'NoneType' object is not ...",33 'NameError'34 ],35 'hidden': False,36 'locked': False,37 'question': r"""38 What type of error will you see when you run the following code?39 >>> def make_adder(n1):40 ... def adder(n2):41 ... return n1 + n242 ...43 >>> adder = make_adder(3)44 >>> adder(4)45 """46 },47 {48 'answer': '>>> def make_adder(n1): ... def adder(n2): ... return n1 + n2 ... return adder ... >>> adder = make_adder(3) >>> adder(4)',49 'choices': [50 r"""51 >>> def make_adder(n1):52 ... def adder(n2):53 ... return n1 + n254 ...55 >>> adder = make_adder(3)56 >>> adder(4)57 """,58 r"""59 >>> def make_adder(n1):60 ... def adder(n2):61 ... return n1 + n262 ... return adder(n1)63 ...64 >>> adder = make_adder(3)65 >>> adder(4)66 """,67 r"""68 >>> def make_adder(n1):69 ... def adder(n2):70 ... return n1 + n271 ... return adder72 ...73 >>> adder = make_adder(3)74 >>> adder(4)75 """,76 r"""77 >>> def make_adder(n1):78 ... def adder(n2):79 ... return n1 + n280 ...81 >>> adder = make_adder(3)(4)82 >>> adder83 """84 ],85 'hidden': False,86 'locked': False,87 'question': 'Which of the following will execute without error?'88 },89 {90 'answer': 'same_arg_twice should return a pointer to apply_twice, without calling it',91 'choices': [92 'Nothing is wrong with the above code',93 'apply_twice does not need a return statement',94 'same_arg_twice should return a pointer to apply_twice, without calling it',95 'One of the variable names is not spelled correctly when referenced'...
마법사 상어와 토네이도.py
Source:마법사 상어와 토네이도.py
1dx,dy = [0,1,0,-1], [-1,0,1,0]2n = int(input())3arr = [list(map(int,input().split())) for i in range(n)]4sx, sy = n//2, n//25ans = 06def check(x,y):7 if 0<=x<n and 0<=y<n:8 return True9 else:10 return False11def move(x,y,p):12 global ans13 xi,yi = x+dx[p], y+dy[p]14 temp = arr[xi][yi]15 arr[xi][yi] = arr[x][y]16 arr[x][y] = 017 total = 018 19 if p % 2 == 0:20 # 1%21 xi,yi = x+dx[(p+1)%4], y22 adder = temp // 10023 total += adder24 if check(xi,yi):25 arr[xi][yi] += adder26 else:27 ans += adder28 xi,yi = x+dx[(p+3)%4], y29 adder = temp // 10030 total += adder31 if check(xi,yi):32 arr[xi][yi] += adder33 else:34 ans += adder35 # 2%36 xi,yi = x+2*dx[(p+1)%4], y+dy[p]37 adder = temp // 5038 total += adder39 if check(xi,yi):40 arr[xi][yi] += adder41 else:42 ans += adder43 xi,yi = x+2*dx[(p+3)%4], y+dy[p]44 adder = temp // 5045 total += adder46 if check(xi,yi):47 arr[xi][yi] += adder48 else:49 ans += adder50 # 7%51 xi,yi = x+dx[(p+1)%4], y+dy[p]52 adder = int(temp * 0.07)53 total += adder54 if check(xi,yi):55 arr[xi][yi] += adder56 else:57 ans += adder58 xi,yi = x+dx[(p+3)%4], y+dy[p]59 adder = int(temp * 0.07)60 total += adder61 if check(xi,yi):62 arr[xi][yi] += adder63 else:64 ans += adder65 # 10%66 xi,yi = x+dx[(p+1)%4], y+2*dy[p]67 adder = temp // 1068 total += adder69 if check(xi,yi):70 arr[xi][yi] += adder71 else:72 ans += adder73 xi,yi = x+dx[(p+3)%4], y+2*dy[p]74 adder = temp // 1075 total += adder76 if check(xi,yi):77 arr[xi][yi] += adder78 else:79 ans += adder80 # 5%81 xi,yi = x, y+3*dy[p]82 adder = temp // 2083 total += adder84 if check(xi,yi):85 arr[xi][yi] += adder86 else:87 ans += adder88 # a89 xi,yi = x, y+2*dy[p]90 if check(xi,yi):91 arr[xi][yi] += temp - total92 else:93 ans += temp - total94 else:95 # 1%96 xi,yi = x, y+dy[(p+1)%4]97 adder = temp // 10098 total += adder99 if check(xi,yi):100 arr[xi][yi] += adder101 else:102 ans += adder103 xi,yi = x, y+dy[(p+3)%4]104 adder = temp // 100105 total += adder106 if check(xi,yi):107 arr[xi][yi] += adder108 else:109 ans += adder110 # 2%111 xi,yi = x+dx[p], y+2*dy[(p+1)%4]112 adder = temp // 50113 total += adder114 if check(xi,yi):115 arr[xi][yi] += adder116 else:117 ans += adder118 xi,yi = x+dx[p], y+2*dy[(p+3)%4]119 adder = temp // 50120 total += adder121 if check(xi,yi):122 arr[xi][yi] += adder123 else:124 ans += adder125 # 7%126 xi,yi = x+dx[p], y+dy[(p+1)%4]127 adder = int(temp * 0.07)128 total += adder129 if check(xi,yi):130 arr[xi][yi] += adder131 else:132 ans += adder133 xi,yi = x+dx[p], y+dy[(p+3)%4]134 adder = int(temp * 0.07)135 total += adder136 if check(xi,yi):137 arr[xi][yi] += adder138 else:139 ans += adder140 # 10%141 xi,yi = x+2*dx[p], y+dy[(p+1)%4]142 adder = temp // 10143 total += adder144 if check(xi,yi):145 arr[xi][yi] += adder146 else:147 ans += adder148 xi,yi = x+2*dx[p], y+dy[(p+3)%4]149 adder = temp // 10150 total += adder151 if check(xi,yi):152 arr[xi][yi] += adder153 else:154 ans += adder155 # 5%156 xi,yi = x+3*dx[p], y157 adder = temp // 20158 total += adder159 if check(xi,yi):160 arr[xi][yi] += adder161 else:162 ans += adder163 # a164 xi,yi = x+2*dx[p], y165 if check(xi,yi):166 arr[xi][yi] += temp - total167 else:168 ans += temp - total169length = 1170p = 0171while sx > 0 or sy > 0:172 for i in range(length):173 move(sx,sy,p)174 sx+=dx[p]; sy+=dy[p]175 if sx == 0 and sy == 0:176 break177 p += 1178 if sx == 0 and sy == 0:179 break180 for i in range(length):181 move(sx,sy,p)182 sx+=dx[p]; sy+=dy[p]183 p = (p+1)%4184 length += 1...
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!!