Order Now  +1 678 648 4277 

Implementation of Functions Assignment Solution


Please complete implementations of the functions described in problem 1, and the methods described in problem 2.

Each function and method must be explicitly tested, once or more, to demonstrate correct operation.

Problem 1

def map_pairs(f, g, pairs):

    # parameters:

    # - f is a function

    # - g is a function

    # - pairs is a list of pairs (2-tuples)

    # returns:

    # - the list of pairs (f(a),g(b)) for each pair (a,b)

    return [(f(a), g(b)) for a,b in pairs]

def reject(test, items):

    # parameters:

    # - test, a function returning True or False

    # - items, a list

    # returns:

    # - those items in the list, in the same order, that fail the test

    return list(filter(lambda num: not test(num), items))

def majority(test, items):

    # parameters:

    # - test, a function returning True or False

    # - items, a list

    # returns:

    # - True if the majority of values in the list pass the test, False otherwise

    return len(list(filter(test, items))) > (len(items) // 2)

def count_if(test, items):

    # parameters:

    # - test, a function returning True or False

    # - items, a list

    # returns:

    # - the number of items that pass the test

    return len(list(filter(test, items)))

def map_values(f, d):

    # parameters:

    # - f, a function

    # - d, a dictionary

    # returns:

    # - a new dictionary with, for each association k : v in the original dictionary, a new association k : f(v)

Problem 2

# Define a class Appt to represent appointments in an appointment book.

#

# The attributes of an Appt are the following items:

#

# - date, a string in 'YYYYMMDD’' format,

# - start_time, a string in 'HHMM' format (a 24-hour time, with 00 for midnight, 13 for 1pm, etc.),

# - duration, an integer number of minutes, and

# - descrip, a string describing the appointment

#

# You may make the following simplifying assumptions:

#

# Each appointment is assumed to take place entirely within one day; that is, appointments do not span multiple days.

#

# Each appointments duration is such that the rule above is not broken. That is, you can't have a million-minute appointment that starts on Sept 17 2021 at noon; simply assume appointments like that are never created in the first place.

#

# Write the following methods for the class Appt:

#

# __init__(date, start_time, duration, descrip), a constructor

# - you need not check in __init__ that values given are reasonable and / or in the correct format(s)

#

# __repr__()

# - construct a string representing the current appointment in some easily readable way

#

# starts_at_same_time(other)

# - returns True if the other appointment starts on the same date and at the same time as the current object, False otherwise

#

# starts_before(other)

# - returns True if the current appointment starts before the other appointment, taking both date and time into account

#

# overlaps(other)

# - returns True if the two appointments overlap, that is if they share at least one minute in common. If two appointments merely "touch" -- that is, one appointment starts on Sept 17 2021 at noon and lasts 60 minutes, and the other starts on Sept 17 2021 at 1 pm, they do not overlap, as far as this method is concerned.

Solution:

Problem 1.

return dict(d.keys(), map(f, d.values()))

Problem 2:

from datetime import datetime, timedelta def get_datetime(date, time): year, month, day = int(date[0:4]), int(date[4:6]), int(date[6:8]) hour, minute = int(time[0:2]), int(time[2:4]) return datetime(year, month, day, hour, minute) class Appt: def __init__(self, date, start_time, duration, descrip): self.date = date self.start_time = start_time self.duration = int(duration) self.description = descrip def __repr__(self): date_obj = get_datetime(self.date, self.start_time) date_string = 'Start Time: ' + date_obj.strftime("%A, %d %B %Y %I:%M%p") duration_string = 'Duration: ' if self.duration // 60 != 0: duration_string += str(self.duration // 60) + ' hour(s), ' duration_string += str(self.duration % 60) + ' minutes' description_string = 'Description: ' + self.description return '\n'.join((date_string, duration_string, description_string)) def starts_at_same_time(self, other): return self.date == other.date and self.start_time == other.start_time def starts_before(self, other): curr_time = get_datetime(self.date, self.start_time) other_time = get_datetime(other.date, other.start_time) return curr_time < other_time def overlaps(self, other): curr_start = get_datetime(self.date, self.start_time) other_start = get_datetime(other.date, other.start_time) curr_end = curr_start + timedelta(minutes=self.duration) other_end = other_start + timedelta(minutes=other.duration) if curr_start > other_start and curr_start < other_end: return True if other_start > curr_start and other_start < curr_end: return True return False