Compares the two trees defined by Nodes a and b and returns true if they are equal in structure and in value
def compare(a, b):
if a == b:
return True
if a is None or b is None:
return False
if a.val != b.val:
return False
return compare(a.left, b.left) and compare(a.right, b.right
Tests
- test_should_return_false_for_non_equal_nodes
- test_should_return_true_for_equal_nodes
TO DO
- test_should_handle_depth_of_one
- test_should_handle_depth_of_two
- test_should_handle_single_child_trees
import unittest
class Test(unittest.TestCase):
def test_should_return_true_for_equal_nodes(self):
self.assertTrue(compare(a_node, b_node))
def test_should_return_false_for_non_equal_nodes(self):
self.assertFalse(compare(a_node, c_node))
class Node:
def __init__(self, val, left, right):
self.val = val
self.left = left
self.right = right
a_node = Node(1, None, None)
b_node = Node(1, None, None)
c_node = Node(2, None, None)
We need the ability to divide an unknown integer into a given number of even parts — or at least as even as they can be
def splitInteger(num,parts):
quotient, remainder = divmod(num, parts)
lower_elements = [quotient] * (parts - remainder)
higher_elements = [quotient + 1] * remainder
return lower_elements + higher_elements
Tests
- test_should_handle_evenly_distributed_cases
TO DO
- test_random_cases
- test_should_handle_big_inputs
- test_should_handle_evenly_distributed_cases
- test_should_handle_uneven_cases
import unittest
class Test(unittest.TestCase):
def test_should_handle_evenly_distributed_cases(self):
self.assertEqual(splitInteger(10, 1), [10])
self.assertEqual(splitInteger(2, 2), [1,1])
self.assertEqual(splitInteger(20, 5), [4,4,4,4,4])
def minimum_swaps(ratings):
if sorted(ratings, reverse=True) == ratings:
return 0
swaps = 1
while sorted(ratings, reverse=True) != sorter(ratings):
swaps += 1
return swaps
def sorter(array):
for i in sorted(range(len(array)), reverse=True):
comp = array[:i]
if len(comp) > 0:
mini = min(comp)
if mini < array[i]:
index = array.index(mini)
array[i], array[index] = array[index], array[i]
return array
return array
Tests
- test_minimum_swaps_should_handle_multiple_swaps
- test_minimum_swaps_should_handle_single_swap
TO DO
- test_minimum_swaps_should_handle_multiple_swaps_on_large_arrays
- test_minimum_swaps_should_handle_zero_swap
import unittest
class Test(unittest.TestCase):
def test_minimum_swaps_should_handle_single_swap(self):
self.assertEqual(minimum_swaps([3,1,2]), 1)
def test_minimum_swaps_should_handle_multiple_swaps(self):
self.assertEqual(minimum_swaps([3,1,2,4]), 2)
class ShoppingCart(object):
def __init__(self):
self.total = 0
self.items = {}
def add_item(self, item_name, quantity, price):
self.total += quantity * price
if type(item_name) == str and quantity > 0:
self.items.update({item_name: quantity})
def remove_item(self, item_name, quantity, price):
if quantity >= self.items[item_name] and quantity >= 1:
items_cost = price * self.items[item_name]
self.total -= items_cost
del self.items[item_name]
else:
self.total -= quantity * price
self.items[item_name] -= quantity
def checkout(self, cash_paid):
balance = 0
if cash_paid < self.total:
return "Cash paid not enough"
balance = cash_paid - self.total
return balance
class Shop(ShoppingCart):
def __init__(self):
self.quantity = 100
def remove_item(self):
self.quantity -= 1
Tests
- test_add_item
- test_add_item_hidden
- test_cart_property_initialization
- test_checkout_returns_correct_value
- test_remove_item
- test_remove_item_hidden
- test_shop_initializaton
- test_shop_is_instance_of_shopping_cart
- test_shop_remove_item_method
import unittest;
class Test(unittest.TestCase):
def setUp(self):
self.cart = ShoppingCart()
self.shop = Shop()
def test_cart_property_initialization(self):
self.assertEqual(self.cart.total, 0, msg='Initial value of total not correct')
self.assertIsInstance(self.cart.items, dict, msg='Items is not a dictionary')
def test_add_item(self):
self.cart.add_item('Mango', 3, 10)
self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items')
self.assertEqual(self.cart.items['Mango'], 3, msg='Quantity of items not correct after adding item')
def test_add_item_hidden(self):
self.cart.add_item('Mango', 3, 10)
self.cart.add_item('Orange', 16, 10)
self.assertEqual(self.cart.total, 190, msg='Cart total not correct after adding items')
self.assertEqual(self.cart.items['Orange'], 16, msg='Quantity of items not correct after adding item')
def test_remove_item(self):
self.cart.add_item('Mango', 3, 10)
self.cart.remove_item('Mango', 2, 10)
self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item')
self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')
def test_remove_item_hidden(self):
self.cart.add_item('Mango', 3, 10)
self.cart.add_item('Orange', 16, 10)
self.cart.remove_item('Mango', 2, 10)
self.assertEqual(self.cart.total, 170, msg='Cart total not correct after removing item')
self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')
self.cart.remove_item('Mango', 2, 10)
self.assertEqual(self.cart.total, 160, msg='Cart total not correct after removing item')
with self.assertRaises(KeyError):
self.cart.items['Mango']
def test_checkout_returns_correct_value(self):
self.cart.add_item('Mango', 3, 10)
self.cart.add_item('Orange', 16, 10)
self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct')
self.assertEqual(self.cart.checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct')
def test_shop_is_instance_of_shopping_cart(self):
self.assertTrue(isinstance(self.shop, ShoppingCart), msg='Shop is not a subclass of ShoppingCart')
def test_shop_initializaton(self):
self.assertEqual(self.shop.quantity, 100, msg='Shop quantity not initialized correctly')
def test_shop_remove_item_method(self):
for i in range(15):
self.shop.remove_item()
self.assertEqual(self.shop.quantity, 85)