Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

# Complete the two 2nd order functions. # elapsedTime(f): for a one-variable fun

ID: 3603617 • Letter: #

Question

# Complete the two 2nd order functions.
# elapsedTime(f): for a one-variable function f(x),
# elapsedTime(f) returns the function to output:
# - f(x), and
# - deltaT, the elapsed time in computing f(x)
# * deltaT must be the time object from the dateTime module
#
# vectorize(f): for a one-variable function f(x),
# vectorize(f) returns the function to accept a list
# l = [x1, x2, ..., xm]
# and output
# [f(x1), f(x2), ..., f(xm)]
#
# Examine the test codes below carefully.
#

from datetime import datetime


def elapsedTime(f):
#-- code ---
  

def vectorize(f):
#-- code --

  

#testcodes with decorators
@elapsedTime
def count(n):
out=0
for i in range(n):
out += 1
return out


out, deltaT= count(50000)
print("count(50000) returns:", out)
print("elapsed time (sec): ", deltaT.total_seconds())
print("type of deltaT: ", type(deltaT))
print("")
#This should print:
#count(50000) returns: 50000
#elapsed time (sec): 0.015502 - the value depends on your computer
#type of deltaT: <class 'datetime.timedelta'>
#

@vectorize
def square(x):
return x**2
length = vectorize(len)
print(square([1,2,3,4,5]))
print(length(["Ken", "Linda", "Joshua", [2, 3], [3, 6, 4]]))
#This should print:
#[1, 4, 9, 16, 25]
#[3, 5, 6, 2, 3]

Explanation / Answer

#   Complete the two 2nd order functions.
#       elapsedTime(f): for a one-variable function f(x),
#           elapsedTime(f) returns the function to output:
#               - f(x), and
#               - deltaT, the elapsed time in computing f(x)
#                   * deltaT must be the time object from the dateTime module
#
#       vectorize(f): for a one-variable function f(x),
#           vectorize(f) returns the function to accept a list
#               l = [x1, x2, ..., xm]
#           and output
#               [f(x1), f(x2), ..., f(xm)]
#
#   Examine the test codes below carefully.
#
from datetime import datetime

def elapsedTime(f):
    def timeCalc(n):
        now = datetime.now()
        out = f(n)
        then = datetime.now()
        timeDelta = then - now
        return (out, timeDelta)

    return timeCalc

def vectorize(f):
    def vec(valList):
        result = []
        for val in valList:
            result.append(f(val))
        return result
    return vec
  

#testcodes with decorators
@elapsedTime
def count(n):
    out=0
    for i in range(n):
        out += 1
    return out

out, deltaT= count(50000)
print("count(50000) returns:", out)
print("elapsed time (sec): ", deltaT.total_seconds())
print("type of deltaT: ", type(deltaT))
print("")
#This should print:
#count(50000) returns: 50000
#elapsed time (sec): 0.015502   -   the value depends on your computer
#type of deltaT: <class 'datetime.timedelta'>
#
@vectorize
def square(x):
    return x**2
length = vectorize(len)
print(square([1,2,3,4,5]))
print(length(["Ken", "Linda", "Joshua", [2, 3], [3, 6, 4]]))
#This should print:
#[1, 4, 9, 16, 25]
#[3, 5, 6, 2, 3]

# copy pastable code link: https://paste.ee/p/dZciW