# Unique Ice Cream Flavors
#
# Let's say we own an ice cream shop and throughout the day, we add the flavor
# of every purchase to a list. At the end of the day, we want to remove all
# duplicates so we know what unique group of flavors were chosen throughout the
# day. How might we achieve this?
# input
purchased_flavors = [
"chocolate",
"vanilla",
"strawberry",
"chocolate",
"peanut butter",
"vanilla",
"rocky road",
"peanut butter"
]
# Ice Cream Flavor Counts
# Now let's say we are building out our program. We now want to know how many
# times each flavor is purchased. Below, we have a partial implementation of
# this class.
# How would you implement this functionality?
class Purchases(object):
def __init__(self, purchases):
self.purchases = purchases
def count_of(self, flavor):
"""Count of a given flavor.
Returns the number of times a given flavor was purchased.
"""
pass
# We might use this class like so:
# purchased_flavors is the list from the first question.
purchases = Purchases(purchased_flavors)
purchases.count_of('chocolate')
# => 2
purchases.count_of('coconut')
# => 0
# Ice Cream Factory Statistics
# We now make our own ice cream and we have the below ice cream factory class to
# manage this. This program is responsible for executing each step in the ice
# cream making process. We've been having some issues so we want to log some
# info about the system (using the `factory_logger.log()` call) at the start
# and end of each step.
# Our intial implementation is below, how might we optimize/refactor this code?
# implements factory_logger.log()
import factory_logger
class IceCreamFactory(object):
def step1(self):
factory_logger.log("start step1 {}".format(datetime.datetime.now()))
# do step 1
factory_logger.log("end step1 {}".format(datetime.datetime.now()))
def step2(self):
factory_logger.log("start step2 {}".format(datetime.datetime.now()))
# do step 2
factory_logger.log("end step2 {}".format(datetime.datetime.now()))
def step3(self):
factory_logger.log("start step3 {}".format(datetime.datetime.now()))
# do step 3
factory_logger.log("end step3 {}".format(datetime.datetime.now()))
When we were a small company, our factory just created single flavors like chocolate and vanilla and the process was the same for each flavor. Now we're starting to create very complex flavors. We've had to expand some of our IceCreamFactory
steps to include flavor-specific logic.
# Creating New Flavors
# When we were a small company, our factory just created single flavors like
# chocolate and vanilla and the process was the same for each flavor. Now we're
# starting to create very complex flavors. We've had to expand some of our
# `IceCreamFactory` steps to include flavor-specific logic.
# How might we make this code more extensible and easier to reason about as we
# continue to add flavors with custom logic?
class IceCreamFactory(object):
def __init__(self, flavor):
self.flavor = flavor
def step1(self):
if flavor == 'rocky road':
# do some things
elif flavor == 'chocolate attack':
# do something different
else:
# do our standard step for
# all other flavors
def step2(self):
pass
def step3(self):
if flavor == 'rocky road':
# do something
elif flavor == 'banana split':
# do something different
else:
# do our standard step for
# all other flavors
def step4(self):
pass