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 algorithms", exercise 2 #16

Open
essepuntato opened this issue Nov 26, 2018 · 15 comments
Open

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

essepuntato opened this issue Nov 26, 2018 · 15 comments
Labels
Exercise The exercises that are introduced in the lectures.

Comments

@essepuntato
Copy link
Contributor

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

@essepuntato essepuntato added the Exercise The exercises that are introduced in the lectures. label Nov 26, 2018
@delfimpandiani
Copy link

# 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
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([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
Copy link

cattura2

@LuciaGiagnolini12
Copy link

LuciaGiagnolini12 commented Nov 26, 2018

# 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
Copy link

#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
Copy link

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
Copy link

federicabologna commented Nov 26, 2018

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
Copy link

MattiaSpadoni commented Nov 27, 2018

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

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
Copy link

andreamust commented Nov 28, 2018

#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
Copy link

tceron commented Nov 28, 2018

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
Copy link

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
Copy link

screenshot at nov 29 17-29-45

@essepuntato
Copy link
Contributor Author

essepuntato commented Nov 30, 2018

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
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Exercise The exercises that are introduced in the lectures.
Projects
None yet
Development

No branches or pull requests