Comments (15)
# 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])))
from 2018-2019.
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"])))
from 2018-2019.
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
from 2018-2019.
from 2018-2019.
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
from 2018-2019.
from 2018-2019.
# 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
from 2018-2019.
#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
from 2018-2019.
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
from 2018-2019.
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"])))
from 2018-2019.
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)
from 2018-2019.
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
from 2018-2019.
#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
from 2018-2019.
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"])))
from 2018-2019.
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:
-
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.
-
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
```
from 2018-2019.
Related Issues (20)
- Lecture "Brute-force algorithms", exercise 3 HOT 13
- Lecture "Brute-force algorithms", exercise 4 HOT 16
- Lecture "Brute-force algorithms", exercise 5 HOT 12
- Lecture "Organising information: unordered structures", exercise 1 HOT 21
- Lecture "Organising information: unordered structures", exercise 2 HOT 19
- Lecture "Organising information: unordered structures", exercise 3 HOT 17
- Lecture "Recursion", exercise 1 HOT 18
- Lecture "Recursion", exercise 2 HOT 20
- Lecture "Divide and conquer algorithm", exercise 1 HOT 12
- Lecture "Divide and conquer algorithm", exercise 2 HOT 5
- Lecture "Dynamic programming algorithms", exercise 1 HOT 15
- Lecture "Dynamic programming algorithms", exercise 2 HOT 8
- Lecture "Organising information: trees", exercise 1 HOT 4
- Lecture "Organising information: trees", exercise 2 HOT 4
- Lecture "Organising information: graphs", exercise 1 HOT 14
- Lecture "Organising information: graphs", exercise 2 HOT 12
- Christmas present game + bonus Python exercise
- Lecture "Backtracking algorithms", exercise 1 HOT 3
- Lecture "Greedy algorithms", exercise 1 HOT 2
- Question about an exercise from last year
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 2018-2019.