Best Python code snippet using websmith_python
PTree.py
Source:PTree.py
1from Node import TreeNode2class ParseTree:3 def __init__(self):4 self.nodes = []5 def addNode(self, nonTerm, children, parent):6 self.nodes.append(TreeNode(nonTerm, children, parent))7 def addRoot(self, nonTerm, children, parent):8 if not self.nodes:9 self.nodes.append(TreeNode(nonTerm, children, parent))10 else:11 return False12 def removeNode(self, val):13 for n in self.nodes:14 if(n.value == val):15 self.nodes.remove(n)16 17 def printTree(self):18 print("====================")19 for node in self.nodes:20 print(node)21 print("====================")22 def findByValue(self, value):23 for node in self.nodes:24 if node.value == value:25 return node26 def findNodesIndex(self, value):27 index = 028 for node in self.nodes:29 if node.value == value:30 return index31 index += 132 def editNode(self, value, kidsIndex, NonTerminalDic, parent):33 kidsIndex+=134 for key in NonTerminalDic.keys():35 if key == value:36 if kidsIndex >= len(NonTerminalDic[key]):37 print("\nDeleting node:",value, "\n")38 self.removeNode(value)39 print("\nYet another backtracking...\n")40 prnt = self.findByValue(parent)41 if value == 'S':42 return None43 return self.editNode(prnt.value, 0, NonTerminalDic, prnt.parent)44 else:45 next_rules = NonTerminalDic[key][kidsIndex]46 print("\nDeleting node:", value, "\n")47 self.removeNode(value)48 self.addNode(value, next_rules, parent)49 new_node = self.nodes[self.findNodesIndex(value)]50 return new_node51 def createTree(self, NonTerminalDic, root, inputStr, index, kidsIndex):52 for child in root.children:53 if child.isupper():54 for key in NonTerminalDic.keys():55 if child == key:56 self.addNode(child, NonTerminalDic[key][0], root.value) #most left rule of production57 self.printTree()58 index = self.createTree(NonTerminalDic, self.findByValue(child), inputStr, index, 0)59 else:60 if child == inputStr[index]:61 self.addNode(child, None, root.value)62 self.printTree()63 index+=164 if index == len(inputStr):65 for node in self.nodes:66 if node.value.isupper():67 pass68 else:69 print(node)70 break71 elif child == '$':72 if len(root.children) == 1 and index != len(inputStr):73 print("\nBacktracking...\n")74 new_root = self.editNode(root.value, kidsIndex, NonTerminalDic, root.parent)75 self.printTree()76 index = self.createTree(NonTerminalDic, new_root, inputStr, index, kidsIndex+1)77 else:78 print("\nChild for which we need backtracking:",child)79 print("Backtracking...\n")80 new_root = self.editNode(root.value, kidsIndex, NonTerminalDic, root.parent)81 if new_root == None:82 break83 self.printTree()84 index = self.createTree(NonTerminalDic, new_root, inputStr, index, kidsIndex+1)85 if len(root.children) != 1:86 break 87 return index...
test_splinter_strategies.py
Source:test_splinter_strategies.py
1import pytest2from stere.fields import Field3from stere.strategy.splinter import (4 FindByCss,5 FindById,6 FindByName,7 FindByTag,8 FindByText,9 FindByValue,10 FindByXPath,11)12from stere.strategy.strategy import strategies13def test_unregistered_strategy():14 """When an unregistered strategy is used15 Then a ValueError should be thrown16 """17 strategies = [18 'css', 'xpath', 'tag', 'name', 'text', 'id', 'value',19 ]20 with pytest.raises(ValueError) as e:21 Field('fail', 'foobar')22 expected_message = f'The strategy "fail" is not in {strategies}.'23 assert expected_message == str(e.value)24def test_unexpected_strategy():25 """Given Stere's default splinter strategies26 When an unexpected strategy is found27 Then this test should fail28 """29 assert strategies == {30 'css': FindByCss,31 'xpath': FindByXPath,32 'tag': FindByTag,33 'name': FindByName,34 'text': FindByText,35 'id': FindById,36 'value': FindByValue,37 }38def test_strategy_attribute_correct():39 """The strategy attribute on each Strategy class should be correct."""40 assert 'css' == FindByCss.strategy41 assert 'id' == FindById.strategy42 assert 'name' == FindByName.strategy43 assert 'tag' == FindByTag.strategy44 assert 'text' == FindByText.strategy45 assert 'value' == FindByValue.strategy...
수의 새로운 연산.py
Source:수의 새로운 연산.py
1# https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AV2b-QGqADMBBASw&categoryId=AV2b-QGqADMBBASw&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=PYTHON&select-1=3&pageSize=10&pageIndex=72def findByValue(value):3 if value == 1:4 return (1, 1)5 temp = 16 temp_x = 07 for i in range(2, 300):8 temp += i9 if temp >= value:10 temp_x = i11 break12 return (temp_x - (temp-value), 1 + (temp-value))13def findByLoc(x, y):14 result = 015 for i in range(1, x+1):16 result += i17 for j in range(0, y-1):18 result += x + j19 return result20T = int(input())21answer = []22for test_case in range(T):23 p, q = map(int, input().split())24 px, py = findByValue(p)25 qx, qy = findByValue(q)26 answer.append(findByLoc(px+qx, py+qy))27 print(findByValue(10000))28for i in range(len(answer)):...
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!!