comp-think / 2021-2022

The GitHub repository containing all the material related to the Computational Thinking and Programming course of the Digital Humanities and Digital Knowledge degree at the University of Bologna (a.a. 2021/2022).
17 stars 9 forks source link

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

Open essepuntato opened 2 years ago

essepuntato commented 2 years ago

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

ManuSrivastava1 commented 2 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

You can see the code in action here

RebeccaJillianBeattie commented 2 years ago

stack_from_list_test

chloeppd commented 2 years ago
Screenshot 2021-10-31 at 8 17 59 PM
federicabonifazi commented 2 years ago
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 commented 2 years ago
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 commented 2 years ago

image image

MaddaGh commented 2 years ago

ex2 test stack from list

tommasobattisti commented 2 years ago
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 commented 2 years ago

image

ManueleVeggi commented 2 years ago
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 commented 2 years ago
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 commented 2 years ago

Cattura

katya-avem commented 2 years ago

image

AnastasiyaSopyryaeva commented 2 years ago

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 commented 2 years ago
Schermata 2021-11-01 alle 18 42 00
elizastuglik commented 2 years ago

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 commented 2 years ago
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 commented 2 years ago

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 commented 2 years ago
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 commented 2 years ago
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 commented 2 years ago
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 commented 2 years ago
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])))