comp-think / 2018-2019

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. 2018/2019).
Other
30 stars 8 forks source link

Lecture "Brute-force algorithms", exercise 2 #16

Open essepuntato opened 5 years ago

essepuntato commented 5 years ago

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

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

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

# Three different test runs
print(test_stack_from_list([15, 38, 56, 4], deque([15, 38, 56, 4])))
print(test_stack_from_list(["A", "B", "C", "D"], deque(["A", "B", "C", "D"])))
print(test_stack_from_list(["A", 78, "Hola", 6], deque(["A", 78, "Hola", 6])))
ilsamoano commented 5 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([12,13,14], deque([12,13,14])))
print (test_stack_from_list(["A",13,"B"], deque(["A",13,"B"])))
print (test_stack_from_list(["A",13,"B"], deque(["B",13,"A"])))

True
True
False
EleonoraPeruch commented 5 years ago

cattura2

LuciaGiagnolini12 commented 5 years ago
# Test case for the algorithm
def test_stack_from_list(chapters_list, expected):
    result = stack_from_list(chapters_list)
    if expected == result:
        return True
    else:
        return False

# Code of the algorithm
from collections import deque
def stack_from_list(chapters_list):
    chapters_stack = deque()
    for item in chapters_list:
        chapters_stack.append(item)

    return chapters_stack

# Two different test runs
print(test_stack_from_list(["chapter 1", "chapter 2", "chapter 3", "chapter 4"], deque(["chapter 1", "chapter 2", "chapter 3", "chapter 4"]))) # true
print(test_stack_from_list(["Owl Post", "Aunt Marge's Big Mistake", "The Knight Bus", "The Leaky Cauldron"], deque(["The Boggart in the Wardrobe", "Aunt Marge's Big Mistake", "The Knight Bus", "The Leaky Cauldron"]))) # false
lisasiurina commented 5 years ago

Test case for the algorithm

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([“Part 1", “Part 2", “Part 3", “Part 4"], deque([“Part 1", “Part 2", “Part 3", “Part 4”]))) Output: True print(test_stack_from_list([“) “Poor Folk ", “The Double“, "The Idiot”, “A Gentle Creature“], deque(["The Double", “Poor Folk”, “The Idiot“, “A Gentle Creature“]))) Output: False

dersuchendee commented 5 years ago

Test case for the algorithm

def stack_from_list(input_list, value_to_search, expected): result =stack_from_list(input_list, value_to_search) if expected == result: return True else: return False

federicabologna commented 5 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(list(["a", "b", "c"]), deque(["c", "b", "a"])))
print(test_stack_from_list(list(["d", "e", "f"]), deque(["d", "e", "f"])))
MattiaSpadoni commented 5 years ago

I cannot understand why Github don't write the indentation in this case.

def test(input_list, expected): result = createstuck(input_list) if expected == result: return True else: return False

from collections import deque def createstack(input_list): stack = deque() for item in input_list: stack.append(item)

return stack

some tests

print(createstack([55, 44, 32, 27], deque([27, 32, 44, 55]))) (True) print(createstack(["A", "B", "C", "D"], deque(["A", "B", "C", "D"]))) (false)

friendlynihilist commented 5 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()  # the stack to create

    for item in input_list:
        output_stack.append(item)

    return output_stack

print(test_stack_from_list(["a", "b", "c"], deque(["a", "b", "c"]))) #true
print(test_stack_from_list(["A", "b", "c"], deque(["a", "b", "c"]))) #false
print(test_stack_from_list([1, 2, 3], deque([3, 2, 1]))) #false
SeverinJB commented 5 years ago
from collections import deque

# Testing Algorithm
def test_stack_from_list(input_list, expected): 
    result = stack_from_list(input_list) 
    if expected == result: 
        return True 
    else: 
        return False 

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

# Testing Input
print(test_stack_from_list([1,2,3,4,5], deque([1,2,3,4,5]))) 
print(test_stack_from_list(["the","answer","to","life","universe","and","everything"], deque(["the","answer","to","life","universe","and","everything"]))) 
print(test_stack_from_list(["the","ultimate","question"], deque(["the","ultimate","question"])))
andreamust commented 5 years ago
#test stack

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()
    # 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([62, 54, 21, 6], deque([62, 54, 21, 6]))) #True
print(test_stack_from_list(["Anna", "Marco", "Luigi", "Andrea"], deque(["Anna", "Marco", "Luigi", "Andrea"])))   #True
print(test_stack_from_list(["red", "blue", "yellow"], deque(["red", "pink"])))   #False
tceron commented 5 years ago

from collections import deque

test case for the algorithm

def test_stack_from_list(input_list, expected):

result = stack_from_list(input_list)
if expected == result:
    return True
else:
    return False

code of the algorithm

def stack_from_list(input_list): output_stack = deque() #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 run

print(test_stack_from_list([10, 16, 82, 5], deque([10, 16, 82, 5]))) print(test_stack_from_list(["zebra", "donkey", "hippo", "giraffe"], deque(["zebra", "donkey", "hippo", "giraffe"])))

simayguzel commented 5 years ago
def test_output_stack(input_list, expected):
    result = output_stack(input_list)
    if expected == result:
        return True
    else:
        return False
from collections import deque

def output_stack(input_list):
    output_stack = deque()

    for item in input_list:
        output_stack.append(item)

    return output_stack

print (test_output_stack([19,18,17], deque([19,18,17])))
print (test_output_stack(["D","E","F"], deque(["D","E","F"])))
print (test_output_stack([22,"A","B"], deque([22,"A","B"])))

True
True
True
DavideApolloni commented 5 years ago

screenshot at nov 29 17-29-45

essepuntato commented 5 years ago

Hi all,

First, you can find my solution as Python file in the GitHub repository.

a few very important comments for this (and future) exercise:

  1. test-driven development: all the tests must be passed in order to claim that an algorithm returns what it is expected. If a test execution return False, the test is not passed. If you need to check the non-compliancy 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. Remeber: first write the test, then develop the algorithm/function.

  2. Python code and indentation: please, in your answers to the various questions, if you have to write down a Python code, be sure that the correct indent is preserved by previewing your post before to publish it. You can use the ``` environment for defining your Python code, e.g.:


```
write your Python code here
```