-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pulled Examples back from the fall class repo.
- Loading branch information
Showing
24 changed files
with
339 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
""" | ||
When squirrels get together for a party, they like to have cigars. | ||
A squirrel party is successful when the number of cigars is between | ||
40 and 60, inclusive. Unless it is the weekend, in which case there | ||
is no upper bound on the number of cigars. | ||
Return True if the party with the given values is successful, | ||
or False otherwise. | ||
""" | ||
|
||
|
||
def cigar_party(num, weekend): | ||
return num >= 40 and (num <= 60 or weekend) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env python | ||
|
||
""" | ||
Examples from: http://codingbat.com | ||
Put here so we can write unit tests for them ourselves | ||
""" | ||
|
||
# Python > Warmup-1 > sleep_in | ||
|
||
# The parameter weekday is True if it is a weekday, and the parameter | ||
# vacation is True if we are on vacation. | ||
# | ||
# We sleep in if it is not a weekday or we're on vacation. | ||
# Return True if we sleep in. | ||
|
||
|
||
def sleep_in(weekday, vacation): | ||
return not weekday or vacation | ||
|
||
|
||
# We have two monkeys, a and b, and the parameters a_smile and b_smile | ||
# indicate if each is smiling. | ||
|
||
# We are in trouble if they are both smiling or if neither of them is | ||
# smiling. | ||
|
||
# Return True if we are in trouble. | ||
|
||
def monkey_trouble(a_smile, b_smile): | ||
return a_smile is b_smile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
def safe_input(): | ||
try: | ||
the_input = input("\ntype something >>> ") | ||
except (KeyboardInterrupt, EOFError) as error: | ||
print("the error: ", error) | ||
error.extra_info = "extra info for testing" | ||
# raise | ||
return None | ||
return the_input | ||
|
||
def main(): | ||
safe_input() | ||
|
||
def divide(x,y): | ||
try: | ||
return x/y | ||
except ZeroDivisionError as err: | ||
print("you put in a zero!!!") | ||
print("the exeption:", err) | ||
err.args = (("very bad palce for a zero",)) | ||
err.extra_stuff = "all kinds of things" | ||
raise | ||
|
||
# if __name__ == '__main__': | ||
# main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,3 +61,4 @@ def test_10(): | |
|
||
def test_11(): | ||
assert cigar_party(39, True) is False | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python | ||
|
||
""" | ||
test file for codingbat module | ||
This version can be run with nose or py.test | ||
""" | ||
|
||
from codingbat import sleep_in, monkey_trouble | ||
|
||
|
||
# tests for sleep_in | ||
def test_false_false(): | ||
assert sleep_in(False, False) | ||
|
||
|
||
def test_true_false(): | ||
assert not (sleep_in(True, False)) | ||
|
||
|
||
def test_false_true(): | ||
assert sleep_in(False, True) | ||
|
||
|
||
def test_true_true(): | ||
assert sleep_in(True, True) | ||
|
||
|
||
# put tests for monkey_trouble here | ||
# monkey_trouble(True, True) → True | ||
# monkey_trouble(False, False) → True | ||
# monkey_trouble(True, False) → False | ||
|
||
def test_monkey_true_true(): | ||
assert monkey_trouble(True, True) | ||
|
||
def test_monkey_false_false(): | ||
assert monkey_trouble(False, False) | ||
|
||
def test_monkey_true_false(): | ||
assert monkey_trouble(True, False) is False | ||
|
||
# more! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env python | ||
|
||
""" | ||
pytest example of a parameterized test | ||
NOTE: there is a failure in here! can you fix it? | ||
""" | ||
import pytest | ||
|
||
|
||
# a (really simple) function to test | ||
def add(a, b): | ||
""" | ||
returns the sum of a and b | ||
""" | ||
return a + b | ||
|
||
# now some test data: | ||
|
||
test_data = [((2, 3), 5), | ||
((-3, 2), -1), | ||
((2, 0.5), 2.5), | ||
(("this", "that"), "this that"), | ||
(([1, 2, 3], [6, 7, 8]), [1, 2, 3, 6, 7, 8]), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize(("input", "result"), test_data) | ||
def test_add(input, result): | ||
assert add(*input) == result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python | ||
|
||
""" | ||
port of the random unit tests from the python docs to py.test | ||
""" | ||
|
||
import random | ||
import pytest | ||
|
||
|
||
seq = list(range(10)) | ||
|
||
|
||
def test_shuffle(): | ||
# make sure the shuffled sequence does not lose any elements | ||
random.shuffle(seq) | ||
# seq.sort() # commenting this out will make it fail, so we can see output | ||
print("seq:", seq) # only see output if it fails | ||
assert seq == list(range(10)) | ||
|
||
|
||
def test_shuffle_immutable(): | ||
"""should get a TypeError with an imutable type """ | ||
with pytest.raises(TypeError): | ||
random.shuffle((1, 2, 3)) | ||
|
||
|
||
def test_choice(): | ||
"""make sure a random item selected is in the original sequence""" | ||
element = random.choice(seq) | ||
assert (element in seq) | ||
|
||
|
||
def test_sample(): | ||
"""make sure all items returned by sample are there""" | ||
for element in random.sample(seq, 5): | ||
assert element in seq | ||
|
||
|
||
def test_sample_too_large(): | ||
"""should get a ValueError if you try to sample too many""" | ||
with pytest.raises(ValueError): | ||
random.sample(seq, 20) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import random | ||
import unittest | ||
|
||
|
||
class TestSequenceFunctions(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.seq = list(range(10)) | ||
|
||
def test_shuffle(self): | ||
# make sure the shuffled sequence does not lose any elements | ||
random.shuffle(self.seq) | ||
self.seq.sort() | ||
self.assertEqual(self.seq, list(range(10))) | ||
|
||
# should raise an exception for an immutable sequence | ||
self.assertRaises(TypeError, random.shuffle, (1, 2, 3)) | ||
|
||
def test_choice(self): | ||
element = random.choice(self.seq) | ||
self.assertTrue(element in self.seq) | ||
|
||
def test_sample(self): | ||
with self.assertRaises(ValueError): | ||
random.sample(self.seq, 20) | ||
for element in random.sample(self.seq, 5): | ||
self.assertTrue(element in self.seq) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.