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 #17

Open
essepuntato opened this issue Nov 9, 2022 · 12 comments
Open

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

essepuntato opened this issue Nov 9, 2022 · 12 comments
Labels

Comments

@essepuntato
Copy link
Contributor

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

@n1kg0r
Copy link

n1kg0r commented Nov 9, 2022

Without the use of deque() function

from collections import deque

# check if the stack is correctly constructed from a list
def test_stack_from_list(input_stack, expected_values):
    # check if the data type is really stack (deque)
    if type(input_stack) != deque:
        return False
    # check if data length is as expected
    if len(input_stack) != len(expected_values):
        return False         
    # check if all data entries are as expected
    expected_values.reverse()
    for i in range(len(expected_values)):
        if input_stack.pop() != expected_values[i]:
            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

# Expect True
print(test_stack_from_list(stack_from_list([1,2,3]), [1,2,3]))
# Console output: True

# Expect False (wrong data type)
print(test_stack_from_list(set(stack_from_list([1,2,3])), [1,2,3]))
# Console output: False

# Expect False (wrong stack length)
print(test_stack_from_list(stack_from_list([1,2]), [1,2,3]))
print(test_stack_from_list(stack_from_list([1,2,3,4]), [1,2,3]))
# Console output: False

# Expect False (values are not equal elementwise)
print(test_stack_from_list(stack_from_list([1,2,'3']), [1,2,3]))
print(test_stack_from_list(stack_from_list([1,3,2]), [1,2,3]))
# Console output: False

Using deque() function

from collections import deque

# check if the stack is correctly constructed from a list
def test_stack_from_list(input_stack, expected_values):
    return input_stack == deque(expected_values)
    
def stack_from_list(input_list):
    output_stack = deque()
    for item in input_list:
        output_stack.append(item)
    return output_stack

# Expect True
print(test_stack_from_list(stack_from_list([1,2,3]), [1,2,3]))
# Console output: True

# Expect False (wrong data type)
print(test_stack_from_list(set(stack_from_list([1,2,3])), [1,2,3]))
# Console output: False

# Expect False (wrong stack length)
print(test_stack_from_list(stack_from_list([1,2]), [1,2,3]))
print(test_stack_from_list(stack_from_list([1,2,3,4]), [1,2,3]))
# Console output: False

# Expect False (values are not equal elementwise)
print(test_stack_from_list(stack_from_list([1,2,'3']), [1,2,3]))
print(test_stack_from_list(stack_from_list([1,3,2]), [1,2,3]))
# Console output: False

@delete4ever
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
# Test case for algorithm
def test(input_list, output_stack):
    if input_list== output_stack: 
        return True
    else: 
        return False
# Three different test runs
print (test(["Alice", "Bob", "Claire", "Danielle"], "Alice"))
print (test(["Alice", "Bob", "Claire", "Danielle"], ["Alice", "Danielle"]))
print (test(["Alice", "Bob", "Claire", "Danielle"], ["Alice", "Bob", "Claire", "Danielle"]))

@alka2696
Copy link

# Code of the function
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

# Test case for the function
def test_list(input_list, output_stack):
    if input_list==output_stack:
        return True
    else:
        return False

# test runs 
print (test_list([1,2,3,4], [1,2,3,4]))
print (test_list(["Alice", "Catherine", "Bob", "Charles","Denver"], "Bob"))
print (test_list(["Ron", "Harry", "Hermione"], ["Ron", "Harry", "Hermione"]))

@EricaAndreose
Copy link

from collections import deque

def test_stack_from_list(input_list, expected):
    result = stack_from_list(input_list)
    if expected == input_list:
        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

# Three test runs that return True
print(test_stack_from_list(("a", "b", "c", "d", "e"), ("a", "b", "c", "d", "e")))
print(test_stack_from_list(("Maria", "Angelo", "Jude"), ("Maria", "Angelo", "Jude")))
print (test_stack_from_list([1, 2, 3, 4], [1, 2, 3, 4]))

@matteo-guenci
Copy link

from collections import deque

def test_stack_from_list(input_list, output_stack):
    input_list = output_stack
    if input_list == output_stack:
        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(["Domenico Bini", "Guglielmo il Conquistatore", "John Cena"], ["Domenico Bini", "Guglielmo il Conquistatore", "John Cena"]))
print(test_stack_from_list(["Gundam", "Stanis La Rochelle", "Dionigi di Alicarnasso"], "Sir Ulrich von Liechtenstein"))
print(test_stack_from_list(["Adventure", "Il marchese del Grillo"], "Muteking"))

@lucia1299
Copy link

from collections import deque

def test_stack_from_list (input_list, expected):
    if input_list == 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

#Test case
print (test_stack_from_list(["water", "tea", "coffee", "alcohol"], ["water", "tea", "coffee", "alcohol"]))

#Console output
True

``

@giorgiacrosilla
Copy link

from collections import deque 

def test_stack_from_list(input_list, output_stack, expected):
    result = stack_from_list(input_list) == deque(output_stack)
    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((["a", "b", "c"]), (("a", "b")), False))
print(test_stack_from_list((["a", "b"]), (("a", "b")), True))

@SpedicatiDaniele
Copy link

SpedicatiDaniele commented Nov 12, 2022

Test case for the algorithm

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

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"], deque(["Ron", "Harry", "Hermione"])))

@ranacoskun
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):
  if stack_from_list(input_list) == expected:
    return True
  else: 
    return False

print(test_stack_from_list([0,1,2,3],deque([0,1,2,3])))
print(test_stack_from_list(['a','b','c','d'],deque(['a','b','c','d'])))

@corrado877
Copy link

from collections import deque

# Test case for the algorithm
def test_stack_from_list (input_list, expected):
    result= stack_from_list (input_list)
    if result==expected:
        return True
    else:
        return False

# Code of the algorithm
def stack_from_list (input_list):
    output_stack=deque ()
    for item in input_list:
        output_stack.append(item)
    return output_stack

# Input lists
F1_drivers_list=["Schumacher", "Senna", "Prost", "Lauda", "Stewart","Fangio"]
F1_tracks_list= ["Silverstone", "Singapore", "Monza", "Spa","Interlagos", "Jeddah"]
Actors_list= ["Johnny Depp", "Tom Hanks","Marlon Brando","Adrien Brody", "Keanu Reeves", "Jack Black"]

# Output_stacks
F1_drivers_stack=deque(F1_drivers_list)
F1_tracks_stack=deque(F1_tracks_list)
Actors_stack=deque(Actors_list)

# Three different test runs
print (test_stack_from_list (F1_drivers_list, F1_drivers_stack))
print (test_stack_from_list (F1_drivers_list, F1_drivers_stack))
print (test_stack_from_list (Actors_list, Actors_stack))

@irematmar
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):
  if stack_from_list(input_list) == expected:
    return True
  else:
    return False

print(test_stack_from_list(["Metallica", "Rammstein", "Muse", "Lamb of God"], deque(["Metallica", "Rammstein", "Muse", "Lamb of God"])))
print(test_stack_from_list(["1", "2", "3", "4"], deque(["a", "b", "c"])))

@essepuntato
Copy link
Contributor Author

Dear all,

Just a reminder about test-driven development. All the tests must be passed to claim that an algorithm returns what is expected. If a test execution returns False, the test is not passed.

If you need to check the non-compliance of the execution of a function on purpose, then you have to create an additional testing function that returns True if the condition of the test is not passed.

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