comp-think / 2019-2020

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. 2019/2020).
Other
12 stars 3 forks source link

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

Open essepuntato opened 4 years ago

essepuntato commented 4 years ago

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

arcangelo7 commented 4 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 result == expected:
        return True
    else:
        return False

print(test_stack_from_list([1, 2, 3, 4, 5], deque([1, 2, 3, 4, 5])))
# It prints True
sntcristian commented 4 years ago

Screenshot (6)

FrancescoFernicola commented 4 years ago

Screenshot 2019-11-06 01 06 07

noreanystrom commented 4 years ago

image

essepuntato commented 4 years ago

Hi all,

please find attached my personal solution – also available online:

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

# 'stack_from_list' defined in Listing 2.

# Three different test runs
print(test_stack_from_list([], deque()))
print(test_stack_from_list([1, 2, 3, 4, 5], deque([1, 2, 3, 4, 5])))

Just a suggestion: when you test your code (any code, not just this exercise), please consider adding some tests which use borderline cases, such as using empty (but admissible) values, such as an empty list in this exercise.

arimuti commented 4 years ago

image The output is: True

morinigiu commented 4 years ago
Schermata 2019-12-08 alle 16 35 59