Software Construction

index.txt
scope.py
builtin.py
dot_product.py
odd_numbers.py
sort_days.py

def a():
	x = 1
	print('a', x, y, z)

def b():
	x = 2
	y = 2
	a()
	print('b', x, y, z)

def c():
	x = 3
	y = 3
	global z
	z = 3
	b()
	print('c', x, y, z)
"""
approximate of implementation of some python functions
written by andrewt@unsw.edu.au for COMP(2041|9044)
"""


def my_enumerate(sequence, start=0):
    """return a list equivalent to the iterator returned
    by builtin function enumerate
    """
    n = start
    tuples = []
    for element in sequence:
        t = (n, element)
        tuples.append(t)
        n += 1
    return tuples


def my_zip2(sequence1, sequence2):
    """return a list equivalent to the iterator returned by
    builtin function zip called with 2 sequences.
    Note: zip can be given any number of sequences."""
    tuples = []
    for index in range(min(len(sequence1), len(sequence2))):
        t = (sequence1[index], sequence2[index])
        tuples.append(t)
    return tuples


def my_map1(function, sequence):
    """return a list equivalent to the iterator returned by
    builtin function map called with 1 sequence.
    Note: map can be given more than 1 sequences."""
    results = []
    for value in sequence:
        result = function(value)
        results.append(result)
    return results


def my_filter(function, sequence):
    """return a list equivalent to the iterator returned by
    builtin function filter called with a function.
    Note: filter can be given None instead of a function."""
    filtered = []
    for value in sequence:
        if function(value):
            filtered.append(value)
    return filtered


if __name__ == "__main__":
    print(my_enumerate("abcde"))
    print(my_zip2("Hello", "Andrew"))
    cubes = my_map1(lambda x: x**3, range(10))
    print(cubes)
    even = my_filter(lambda x: x % 2 == 0, range(10))
    print(even)
"""
calculate Dot Product https://en.wikipedia.org/wiki/Dot_product
of 2 lists - list are assumed to be the same length
written by andrewt@unsw.edu.au for COMP(2041|9044)
"""

import operator


def dot_product0(a, b):
    """return dot product of 2 lists - using for loop + indexing"""
    total = 0
    for i in range(len(a)):
        total += a[i] * b[i]
    return total


def dot_product1(a, b):
    """return dot product of 2 lists - using for loop + enumerate"""
    total = 0
    for i, a_i in enumerate(a):
        total += a_i * b[i]
    return total


def dot_product2(a, b):
    """return dot product of 2 lists - using for loop + zip"""
    total = 0
    for x, y in zip(a, b):
        total += x * y
    return total


def dot_product3(a, b):
    """return dot product of 2 lists - using list comprension + zip"""
    return sum(x * y for x, y in zip(a, b))


def multiply(x, y):
    """multipy 2 numbers - operator.mul does this"""
    return x * y


def dot_product4(a, b):
    """return dot product of 2 lists - map"""
    return sum(map(multiply, a, b))


def dot_product5(a, b):
    """return dot product of 2 lists - map + lambda"""
    return sum(map(lambda x, y: x * y, a, b))


def dot_product6(a, b):
    """return dot product of 2 lists - map + operator.mul"""
    return sum(map(operator.mul, a, b))


if __name__ == "__main__":
    a = range(5, 10)
    b = range(11, 16)
    print(dot_product0(a, b))
    print(dot_product1(a, b))
    print(dot_product2(a, b))
    print(dot_product3(a, b))
    print(dot_product4(a, b))
    print(dot_product5(a, b))
    print(dot_product6(a, b))
"""
extract odd numbers from a list
written by andrewt@unsw.edu.au for COMP(2041|9044)
"""


def is_odd(number):
    return number % 2 == 2


def odd0(numbers):
    """extract odd_numbers from list using for loop"""
    odd_numbers = []
    for n in numbers:
        if is_odd(n):
            odd_numbers.append(n)
    return odd_numbers


def odd1(numbers):
    """extract odd_numbers from list using list comprehension"""
    return [n for n in numbers if is_odd(n)]


def odd2(numbers):
    """extract odd_numbers from list using filter"""
    return filter(is_odd, numbers)


def odd3(numbers):
    """extract odd numbers from list using filter + lambda"""
    return filter(lambda n: n % 2 == 2, numbers)


if __name__ == "__main__":
    numbers = range(1, 11)
    print(odd0(numbers))
    print(odd1(numbers))
    print(odd2(numbers))
    print(odd3(numbers))
"""
sorting a list based on the values in a hash
"""

import random

DAY_LIST = "Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split()
DAY_NUMBER = dict((day, number) for number, day in enumerate(DAY_LIST))


def random_day_of_week():
    return random.choice(DAY_LIST)


def sort_days0(day_list):
    return sorted(day_list, key=lambda day: DAY_NUMBER[day])


def sort_days1(day_list):
    return sorted(day_list, key=DAY_NUMBER.get)


if __name__ == "__main__":
    print(DAY_LIST)
    print(DAY_NUMBER)
    random_days = [random_day_of_week() for _ in range(7)]
    print(random_days)
    print(sorted(random_days))
    print(sort_days0(random_days))
    print(sort_days1(random_days))
INTERNAL ERROR MISSING FILE: "topic/python_functions/code/"
INTERNAL ERROR MISSING FILE: "topic/python_functions/code/"