+1 (315) 557-6473 

Program To Create Flavor Organizer in Python Language Assignment Solution.


Instructions

Objective
Write a python assignment program to create flavor organizer.

Requirements and Specifications

Program to create flavor organizer in python
Program to create flavor organizer in python 1
Program to create flavor organizer in python 2
Program to create flavor organizer in python 3

Screenshots

Program to create flavor organizer in python 4
Program to create flavor organizer in python 5

Source Code

# Spring 2021 - Assignment 3

# Please include your name and the names of the students with whom you discussed any of the problems in this homework

debugging = False

def debug(*s):

     if debugging:

          print(*s)

## problem 1-a) organizebyFlavor - 15%

def organizebyFlavor(feedingLog):

     # Create the new dict

     new_feedingLog = dict()

     for key in feedingLog:

          for flavorName in feedingLog[key]:

               if not flavorName in new_feedingLog:

                    new_feedingLog[flavorName] = dict()

               new_feedingLog[flavorName][key] = feedingLog[key][flavorName]

     return new_feedingLog

# Test

myCatsLog = {(7,2020):{"Oceanfish":7, "Tuna":1, "Whitefish":3, "Chicken":4, "Beef":2},

              (8,2020):{"Oceanfish":6, "Tuna":2, "Whitefish":1, "Salmon":3, "Chicken":6},

              (9,2020):{"Tuna":3, "Whitefish":3, "Salmon":2, "Chicken":5, "Beef":2, "Turkey":1, "Sardines":1},

              (10,2020):{"Whitefish":5, "Sardines":3, "Chicken":7, "Beef":3},

              (11,2020):{"Oceanfish":3, "Tuna":2, "Whitefish":2, "Salmon":2, "Chicken":4, "Beef":2, "Turkey":1},

              (12,2020):{"Tuna":2, "Whitefish":2, "Salmon":2, "Chicken":4, "Beef":2, "Turkey":4, "Sardines":1},

              (1,2021):{"Chicken":7,"Beef":3, "Turkey":4, "Whitefish":1, "Sardines":2} }

debug("Test for Question 1-a)")

debug(organizebyFlavor(myCatsLog))

from functools import reduce

## problem 1-b) popularFlavor - 15%

def popularFlavor(feedingLog):

     newLog = organizebyFlavor(feedingLog)

     # The following line will return tuples with the name of the flavor and the total number of cans

     tuples_sum = map(lambda x: (x[0], sum(x[1].values())), newLog.items())

     # Now, sort the list of tuples by descending order and pick first

     tuples_sum_sorted = sorted(tuples_sum, key = lambda tup: tup[1], reverse = True)

     return tuples_sum_sorted[0]

# Test

debug("\nTest for Question 1-b)")

debug(popularFlavor(myCatsLog))

## problem 2) unzip - 15%

def unzip(L):

     return tuple(map(list, zip(*L)))

# Test

debug("\nTest for Question 2)")

debug(unzip([(1,"a",10),(2,"b",11),(3,"c",12),(4,"d",13)]))

## problem 3 - findCycle - 15%

# Helper function

def hasCycle(graph, start):

     g = graph.copy()

     if not start in g:

          return True

     current = g.pop(start)

     if current is None:

          return False

     return True*hasCycle(g, current)

def findCycle(graph, start):

     if not hasCycle(graph, start):

          return None

     if not start in graph:

          output = None

     elif len(graph) == 1:

          output = list(graph.values())[0]

     else:

          startEnd = graph.pop(start)

          nextElement = findCycle(graph, startEnd)

          if nextElement != None:

               output = ''.join(startEnd) + ''.join(nextElement)

          else:

               output = ''.join(startEnd)

          #return startEnd + findCycle(graph, startEnd)

     if isinstance(output, str):

          return list(output)

     else:

          return None

# Test

debug("\nTest for Question 3)")

graph = {'A':'B','B':'D','C':'G','D':'E','E':'C', 'F':'I','G':'B','H':None,'I':'H'}

debug(findCycle(graph,'A'))

debug(findCycle(graph,'F'))

## problem 4 - getNextNode - 10%

def getNextNode(graph, start):

     class MyIter:

          def __init__(self, graph, start):

               self.graph = graph

               self.current = start

          def __iter__(self):

               return self

          def __next__(self):

               current = self.current

               if self.current != None:

                    self.current = self.graph[self.current]

                    return current

               else:

                    raise StopIteration

               #if self.current != None:

               # self.current = self.graph[self.current]

               #return current

     return MyIter(graph, start)

# Test

debug("\nTest for Question 4)")

graph = {'A':'B','B':'D','C':'G','D':'E','E':'C', 'F':'I','G':'B','H':None,'I':'H'}

graph_nodes = getNextNode(graph, 'A')

debug(graph_nodes.__next__())

debug(graph_nodes.__next__())

L = []

for i in range(0,15):

     L.append(graph_nodes.__next__())

debug(L)

## problem 5 - DecodeIter - 25%

class DecodeIter:

     def __init__(self, ttable, strInput):

          self.ttable = ttable

          self.input = strInput

          if isinstance(strInput, str):

               self.iterator = iter(strInput)

          else:

               self.iterator = strInput

     def __iter__(self):

          return self

     def __next__(self):

          current = self.iterator.__next__()

          word = ""

          # Get word

          while current != ' ':

               if current in self.ttable:

                    word += self.ttable[current]

               else:

                    word += current

               current = next(self.iterator, ' ')

          return word

# Test

debug("\nTest for Question 5)")

ttable = {'C': 'A', 'D': 'B', 'E': 'C', 'F': 'D', 'G': 'E', 'H': 'F', 'I': 'G', 'J': 'H', 'K': 'I', 'L': 'J', 'M': 'K',

           'N': 'L', 'O': 'M', 'P': 'N', 'Q': 'O', 'R': 'P', 'S': 'Q', 'T': 'R', 'U': 'S', 'V': 'T', 'W': 'U', 'X': 'V',

           'Y': 'W', 'Z': 'X', '[': 'Y', '\\': 'Z', 'c': 'a', 'd': 'b', 'e': 'c', 'f': 'd', 'g': 'e', 'h': 'f',

           'i': 'g', 'j': 'h', 'k': 'i', 'l': 'j', 'm': 'k', 'n': 'l', 'o': 'm', 'p': 'n', 'q': 'o', 'r': 'p', 's': 'q',

           't': 'r', 'u': 's', 'v': 't', 'w': 'u', 'x': 'v', 'y': 'w', 'z': 'x', '{': 'y', '|': 'z', '2': '0', '3': '1',

           '4': '2', '5': '3', '6': '4', '7': '5', '8': '6', '9': '7', ':': '8', ';': '9'}

strInput = "R{vjqp ku cp gcu{ vq ngctp, rqygthwn rtqitcookpi ncpiwcig. Kv jcu ghhkekgpv jkij-ngxgn fcvc uvtwevwtgu cpf c ukorng dwv ghhgevkxg crrtqcej vq qdlgev qtkgpvgf-rtqitcookpi. R{vjqp'u gngicpv u{pvcz cpf f{pcoke v{rkpi, vqigvjgt ykvj kvu kpvgtrtgvgf pcvwtg, ocmg kv cp kfgcn ncpiwcig hqt uetkrvkpi cpf tcrkf crrnkecvkqp fgxgnqrogpv kp ocp{ ctgcu qp oquv rncvhqtou."

words = DecodeIter(ttable, strInput)

debug(words.__next__())

debug(words.__next__())

debug(words.__next__())

debug(words.__next__())

L = []

for w in words:

     L.append(w)

debug(L)