Created
October 14, 2021 18:54
-
-
Save jjfiv/6fcd84a8ab926541de2734ed106e4f3d to your computer and use it in GitHub Desktop.
CS145-F2021 Project 4 Starter Code
This file contains hidden or 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
def maxmin(xs): | |
""" | |
This function simultaneously computes the maximum | |
and the minimum values present in a list. | |
Input: a list of ordered values. | |
Returns: [maximum, minimum] or [None, None] for an empty list. | |
""" | |
minimum = None | |
maximum = None | |
for x in xs: | |
if minimum == None or x < minimum: | |
minimum = x | |
if maximum == None or x > maximum: | |
maximum = x | |
return [maximum, minimum] | |
def test_maxmin(): | |
# Your job is to polish up these tests. | |
assert maxmin([]) == [None, None] | |
assert maxmin([1]) == TODO | |
assert maxmin([1, 2, 3]) == TODO | |
assert maxmin([3, 2, 1]) == TODO | |
def swap(xs, i, j): | |
# HINT: do not use a loop. | |
# WARNING: | |
# If you do not use a temporary/local variable... | |
# then you MUST have a citation. | |
pass | |
def test_swap(): | |
xs = [1, 2, 3, 4] | |
swap(xs, 0, 3) | |
assert xs == [4, 2, 3, 1] | |
swap(xs, 1, 3) | |
assert xs == [4, 1, 3, 2] | |
def is_sorted(xs): | |
# HINT: can you put a <= between adjacent pairs of elements? | |
return False | |
def test_is_sorted(): | |
assert is_sorted([1, 2, 3, 4]) | |
assert not is_sorted([2, 1, 3, 4]) | |
assert not is_sorted([1, 2, 3, 5, 4]) | |
def factors(n): | |
output = [] | |
# HINT: the loop will look a lot like is_prime, but collect | |
return output | |
def test_factors(): | |
assert factors(2) == [1, 2] | |
assert factors(4) == [1, 2, 4] | |
assert factors(17) == [1, 17] | |
assert factors(24) == [1, 2, 3, 4, 6, 8, 12, 24] | |
def median(xs): | |
# Look, I sorted it if you need: | |
if not is_sorted(xs): | |
xs.sort() | |
# The median is the middle item or the average of the two middle items. | |
return 99 | |
def test_median(): | |
assert median([]) == 0 | |
assert median([2]) == 2 | |
assert median([1, 2, 3, 4, 5]) == 3 | |
assert median([4, 5, 2, 3, 1]) == 3 | |
assert abs(median([1, 2, 3, 4]) - 2.5) < 0.0001 | |
def is_palindrome(data): | |
# palindromes are the same forward and backward | |
# negative indexing may make this easier... | |
return False | |
def test_is_palindrome(): | |
assert is_palindrome("abcba") | |
assert is_palindrome("abba") | |
assert not is_palindrome("abxyba") | |
assert not is_palindrome("xy") | |
# empty should be True. | |
assert is_palindrome("") | |
assert is_palindrome([]) | |
# should work for lists as well, why not? | |
assert is_palindrome([1, 2, 1]) | |
assert not is_palindrome([2, 3]) | |
assert not is_palindrome([1, 2, 3, 4, 2, 1]) | |
if __name__ == "__main__": | |
# Is the import line crashing? Watch the video for how to install with Thonny. | |
# Tools > Manage Packages in menu | |
# Type pytest | |
# Click Install | |
import pytest | |
# This will run all the test_* functions and pretty-print where they're getting stuck! | |
pytest.main([__file__]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment