Lecture "Brute-force algorithms", exercise 2
essepuntato opened this issue · 17 comments
Create a test case for the algorithm introduced in Listing 2.
from collections import deque
def test_stack_from_list(input_list):
result = stack_from_list(input_list)
for item in reversed(input_list):
y = result.pop()
if y == item:
continue
else: return False
return True
def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
return output_stack
from collections import deque
# test
def test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
if result == expected:
return True
else:
return False
# function
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((["Doctor Who", "The Dark Crystal", "Stranger Things", "The Mandalorian"]),
deque(["Doctor Who", "The Dark Crystal", "Stranger Things", "The Mandalorian"])))
# return True
print(test_stack_from_list((["Doctor Who", "The Dark Crystal", "Stranger Things", "The Mandalorian"]),
deque(["Doctor Who", "Stranger Things", "The Dark Crystal", "The Mandalorian"])))
# return False, the order is the same as the input list
print(test_stack_from_list((["Doctor Who", "The Dark Crystal", "Stranger Things", "The Mandalorian"]),
deque([1, 2, 3, 4])))
# return False, function returns strings, not numbers
print(test_stack_from_list((["Doctor Who", "", "Stranger Things", "The Mandalorian"]),
deque(["Doctor Who", "", "Stranger Things", "The Mandalorian"])))
# return True, function accepts blank items in list
'''
def test_stack(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
print (test_stack(["a","b","c"], deque(['a', 'b', 'c'])))
print (test_stack(["ciao","hello","bonjour"], deque(['ciao', 'hello', 'bonjour'])))
print (test_stack([""], deque([''])))
'''
from collections import deque
# Test case for the function
def test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
return expected == result
# Code of the function
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
# Three different test runs returning True
print(test_stack_from_list([], deque()))
print(test_stack_from_list(list([1, 2, 3, 4, 5]), deque([1, 2, 3, 4, 5])))
print(test_stack_from_list(["Rick", "Morty", "Summer", "Beth", "Jerry"], deque(["Rick", "Morty", "Summer", "Beth","Jerry"])))
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
from collections import deque
def test_stack(input_list, expected):
result = stack_from_list(input_list)
return result == expected
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
tv= list(["The Office", "Lost", "Fargo"])
print(test_stack(tv, deque(["The Office", "Lost", "Fargo"])))
from collections import deque
def test(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() # 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
#Define a test case.
print(test([2, 4, 6, 8, 10, 12], deque([2, 4, 6, 8, 10, 12]))) # returns True
print(test(["a", "b", "c", "f"], deque(["a", "b", "c", "f"]))) # returns True
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() # 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
#tests
print(test_stack_from_list(list(["Lost", "OA", "Dark", "The Office"]), deque(["Lost", "OA", "Dark", "The Office"])))
print(test_stack_from_list(list(["JoJo", "Psycho Pass", "Promise Neverland", "Made in Abyss"]), deque(["JoJo", "Psycho Pass", "Promise Neverland", "Made in Abyss"])))
print(test_stack_from_list(list([""]), deque([""])))
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([]), deque([])))
print(test_stack_from_list(list(["The Beatles", "Rolling Stones", "Queen", "Pink Floyd"]), deque(["The Beatles", "Rolling Stones", "Queen", "Pink Floyd"])))
print(test_stack_from_list(list(["Bojack", "Diane", "Todd"]), deque(["Bojack", "Diane", "Todd"])))
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() # 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:
print(test_stack_from_list(list(["a", "c", "y", "x"]), deque(['a', 'c', 'y', 'x']))) # this test returns True
# test 2:
print(test_stack_from_list(list([3, 4, 5, 6]), deque([3, "4", 5, 6]))) # this test returns False, "4" is not a number
# test 3:
print(test_stack_from_list(list([" ", "-", " "]), deque([" ", "-", ""]))) # this test returns False, "result" is not equal to "expected"
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(["Annie Hall","Manhattan","Match Point", "Midnight in Paris"], deque(["Annie Hall","Manhattan","Match Point", "Midnight in Paris"]))) #returns True
print(test_stack_from_list(["Journal of Nutrition", "Science", "The Lancet"], deque(["Journal of Nutrition", "Science", "The Lancet"]))) #returns True
print(test_stack_from_list(["s", "v", 0], deque(["s", "v", "0"]))) #returns False
from _collections import deque
#test definition
def test_stack_from_list (input_list, output_stack):
result = stack_from_list (input_list)
if output_stack == result:
return True
else:
return False
#algorithm definition
def stack_from_list(input_list):
output_stack = deque()
for item in input_list:
output_stack.append(item)
return output_stack
#definition variables for list and stack for short
my_list = ["Giammei", "Slater", "Murgi", "Virginia", "Chiru", "Fleur"]
my_stack = deque(["Giammei", "Slater", "Murgi", "Virginia", "Chiru", "Fleur"])
#three test runs
print(test_stack_from_list(my_list, my_stack))
print(test_stack_from_list(list(), deque(list())))
print(test_stack_from_list([2,3,5,4,1], deque([2,3,5,4,1])))
from collections import deque
# test
def test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
if result == expected:
return True
else:
return False
# code
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(["living room", "kitchen", "bedroom"], deque(["living room", "kitchen", "bedroom"]))) # return True
print(test_stack_from_list([], deque([]))) # return True
print(test_stack_from_list([1, 2, 3, 4, 5, 6], deque([1, 2, 3, 4, 5]))) #return False ```
from collections import deque
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
def test_stack_from_list(input_list, expected):
result = stack_from_list(input_list)
if result == expected:
return True
else:
return False
print (test_stack_from_list(["a", "b", "c", "d"], deque(["a", "b", "c", "d"]))) # returns True
print (test_stack_from_list([3, "b", 2.9, " "], deque([3, "b", 2.9, " "]))) # 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([10,75,114,2020],deque([10,75,114,2020])))
print(test_stack_from_list(["river", "lake", "sea", "ocean"],deque(["river", "lake", "sea", "ocean"])))
`from collections import deque
cities_list = ["Ottavia", "Leonia", "Trude"]
animal_list = ["dog", "cat", "butterfly"]
weather_list = ["sunny", "windy"]
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(cities_list, deque(['Ottavia', 'Leonia', 'Trude'])))
print(test_stack_from_list(animal_list, deque(['dog', 'cat', 'butterfly'])))
print(test_stack_from_list(weather_list, deque(['sunny', 'windy'])))
`
Hi all,
Thanks for your solution. A few comments:
- @edoardodalborgo your testing function is not compliant with the template I've provided to you. In particular, it will always return a successful execution (also when the algorithm works incorrectly), since you do not have any way to specify what is the expected value.
- @ChiaraCati and @vanessabonanno, please remember that you can claim that an algorithm works correctly against the tests if all the tests are passed (i.e. they return
True
). As far as I can see, this is not true for some of your test. - Remember to use the Markdown environment for preserving the spaces in your Python code, as reminded during the lectures.