Skip to content

Instantly share code, notes, and snippets.

@ecthiender
Created September 10, 2015 22:11
Show Gist options
  • Save ecthiender/7fd18683673885371047 to your computer and use it in GitHub Desktop.
Save ecthiender/7fd18683673885371047 to your computer and use it in GitHub Desktop.
Some limey functions!
# -*- coding: utf8 -*-
# Some limey functions!
from functools import reduce
import itertools
import unittest
def omit(x, li):
"""
omit(integer, list) -> list
The function takes in a list and an integer, and returns a sub-set list
ommiting that integer. If the integer does not exist in the list, then it
returns the original list.
Example,
omit(2, [1,2,3,5]) -> [1,3,5]
omit(4, [1,2,3,5]) -> [1,2,3,5]
"""
# given the current element x, get the rest of the list except x:
# basically take advantage of list slicing, and slice up the first part
# upto x element, and the second part is the slice after x, then
# concatenate the lists together
if x in li:
return li[:li.index(x)] + li[li.index(x)+1:]
# if x is not in list, return the original list
return li
def citrus_product(nums):
"""
citrus_product(list) -> list
The function takes in a list and returns another list, such that the i-th
element of the output list is product of all elements of the input list
except the i-th element of the input list.
Example, citrus_product([1,2,3,4]) -> [24,12,8,6]
"""
# calculate product of all other elements except the current one..
def calculate_product(x):
# given the current element x, get the rest of the list except x
rest_of_the_list = omit(x, nums)
# reduce the list to product of all elements
return reduce(lambda x, y: x * y, rest_of_the_list)
return map(calculate_product, nums)
def citrus_sum(n, numbers):
"""
citrus_sum(int, list) -> bool
The function takes in an integer and a list, and checks if sum of any pair
in the list is equal to the integer argument. Returns True if check succeeds
else False.
Example,
citrus_sum(12, [1,2,3,4,5,6,7,8,9]) -> True (because 5 + 7 == 12)
citrus_sum(20, [1,2,3,4,5,6]) -> False
"""
# function which takes an item and checks if the pair of this item and any
# item in the list sums up to n; if yes the return True else False
def check_nums(x):
# get the rest of the list except the current element
rest_of_the_list = omit(x, numbers)
# see if any other element in the list can pair up with the current
# element to sum up to n
for y in rest_of_the_list:
if x + y == n:
return True
return False
# the list of matched numbers which can pair up with some number in the list
# to have sum equal to n
matched_nums = filter(check_nums, numbers)
# alternate version:
# itertools.product to get all possible pairs from the list,
# then filter elements where the sum of a pair is equal to n
# matched_nums = filter(lambda x: sum(x) == n, itertools.product(numbers,
# numbers))
# if list is empty, no pairs to sum up to n found
if not matched_nums:
return False
return True
class CitrusTest(unittest.TestCase):
"""
Test out the Citrus and helper functions. The testcases below should be
self-explanatory.
"""
def test_omit(self):
input = (2, [1, 2, 3, 5])
expected_output = [1, 3, 5]
output = omit(*input)
self.assertEqual(output, expected_output)
def test_omit_absent(self):
input = (4, [1, 2, 3, 5])
expected_output = [1, 2, 3, 5]
output = omit(*input)
self.assertEqual(output, expected_output)
def test_citrus_product(self):
input = [1, 2, 3, 4]
expected_output = [24, 12, 8, 6]
output = citrus_product(input)
self.assertEqual(output, expected_output)
def test_citrus_sum_true(self):
input = (12, [1, 2, 5, 6, 7])
expected_output = True
output = citrus_sum(*input)
self.assertEqual(output, expected_output)
def test_citrus_sum_false(self):
input = (20, [1, 2, 5, 6, 7])
expected_output = False
output = citrus_sum(*input)
self.assertEqual(output, expected_output)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment