Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lecture "Brute-force argorithms", exercise 2 #18

Open
essepuntato opened this issue Oct 28, 2021 · 22 comments
Open

Lecture "Brute-force argorithms", exercise 2 #18

essepuntato opened this issue Oct 28, 2021 · 22 comments
Labels

Comments

@essepuntato
Copy link
Contributor

Create a test case for the algorithm introduced in Listing 2.

@ManuSrivastava1
Copy link

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

You can see the code in action here

@RebeccaJillianBeattie
Copy link

stack_from_list_test

@chloeppd
Copy link

Screenshot 2021-10-31 at 8 17 59 PM

@federicabonifazi
Copy link

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"])))

image

@CarmenSantaniello
Copy link

CarmenSantaniello commented Nov 2, 2021

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'])

@olgagolgan
Copy link

image
image

@MaddaGh
Copy link

MaddaGh commented Nov 2, 2021

ex2 test stack from list

@tommasobattisti
Copy link

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])))

@martasoricetti
Copy link

image

@ManueleVeggi
Copy link

ManueleVeggi commented Nov 3, 2021

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

@OrsolaMBorrini
Copy link

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

@NoraPs
Copy link

NoraPs commented Nov 4, 2021

Cattura

@katya-avem
Copy link

image

@AnastasiyaSopyryaeva
Copy link

AnastasiyaSopyryaeva commented Nov 5, 2021

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

@angstigone
Copy link

Schermata 2021-11-01 alle 18 42 00

@elizastuglik
Copy link

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])))

@Bianca-LM
Copy link

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"]))

@essepuntato
Copy link
Contributor Author

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.

@AmeliaLamargese
Copy link

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()))

@sanyuezoe
Copy link

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])))

@teragramgius
Copy link

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"])))

@sarabecchi
Copy link

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])))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests