from tester import runtests

# is_one_away should return true if str1 can be modified to be equal to str2
# using only one insertion, one deletion, or one modification (of one character).

def is_one_away(str1, str2):
    # Your code here
    pass

tests = [
    [['blarg', 'blarg'], True],
    [['blar', 'blarg'], True],
    [['bla', 'blarg'], False],
    [['', 'b'], True],
    [['brarg', 'blarg'], True],
    [['blrarg', 'blarg'], True],
    [['', ''], True],
    [['blarg', 'blargg'], True],
    [['blarg', 'blarggg'], False],
]

runtests(tests, is_one_away)
Exemple #2
0
from tester import runtests

# urlify should take in a list of characters, and replace spaces with '%20'.
# it must do this inplace, but is guaranteed that there will be trailing
# whitespace to accomodate the extra characters that will need to be written.

def urlify(lstring):
    # Your code here
    pass

tests = [
    [[list('this is a url string        ')], list('this%20is%20a%20url%20string')],
    [[list('smalltest')], list('smalltest')], 
    [[list('')], list('')], 
    [[list(' b  ')], list('%20b')],
    [[list('blarg arg  ')], list('blarg%20arg')],
]

runtests(tests, urlify, inplace=True)
from linkedlist import linkedlist_from_list
from tester import runtests

# remove_dups should modify ll inplace to remove any nodes of the linked list
# with duplicate values.

def remove_dups(ll):
    # Your code here
    pass

tests = [
    [[linkedlist_from_list([1, 2, 3, 3, 4, 5])], linkedlist_from_list([1, 2, 3, 4, 5])],
    [[linkedlist_from_list([5, 4, 3, 2, 5 ,1])], linkedlist_from_list([5, 4, 3, 2, 1])],
    [[linkedlist_from_list([1, 1])], linkedlist_from_list([1])],
    [[linkedlist_from_list([1])], linkedlist_from_list([1])],
    [[linkedlist_from_list(['always', 'always', 'remove', 'duplicates'])], linkedlist_from_list(['always', 'remove', 'duplicates'])],
    [[linkedlist_from_list([])], linkedlist_from_list([])],
    [[linkedlist_from_list([1, 2, 3, 1, 3, 0])], linkedlist_from_list([1, 2, 3, 0])]
]

runtests(tests, remove_dups, inplace=True)
from tester import runtests

# is_unique should return true if there are no duplicate characters in astring

def is_unique(astring):
    # Your code here
    pass

tests = [
    [['this'], True],
    [['abcdefgh'], True],
    [['abcdefgha'], False],
    [['HAha'], True],
    [['blarg'], True],
    [['letters'], False],
    [[''], True],
]

runtests(tests, is_unique)
from linkedlist import linkedlist_from_list
from tester import runtests

# palindrome should return True if ll represents a palindrome.


def palindrome(ll):
    # Your code here
    pass


tests = [
    [[linkedlist_from_list([1, 2, 3, 2, 1])], True],
    [[linkedlist_from_list([1, 2, 3, 3, 2, 1])], True],
    [[linkedlist_from_list([1, 2, 3, 4, 2, 1])], False],
    [[linkedlist_from_list([1, 2, 3, 3, 2, 2])], False],
    [[linkedlist_from_list([1, 2])], False],
    [[linkedlist_from_list([1])], True],
    [[linkedlist_from_list([])], True],
]

runtests(tests, palindrome)
from linkedlist import linkedlist_from_list
from tester import runtests

# palindromically_reorder should modify the passed linked list inplace such that
# the i'th node of the list will be followed by the N-1-i'th node.
# Sounds confusing. Is a little. Have a look at the test cases.

def palindromically_reorder(ll):
    # Your code here
    pass


tests = [
    [[linkedlist_from_list([1, 2, 3, 4, 5])], linkedlist_from_list([1, 5, 2, 4, 3])],
    [[linkedlist_from_list([1, 2, 3, 3, 4, 5])], linkedlist_from_list([1, 5, 2, 4, 3, 3])],
    [[linkedlist_from_list([])], linkedlist_from_list([])],
    [[linkedlist_from_list([1])], linkedlist_from_list([1])],
    [[linkedlist_from_list([1, 2])], linkedlist_from_list([1, 2])],
    [[linkedlist_from_list([1, 2, 3])], linkedlist_from_list([1, 3, 2])],
]

runtests(tests, palindromically_reorder, inplace=True)
    ra = []
    rb = []
    for thing in testcase:
        if type(thing) is int:
            test.push(thing)
        else:
            ra.append(test.pop())
        rb.append(test.number_of_stacks())
    return (ra, rb)

(r1a, r1b) = results_from_testcase([1, 2, 'p', 'p'])
(r2a, r2b) = results_from_testcase([1, 2, 3, 4, 5, 'p', 'p', 'p', 'p', 'p', 'p'])
(r3a, r3b) = results_from_testcase([1, 2, 3, 4, 5, 'p', 'p', 'p', 'p', 'p', 'p', 'p'])
(r4a, r4b) = results_from_testcase([2, 5, 'p', 6, 7, 'p', 8, 9, 10, 'p', 'p', 'p', 'p', 'p'])
(r5a, r5b) = results_from_testcase(['p', 1, 2, 3, 4, 5, 'p'])

tests = [
    [[r1a], [2, 1]],
    [[r1b], [1, 1, 1, 0]],
    [[r2a], [5, 4, 3, 2, 1, None]],
    [[r2b], [1, 1, 1, 1, 2, 1, 1, 1, 1, 0, 0]],
    [[r3a], [5, 4, 3, 2, 1, None, None]],
    [[r3b], [1, 1, 1, 1, 2, 1, 1, 1, 1, 0, 0, 0]],
    [[r4a], [5, 7, 10, 9, 8, 6, 2]],
    [[r4b], [1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 0]],
    [[r5a], [None, 5]],
    [[r5b], [0, 1, 1, 1, 1, 2, 1]],
]

runtests(tests)
    ],
    [
        [[
            [1]
        ]],
        [
            [1]
        ]
    ],
    [
        [[]],
        []
    ],
    [
        [[
            [1, 2, 3, 4],
            [5, 6, 7, 8],
            [9, 10, 11, 12],
            [13, 14, 15, 16]
        ]],
        [
            [13, 9, 5, 1],
            [14, 10, 6, 2],
            [15, 11, 7, 3],
            [16, 12, 8, 4]
        ]
    ]
]

runtests(tests, rotate_matrix, inplace=True)
Exemple #9
0
from linkedlist import linkedlist_from_list
from tester import runtests

# sum_lists should take two linked lists, treat them as if they were decimal numbers,
# and return a list that represents the sum of those numbers. (see tests for examples)

def sum_lists(l1, l2):
    # Your code here
    pass

tests = [
    [[linkedlist_from_list([1, 2, 3, 3, 4]), linkedlist_from_list([1, 2, 3, 3, 4])], linkedlist_from_list([2, 4, 6, 6, 8])],
    [[linkedlist_from_list([1, 5, 0]), linkedlist_from_list([1, 5, 0])], linkedlist_from_list([3, 0, 0])],
    [[linkedlist_from_list([9, 9]), linkedlist_from_list([1])], linkedlist_from_list([1, 0, 0])],
    [[linkedlist_from_list([9]), linkedlist_from_list([9])], linkedlist_from_list([1, 8])],
    [[linkedlist_from_list([]), linkedlist_from_list([])], linkedlist_from_list([])],
    [[linkedlist_from_list([9, 9, 9, 9, 9]), linkedlist_from_list([2])], linkedlist_from_list([1, 0, 0, 0, 0, 1])],
    [[linkedlist_from_list([1, 0, 0, 0, 0]), linkedlist_from_list([2])], linkedlist_from_list([1, 0, 0, 0, 0, 2])],
]

runtests(tests, sum_lists)
from tester import runtests

# string_compress should return a string that has had any runs of letters
# condensed into just that letter, followed by the number of that letter.
# for example, 'aaaaaaa' -> 'a7', 'aaabbbb' -> 'a3b4'
# If the "compressed" string is larger than the original string,
# the function should instead return the original string.

def string_compress(string):
    # Your code here
    pass

tests = [
    [['abcdef'], 'abcdef'], 
    [['aaaaaaa'], 'a7'],
    [['aaabbbb'], 'a3b4'],
    [[''], ''],
    [['aaabbbabcdefg'], 'aaabbbabcdefg'],
    [['aaabbbabcdefgggggggggggggggggggggggg'], 'a3b3a1b1c1d1e1f1g24'],
]

runtests(tests, string_compress)
Exemple #11
0
def main():
    import tester
    tester.runtests(__file__)
from tester import runtests

# zero_matrix takes a matrix m (similar to the previous question), and modifies
# it inplace to zero-out any rows and columns that had a zero on them.


def zero_matrix(m):
    # Your code here
    pass


tests = [
    [[[[1, 2, 3], [4, 0, 6], [7, 8, 9]]], [[1, 0, 3], [0, 0, 0], [7, 0, 9]]],
    [[[[1, 2], [0, 4]]], [[0, 2], [0, 0]]],
    [[[[1]]], [[1]]],
    [[[]], []],
    [
        [[[0, 0, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]],
        [[0, 0, 0, 0], [0, 0, 7, 8], [0, 0, 11, 12], [0, 0, 15, 16]],
    ],
    [
        [[[0, 2, 3, 4], [5, 0, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]],
        [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 11, 12], [0, 0, 15, 16]],
    ],
]

runtests(tests, zero_matrix, inplace=True)
from tester import runtests

# check_permutation should return true if str1 is a permutation of str2
# a permutation means that the letters are all the same, but may be rearanged.

def check_permutation(str1, str2):
    # Your code here
    pass

tests = [
    [['thing', 'thang'], False],
    [['thing', 'ting'], False],
    [['thing', 'thing'], True],
    [['thing', 'gnith'], True],
    [['', ''], True],
    [['', 'blarg'], False],
    [['blarggg', 'blarg'], False],
]

runtests(tests, check_permutation)
from tester import runtests
from collections import defaultdict

# is_permutation_of_plaindrome should return true if string can be permuted
# into a palindrome
# (A palindrome is a sentence/word that reads the same backwards and forwards)
# (for example, taco cat)
# Whitespace can be ignored.

def is_permutation_of_palindrome(string):
    # Your code here
    pass

tests = [
    [['tact coa'], True], 
    [['grad drag'], True],
    [['WAAARGARBLL'], False],
    [[''], True],
    [['r'], True],
    [['r '], True],
    [['rb'], False],
]

runtests(tests, is_permutation_of_palindrome)
Exemple #15
0
# is_loop should return True if there is a loop in the linked list.
# That is, if any node in the linked list points to a node earlier in the list (or itself).

def is_loop(ll):
    # Your code here
    pass

test1 = linkedlist_from_list([1])
test1.n = test1

test2 = linkedlist_from_list([1, 2, 3, 4, 5])
test2.n.n.n.n = test2

test3 = linkedlist_from_list([1, 2, 3, 4])
test3.n.n.n = test3

test4 = linkedlist_from_list([1, 2, 3, 4])
test4.n.n.n = test4.n.n

tests = [
    [[test1], True],
    [[test2], True],
    [[test3], True],
    [[test4], True],
    [[linkedlist_from_list([1, 2, 3, 3, 4, 5])], False],
    [[linkedlist_from_list([1])], False],
    [[linkedlist_from_list([])], False],
]

runtests(tests, is_loop)
Exemple #16
0
from linkedlist import linkedlist_from_list
from tester import runtests

# The partition function should take a linked list and a value.
# the linked list should be modified inplace such that all values
# less than the value end up in the first half of the list, and the
# values greater than or equal to the passed value end up in the
# second half of the list.

def partition(ll, val):
    # Your code here
    pass

tests = [
    [[linkedlist_from_list([1, 2, 3, 3, 4, 5]), 3], linkedlist_from_list([1, 2, 3, 3, 4, 5])],
    [[linkedlist_from_list([3, 3, 4, 5, 1, 2]), 3], linkedlist_from_list([1, 2, 3, 3, 4, 5])],
    [[linkedlist_from_list([6, 2, 33, 3, 44, 5]), 6], linkedlist_from_list([2, 3, 5, 6, 33, 44])],
    [[linkedlist_from_list([]), 3], linkedlist_from_list([])],
    [[linkedlist_from_list([6]), 3], linkedlist_from_list([6])],
    [[linkedlist_from_list([3, 2]), 3], linkedlist_from_list([2, 3])]
]

runtests(tests, partition, inplace=True)
from linkedlist import linkedlist_from_list
from tester import runtests

# kth_from_last should return the k-th-from-last element in the linked list ll.

def kth_from_last(k, ll):
    # Your code here
    pass

tests = [
    [[5, linkedlist_from_list([1, 2, 3, 3, 4, 5])], 1],
    [[0, linkedlist_from_list([5, 4, 3, 2, 5 ,1])], 1],
    [[1, linkedlist_from_list([1, 1])], 1],
    [[0, linkedlist_from_list([1])], 1],
    [[2, linkedlist_from_list(['always', 'always', 'remove', 'duplicates'])], 'always'],
    [[1, linkedlist_from_list([1, 2, 3, 1, 3, 0])], 3]
]

runtests(tests, kth_from_last)
# wrap-around. (see below for examples.)
# Implement is_rotation such that it uses the is_substring function exactly
# once to determine whether s1 is a rotation of s2.

def is_rotation(s1, s2):
    # This is gross. What is the correct way to do this?
    global used_once
    used_once = False

    def is_substring(s1, s2):
        global used_once
        if not used_once:
            used_once = True
            return s1 in s2
        raise Exception('Cheater cheater pumkin eater')

    # Your code here


tests = [
    [['is this a rotation?', 'rotation?is this a '], True],
    [['is this a rotation?', 'rotation?is this a  '], False],
    [['\'Twas then that the hurdy gurdy man came singing songs of love\'Twas then that the hurdy gurdy man came singing songs of love', '\'Twas then that the hurdy gurdy man came singing songs of love'], False],
    [['shakira', 'shakirashakira'], False],
    [['blargg', 'gblarg'], True],
    [['',''], True],
    [['Oh baby when you talk like that', ''], False]
]

runtests(tests, is_rotation)