Пример #1
0
import util.test as test


def two_x_plus_four_y(x, y):
    """
    Parameters: integers x, y
    Returns: four times x plus two times y
    """
    # Start of your code
    return None
    # End of your code


test.test_equals(two_x_plus_four_y(1, 2), 8)
test.test_equals(two_x_plus_four_y(3, 5), 22)
test.test_equals(two_x_plus_four_y(10, 1), 42)
Пример #2
0
import util.test as test


"""
Define a function filter_longer_than():
    Parameters: a list (of strings) and an integer
    Returns: a list containing only the strings of the original list that have length
        greater than the given integer
"""
# Start of your code

# End of your code


test.test_equals(filter_longer_than(["", "1", "12", "123", "1234"], 3), ["1234"])
test.test_equals(filter_longer_than(["ab", "123"], 1), ["ab", "123"])
Пример #3
0
import util.test as test
"""
Define a function longer_than_two():
    Parameters: a string
    Returns: True if the length of the string is greater than two, False otherwise
"""
# Start of your code

# End of your code

test.test_equals(longer_than_two('a'), False)
test.test_equals(longer_than_two('ab'), False)
test.test_equals(longer_than_two('abc'), True)
Пример #4
0
from linked_list_utils import Node, construct_linked_list, test_linked_list_equals
import util.test as test
"""
Define a function linked_list_append():
    Parameters: a linked list, a value
    Side Effect: appends the value to the linked list
    Returns: N/A
"""
# Start of your code

# End of your code

list = construct_linked_list([1])
linked_list_append(list, 2)
test.test_equals(test_linked_list_equals(list, [1, 2]), True)

list = construct_linked_list([1, 3, 5])
linked_list_append(list, 7)
test.test_equals(test_linked_list_equals(list, [1, 3, 5, 7]), True)
Пример #5
0
import util.test as test


def two_s1_four_s2(s1, s2):
    """
    Parameters: strings s1, s2
    Returns:
        a string formed by concatenating two copies of s1 and four copies of s2
    """
    # Start of your code
    return None
    # End of your code


test.test_equals(two_s1_four_s2('a', 'b'), 'aabbbb')
test.test_equals(two_s1_four_s2('12', 'xy'), '1212xyxyxyxy')
Пример #6
0
from linked_list_utils import Node, construct_linked_list, test_linked_list_equals
import util.test as test
"""
Define a function linked_list_delete():
    Parameters: a linked list, a value
    Side Effect: deletes the value from the linked list, if it exists
    Returns: N/A
"""
# Start of your code

# End of your code

list = construct_linked_list([1, 2])
linked_list_delete(list, 2)
test.test_equals(test_linked_list_equals(list, [1]), True)

list = construct_linked_list([1, 3, 5])
linked_list_delete(list, 3)
test.test_equals(test_linked_list_equals(list, [1, 5]), True)

list = construct_linked_list([1, 3, 5])
linked_list_delete(list, 5)
test.test_equals(test_linked_list_equals(list, [1, 3]), True)
Пример #7
0
import util.test as test
"""
Define a function is_even():
    Parameters: an integer
    Returns: True if the integer is even, False otherwise
"""
# Start of your code

# End of your code

test.test_equals(is_even(0), True)
test.test_equals(is_even(1), False)
test.test_equals(is_even(2), True)
test.test_equals(is_even(3), False)
test.test_equals(is_even(4), True)
Пример #8
0
import util.test as test


def pairs_are_equal(a, b, c, d):
    """
    Parameters: booleans a, b, c, d
    Returns:
        True if a, b are equal and c, d are equal
        False otherwise
    """
    # Start of your code
    return None
    # End of your code


test.test_equals(pairs_are_equal(True, True, False, False), True)
test.test_equals(pairs_are_equal(True, True, True, True), True)
test.test_equals(pairs_are_equal(False, False, False, False), True)
test.test_equals(pairs_are_equal(False, False, True, True), True)
test.test_equals(pairs_are_equal(True, False, True, True), False)
test.test_equals(pairs_are_equal(False, True, True, True), False)
test.test_equals(pairs_are_equal(False, False, True, False), False)
test.test_equals(pairs_are_equal(False, False, False, True), False)
Пример #9
0
import util.test as test


def bubble_sort(nums):
    return None


test.test_equals(bubble_sort([4, 2, 5, 3, 1]), [1, 2, 3, 4, 5])
Пример #10
0
import util.test as test
"""
Define a function contains_none():
    Parameters: a 2D list
    Returns: True if the 2D list None as an element in a row, False otherwise 
"""
# Start of your code

# End of your code

test.test_equals(contains_none([[0]]), False)
test.test_equals(contains_none([[None]]), True)
test.test_equals(contains_none([[0, 1, 2], [4, None, 4], [1, 6, 7]]), True)
test.test_equals(contains_none([[1, 2, 3], [4, 5, 6], [7, 7, 7]]), False)
Пример #11
0
import util.test as test


def insertion_sort(nums):
    return None


test.test_equals(insertion_sort([4, 2, 5, 3, 1]), [1, 2, 3, 4, 5])
Пример #12
0
import util.test as test
"""
Define a function sum_2d_list():
    Parameters: a 2D list
    Returns: sum of all the elements in the 2D list
"""
# Start of your code

# End of your code

test.test_equals(sum_2d_list([[1, 2], [3, 4]]), 10)
test.test_equals(sum_2d_list([[1], [2, 3]]), 6)
Пример #13
0
import util.test as test
"""
Define a function nums_greater_than():
    Parameters: a list, an integer
    Returns: a set containing all the numbers in the list greater than the given integer
"""

test.test_equals(nums_greater_than([], 2), set())
test.test_equals(nums_greater_than([1, 2, 3], 2), {3})
test.test_equals(nums_greater_than([1, 2, 3], 0), {1, 2, 3})
test.test_equals(nums_greater_than([1, 2, 3], 4), set())
Пример #14
0
import util.test as test


def mystery_func(num):
    return num * 31 + num


"""
Define a function add_mystery_func_results():
    Parameters: two ints
    Returns: the sum of calling mystery_func() on each int
"""
# Start of your code

# End of your code


test.test_equals(add_mystery_func_results(1, 2), mystery_func(1) + mystery_func(2))
test.test_equals(add_mystery_func_results(2, 3), mystery_func(2) + mystery_func(3))
test.test_equals(add_mystery_func_results(4, 11), mystery_func(4) + mystery_func(11))
Пример #15
0
import util.test as test
"""
Define a function index_of_max_in_2d_list():
    Parameters: a 2D list of integers
    Returns: a tuple containing the indexes of the greatest number in the 2D list (None if empty)
"""

test.test_equals(index_of_max_in_2d_list([[]]), None)
test.test_equals(index_of_max_in_2d_list([[1]]), (0, 0))
test.test_equals(index_of_max_in_2d_list([[1, 2], [3, 4]]), (1, 1))
test.test_equals(index_of_max_in_2d_list([[1, 4], [3, 2]]), (0, 1))
test.test_equals(index_of_max_in_2d_list([[-1, -2], [-3, -4]]), (0, 0))
Пример #16
0
import util.test as test


"""
Define a function get_unique_elements():
    Parameters: a list
    Returns: a set containing the unique elements of the given list 
"""
# Start of your code

# End of your code


test.test_equals(get_unique_elements([1, 1]), {1})
test.test_equals(get_unique_elements([1, 1, 2, 3]), {1, 2, 3})
test.test_equals(get_unique_elements([1, 2, 2]), {1, 2})
Пример #17
0
import util.test as test


def sum_of_ints_between(lo, hi):
    """
    Implement this function using a while loop.
    Parameters: integers lo, hi
    Returns: sum of the integers from lo to hi (inclusive)
    """
    # Start of your code
    return None
    # End of your code


test.test_equals(sum_of_ints_between(1, 2), 3)
test.test_equals(sum_of_ints_between(1, 5), 15)
Пример #18
0
import util.test as test
"""
Define a function filter_longer_than_two():
    Parameters: a list (of strings)
    Returns: a list containing only the strings of the original list that have length
        greater than two
"""
# Start of your code

# End of your code

test.test_equals(filter_longer_than_two([]), [])
test.test_equals(filter_longer_than_two(["", "1", "12", "123", "1234"]),
                 ["123", "1234"])
test.test_equals(filter_longer_than_two(["abc", "123"]), ["abc", "123"])
Пример #19
0
import util.test as test


def second_and_last(l):
    """
    Parameters: list l
    Returns: a list that contains the second and last elements of l
    """
    # Start of your code
    return None
    # End of your code


test.test_equals(second_and_last([1, 2, 3, 4]), [2, 4])
test.test_equals(second_and_last([1, 2, 3]), [2, 3])
test.test_equals(second_and_last([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), [2, 10])
Пример #20
0
import util.test as test


"""
Define a function is_matching_parentheses():
    Parameters: a string of parentheses
    Returns: True if the parentheses are correctly matching, False otherwise
"""
# Start of your code

# End of your code


test.test_equals(is_matching_parentheses(')('), False)
test.test_equals(is_matching_parentheses('()('), False)
test.test_equals(is_matching_parentheses('('), False)
test.test_equals(is_matching_parentheses('())('), False)

test.test_equals(is_matching_parentheses(''), True)
test.test_equals(is_matching_parentheses('()'), True)
test.test_equals(is_matching_parentheses('(()())'), True)
test.test_equals(is_matching_parentheses('()()'), True)
test.test_equals(is_matching_parentheses('(())'), True)

Пример #21
0
import util.test as test


def is_positive(x):
    """
    Parameters: integer x
    Returns: 'true' if x is positive, 'false' otherwise
    """
    # Start of your code
    return None
    # End of your code


test.test_equals(is_positive(5), 'true')
test.test_equals(is_positive(0), 'false')
test.test_equals(is_positive(-1), 'false')
Пример #22
0
import util.test as test


def remove_first_two_and_last_two(l):
    """
    Parameters: list l
    Returns: a list formed by removing the first two and last two elements of l
    """
    # Start of your code
    return None
    # End of your code


test.test_equals(remove_first_two_and_last_two([1, 2, 3, 4, 5, 6]), [3, 4])
test.test_equals(remove_first_two_and_last_two([1, 2, 3, 4]), [])
test.test_equals(
    remove_first_two_and_last_two([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
    [3, 4, 5, 6, 7, 8])
Пример #23
0
import util.test as test


def selection_sort(nums):
    return None


test.test_equals(selection_sort([4, 2, 5, 3, 1]), [1, 2, 3, 4, 5])
Пример #24
0
import util.test as test


"""
Define a function two_greatest():
    Parameters: a list of integers
    Returns: a tuple containing two values:
        First value is the greatest integer in the list (None if there is no greatest integer)
        Second value is the second greatest integer in the list (None if there is no second greatest integer)
"""


test.test_equals(two_greatest([]), (None, None))
test.test_equals(two_greatest([2]), (2, None))
test.test_equals(two_greatest([1, 2]), (2, 1))
test.test_equals(two_greatest([3, 4, 2, 1, 5]), (5, 4))
test.test_equals(two_greatest([-1, -2, -3]), (-1, -2))
Пример #25
0
import util.test as test
"""
Define a function filter_greater_than():
    Parameters: a list and an integer
    Returns: a list containing only the elements of the original list that are greater
        than the given integer
"""
# Start of your code

# End of your code

test.test_equals(filter_greater_than([-1, 0, 5, 7, 10], 6), [7, 10])
test.test_equals(filter_greater_than([1, 2], 0), [1, 2])
Пример #26
0
import util.test as test
"""
Define a function index_of_max_of_list():
    Parameters: a list of integers
    Returns: the index of the largest integer in the list (-1 if the list is empty)
"""

test.test_equals(index_of_max_of_list([]), -1)
test.test_equals(index_of_max_of_list([1]), 0)
test.test_equals(index_of_max_of_list([1, 3]), 1)
test.test_equals(index_of_max_of_list([-1, -2, 5, 1]), 2)
test.test_equals(index_of_max_of_list([-1, -2, -3]), 0)
test.test_equals(index_of_max_of_list([-3, -2, -1]), 2)
Пример #27
0
import util.test as test


"""
Define a function exists_triple():
    Parameters: a 3x3 2D list
    Returns: True if there is a row, column, or diagonal with 3 of the same element, False otherwise
"""
# Start of your code

# End of your code


# Triple row exists
test.test_equals(exists_triple([[0, 0, 0], [1, 2, 3], [4, 5, 6]]), True)
test.test_equals(exists_triple([[0, 1, 2], [4, 4, 4], [5, 6, 7]]), True)
test.test_equals(exists_triple([[1, 2, 3], [4, 5, 6], [7, 7, 7]]), True)

# Triple column exists
test.test_equals(exists_triple([[0, 1, 2], [0, 3, 4], [0, 5, 6]]), True)
test.test_equals(exists_triple([[0, 1, 2], [3, 1, 4], [5, 1, 6]]), True)
test.test_equals(exists_triple([[1, 2, 0], [3, 4, 0], [5, 6, 0]]), True)

# Triple diagonal exists
test.test_equals(exists_triple([[0, 1, 2], [3, 0, 4], [5, 6, 0]]), True)
test.test_equals(exists_triple([[1, 2, 0], [3, 0, 4], [0, 5, 6]]), True)

# No triple exists
test.test_equals(exists_triple([[0, 1, 2], [3, 4, 5], [6, 7, 8]]), False)
test.test_equals(exists_triple([[0, 0, 1], [2, 3, 4], [5, 6, 7]]), False)
Пример #28
0
import util.test as test
"""
Define a function equal_length():
    Parameters: two strings
    Returns: True if the length of the two strings are equal, False otherwise
"""
# Start of your code

# End of your code

test.test_equals(equal_length('a', 'b'), True)
test.test_equals(equal_length('abc', '123'), True)
test.test_equals(equal_length('a', 'aa'), False)
Пример #29
0
import util.test as test


def swap_first_and_last(s):
    """
    Parameters: string s
    Returns: a string formed by swapping the first and last characters of s
    """
    # Start of your code
    return None
    # End of your code


test.test_equals(swap_first_and_last('ab'), 'ba')
test.test_equals(swap_first_and_last('abc'), 'cba')
test.test_equals(swap_first_and_last('abcde'), 'ebcda')
Пример #30
0
import util.test as test
"""
Define a function last_two():
    Parameters: two strings
    Returns: a string formed by concatenating the last two characters of the first string with the last two
        characters of the second string
"""
# Start of your code

# End of your code

test.test_equals(last_two('abc', '123'), 'bc23')