Open essepuntato opened 3 years ago
To test the algorithm introduced in Listing 2, I have found 2 different approaches. My first approach is based upon the "First In Last Out" principle of the stacks and the second approach is a much more rigorous element-by-element comparison of input and the output stacks.
first approach
def test_stack_from_list(input_list,output_stack,expected):
x = len(input_list)
x -= 1
y = output_stack.pop()
print(input_list)
if input_list[x]==y and expected==True:
print("Function works")
else:
print("Function has some problem")
You can see the code in action here second approach
def test_stack_from_list(input_list,output_stack,expected):
x = len(input_list)
y= 0
while y <= x-1:
if input_list[y]==output_stack[y]:
y += 1
else:
return False
def test_stack_from_list (input_list, expected):
result = stack_from_list(input_list)
if expected == result:
return True
else:
return False
from collections import deque
def stack_from_list(input_list):
output_stack = deque()
# Iterate each element in the input list and add it to the stack
for item in input_list:
output_stack.append(item)
return output_stack
print(test_stack_from_list(["Harry", "Ron", "Hermione", "Hagrid"], deque(["Harry", "Ron", "Hermione", "Hagrid"])))
from collections import deque
def test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
if expected == result:
return True
else:
return False
my_list = ["Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"]
def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
return output_stack
print(test_stack_from_list(my_list, deque(["Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"])))
print(stack_from_list(my_list))
It returns:
True
deque(['Gryffindor', 'Hufflepuff', 'Ravenclaw', 'Slytherin'])
from collections import deque
def test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
if result == expected:
return True
else:
return False
def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
return output_stack
print(test_stack_from_list(['a', 'b', 'c'], deque(['a', 'b', 'c'])))
print(test_stack_from_list([1, 5, 7, 9, 2, 3], deque([1, 5, 7, 9, 2, 3])))
from collections import deque
def test_stack_from_list(input_list, expected_stack):
result = stack_from_list(input_list)
if result == expected_stack:
return True
else:
return False
def stack_from_list(input_list):
output_stack = deque() # the stack to create
# Iterate each element in the input list and add it to the stack
for item in input_list:
output_stack.append(item)
return output_stack
#Test 1
stack_alfa = deque(["Saba", "Montale", "Caproni", "Penna"]) #Create the expected stack
list_alfa = list(["Saba", "Montale", "Caproni", "Penna"]) #Create the expected test input list
print(test_stack_from_list(list_alfa, stack_alfa)) #Execute the test. Expect: T
#Test 2
stack_beta = deque(["Saba", "Montale", "Caproni", "Penna"]) #Create the expected stack
list_beta = list(["Saba", "Montale", "Caproni", "Moravia"]) #Create the expected test input list
print(test_stack_from_list(list_beta, stack_beta)) #Execute the test. Expect: F
If we execute the code, the result is True
for Test 1 and False
for Test 2: as a consequence, the code should be correct
def test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
if result == expected:
return True
else:
return False
from collections import deque
def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
return output_stack
#First test
example_list1 = list(["A","B","D","C"])
expected_stack1 = deque(["A","B","D","C"])
print(test_stack_from_list(example_list1, expected_stack1)) #Returns True
#Second test
example_list2 = list([1,4,8,0,-12])
expected_stack2 = deque([4,8,1,-12])
print(test_stack_from_list(example_list2,expected_stack2)) #Returns False
Task_2
from collections import deque
def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
return output_stack
def testing(input_list, expected):
result = stack_from_list(input_list)
if result == expected:
return True
else:
return False
print(testing([0, 1, 2], deque([0, 1, 2]))) #the test returns True
from collections import deque
def test_stack_from_list(input_list, expected)
result = stack_from_list(input_list)
if result == expected
return True
else:
return False
Def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
return output_stack
print(test_stack_from_list(list[12, 13, 14, 15], deque([12, 13, 14, 15])))
def test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
if expected == result:
return True
else:
return False
from collections import deque
def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
return output_stack
print (test_stack_from_list(["0", "1", "2", "3"], ["0", "1", "2", "3"]))
Hi @ManuSrivastava1,
Please, go back to see how the template of the test works, since you are specifying too many parameters to the testing function. You have made things a little more complicated for a user perspective.
from collections import deque
test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
if result == expected:
return True
else:
return False
def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
print(test_stack_from_list([1, 2, 3], deque([1, 2, 3])))
print(test_stack_from_list([1, "hello", 3], deque([1, "hello", 3])))
print(test_stack_from_list(["hello", "ciao"], deque(["hello", "ciao"])))
print(test_stack_from_list([], deque()))
from collections import deque
def test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
if expected == result:
return True
else:
return False
def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
return output_stack
print(test_stack_from_list([1, 2, 3, 4, 5], deque([1, 2, 3, 4, 5])))
print(test_stack_from_list(["Alice", "Catherine", "Bob", "Charles"], deque(["Alice", "Catherine", "Bob", "Charles"])))
print(test_stack_from_list(["Ron", "Harry", "Hermione", 25, 4], deque(["Ron", "Harry", "Hermione", 25, 4])))
from collections import deque
def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
return output_stack
def test(input_list, expected):
result = stack_from_list(input_list)
if result == expected:
return True
else:
return False
print (test(["a", "3", "c"]), deque(["a", "3", "c"])))
from collections import deque
def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
return output_stack
def test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
if expected == result:
return True
else:
return False
print(test_stack_from_list(["a", "b", "c"], deque(["a", "b", "c"])))
print(test_stack_from_list([1, 2, 3], deque([1, 2, 3])))
print(test_stack_from_list([], deque([])))
print(test_stack_from_list(["a", "a", "a"], deque(["a", "a", "a"])))
print(test_stack_from_list([1, 1, 1], deque([1, 1, 1])))
Create a test case for the algorithm introduced in Listing 2.