def is_palindrome(s):



from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main(
        "is_string_palindromic_punctuation.tsv", is_palindrome)
Exemplo n.º 2
0

@enable_timer_hook
def find_smallest_subarray_covering_set_wrapper(timer, paragraph, keywords):
    copy = keywords

    timer.start()
    (start, end) = find_smallest_subarray_covering_set(paragraph, keywords)
    timer.stop()

    if start < 0 or start >= len(paragraph) or \
                    end < 0 or end >= len(paragraph) or \
                    start > end:
        raise TestFailureException("Index out of range")

    for i in range(start, end + 1):
        copy.discard(paragraph[i])

    if copy:
        raise TestFailureException("Not all keywords are in the range")

    return end - start + 1


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main(
        "smallest_subarray_covering_set.tsv",
        find_smallest_subarray_covering_set_wrapper)
Exemplo n.º 3
0
def plus_one(A):
    A[-1] += 1
    for i in reversed(range(1, len(A))):
        if A[i] != 10:
            break
        A[i] = 0
        A[i - 1] += 1

    if A[0] == 10:
        A[0] = 1
        A.append(0)

    return A


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main("int_as_array_increment.tsv",
                                              plus_one)
Exemplo n.º 4
0
from binary_tree_node import BinaryTreeNode


def is_binary_tree_bst(tree, low_range=float('-inf'), high_range=float('inf')):
    # Implement this placeholder.
    return True


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('is_tree_a_bst.tsv',
                                              is_binary_tree_bst)
Exemplo n.º 5
0
class Team:
    Player = collections.namedtuple('Player', ('height'))

    def __init__(self, height):
        self._players = [Team.Player(h) for h in height]

    # Checks if team0 can be placed in front of team1.
    @staticmethod
    def valid_placement_exists(team0, team1):
        # Implement this placeholder.
        return True


@enable_timer_hook
def valid_placement_exists_wrapper(timer, team0, team1, expected_01,
                                   expected10):
    t0, t1 = Team(team0), Team(team1)

    timer.start()
    if Team.valid_placement_exists(
            t0, t1) != expected_01 or Team.valid_placement_exists(
                t1, t0) != expected10:
        raise TestFailureException("")


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('is_array_dominated.tsv',
                                              valid_placement_exists_wrapper)
def rearrange_wrapper(timer, A):
    timer.start()
    rearrange(A)
    timer.stop()


def check_answer(A):
    for i in range(len(A)):
        if i % 2:
            if A[i] < A[i - 1]:
                raise TestFailureException('')
            if i + 1 < len(A):
                if A[i] < A[i + 1]:
                    raise TestFailureException('')
                else:
                    if i > 0:
                        if A[i - 1] < A[i]:
                            raise TestFailureException('')
            if i + 1 < len(A):
                if A[i + 1] < A[i]:
                    raise TestFailureException('')

    check_answer(A)


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('alternating_array.tsv',
                                              rearrange_wrapper)
Exemplo n.º 7
0
def apply_permutation(perm, A):
    # Implement this placeholder.
    return


def apply_permutation_wrapper(perm, A):
    apply_permutation(perm, A)
    return A


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main("apply_permutation.tsv",
                                              apply_permutation_wrapper)
Exemplo n.º 8
0
from list_node import ListNode
from test_framework.test_utils import enable_timer_hook


# Insert new_node after node.
def insert_after(node, new_node):
    # Implement this placeholder.
    return


@enable_timer_hook
def insert_list_wrapper(timer, l, node_idx, new_node_data):
    node = l
    for _ in range(node_idx - 1):
        node = node.next
    new_node = ListNode(new_node_data)

    timer.start()
    insert_after(node, new_node)
    timer.stop()

    return l


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('insert_in_list.tsv',
                                              insert_list_wrapper)
Exemplo n.º 9
0
from test_framework.test_failure_exception import TestFailureException


def decoding(s):
    # Implement this placeholder.
    return ''


def encoding(s):
    # Implement this placeholder.
    return ''


def rle_tester(encoded, decoded):
    if decoding(encoded) != decoded:
        raise TestFailureException('Decoding failed')
    if encoding(decoded) != encoded:
        raise TestFailureException('Encoding failed')


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('run_length_compression.tsv',
                                              rle_tester)
Exemplo n.º 10
0
def replace_and_remove(size, s):
    # Implement this placeholder.
    return 0


def replace_and_remove_wrapper(size, s):
    res_size = replace_and_remove(size, s)
    return s[:res_size]


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('replace_and_remove.tsv',
                                              replace_and_remove_wrapper)
Exemplo n.º 11
0
import collections

from test_framework.test_utils import enable_timer_hook

Item = collections.namedtuple('Item', ('weight', 'value'))


def optimum_subject_to_capacity(items, capacity):
    # Implement this placeholder.
    return 0


@enable_timer_hook
def optimum_subject_to_capacity_wrapper(timer, items, capacity):
    items = [Item(*i) for i in items]
    timer.start()
    return optimum_subject_to_capacity(items, capacity)


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main(
        "knapsack.tsv", optimum_subject_to_capacity_wrapper)
def longest_subarray_with_distinct_entries(A):
    # Implement this placeholder.
    return 0


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main(
        'longest_subarray_with_distinct_values.tsv',
        longest_subarray_with_distinct_entries)
Exemplo n.º 13
0
def merge_sorted_arrays(sorted_arrays):
    # Implement this placeholder.
    return []


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main("sorted_arrays_merge.tsv",
                                              merge_sorted_arrays)
Exemplo n.º 14
0
def generate_pascal_triangle(n):
    # Implement this placeholder.
    return []


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('pascal_triangle.tsv',
                                              generate_pascal_triangle)
Exemplo n.º 15
0
from binary_tree_node import BinaryTreeNode


def sum_root_to_leaf(tree, partial_path_sum=0):
    # Implement this placeholder.
    return 0


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('sum_root_to_leaf.tsv',
                                              sum_root_to_leaf)
Exemplo n.º 16
0
def minimum_total_waiting_time(service_times):
    # Implement this placeholder.
    return 0


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('minimum_waiting_time.tsv',
                                              minimum_total_waiting_time)
Exemplo n.º 17
0
def max_square_submatrix(A):
    # Implement this placeholder.
    return 0


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('max_square_submatrix.tsv',
                                              max_square_submatrix)
def longest_matching_parentheses(s):
    # Implement this placeholder.
    return 0


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main(
        'longest_substring_with_matching_parentheses.tsv',
        longest_matching_parentheses)
Exemplo n.º 19
0
def calculate_bonus(productivity):
    # Implement this placeholder.
    return 0


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('bonus.tsv', calculate_bonus)
Exemplo n.º 20
0
def convert_base(num_as_string, b1, b2):
    # Implement this placeholder.
    return ''


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main("convert_base.tsv", convert_base)
Exemplo n.º 21
0
def is_match(regex, s):
    # Implement this placeholder.
    return True


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('regular_expression.tsv',
                                              is_match)
Exemplo n.º 22
0

# Assumes node_to_delete is not tail.
def deletion_from_list(node_to_delete):
    # Implement this placeholder.
    return


@enable_timer_hook
def deletion_from_list_wrapper(timer, head, node_to_delete_idx):
    node_to_delete = head
    if node_to_delete is None:
        raise RuntimeError('List is empty')
    for _ in range(node_to_delete_idx):
        if node_to_delete.next is None:
            raise RuntimeError("Can't delete last node")
        node_to_delete = node_to_delete.next

    timer.start()
    deletion_from_list(node_to_delete)
    timer.stop()

    return head


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('delete_node_from_list.tsv',
                                              deletion_from_list_wrapper)
Exemplo n.º 23
0
def parity(x):
    # Implement this placeholder.
    return 0


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('parity.tsv', parity)
def expression_synthesis(digits, target):
    # Implement this placeholder.
    return True


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main("insert_operators_in_string.tsv",
                                              expression_synthesis)
Exemplo n.º 25
0
import collections

from test_framework.test_utils import enable_timer_hook

Point = collections.namedtuple("Point", ("x", "y"))


def find_line_with_most_points(points):
    # Implement this placeholder.
    return 0


@enable_timer_hook
def find_line_with_most_points_wrapper(timer, points):
    points = [Point(*x) for x in points]
    timer.start()
    return find_line_with_most_points(points)


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main(
        'line_though_most_points.tsv', find_line_with_most_points_wrapper)
Exemplo n.º 26
0
def has_path_sum(tree, remaining_weight):
    # Implement this placeholder.
    return True


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('path_sum.tsv', has_path_sum)
def shortest_equivalent_path(path):
    # Implement this placeholder.
    return ''


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main(
        'directory_path_normalization.tsv', shortest_equivalent_path)
# Given n, return all primes up to and including n.
import math


def generate_primes(n):
    arr = []
    i = 2
    while i <= n:
        if is_prime(i):
            arr.append(i)
        i += 1
    return arr


def is_prime(num):
    i = 2
    while i <= math.sqrt(num):
        if num % i == 0:
            return False
        i += 1
    return True


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main("prime_sieve.tsv",
                                              generate_primes)
Exemplo n.º 29
0
# Returns the number of valid entries after deletion.
def delete_duplicates(A):
    # Implement this placeholder.
    return 0


def delete_duplicates_wrapper(A):
    idx = delete_duplicates(A)
    return A[:idx]


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main('sorted_array_remove_dups.tsv',
                                              delete_duplicates_wrapper)
from binary_tree_node import BinaryTreeNode


def binary_tree_from_preorder_inorder(preorder, inorder):
    # Implement this placeholder.
    return None


from test_framework import test_utils_generic_main, test_utils

if __name__ == '__main__':
    test_utils_generic_main.generic_test_main(
        'tree_from_preorder_inorder.tsv', binary_tree_from_preorder_inorder)