TDT4109/Exercise 1/10 - Bakekurs/10-b.py

62 lines
1.9 KiB
Python
Raw Normal View History

2020-08-26 14:47:36 +02:00
COLUMN_PADDING = 10
class recipe:
def __init__(self, ingredients, standardPortions):
self.ingredients = ingredients
self.standardPortions = standardPortions
def toMap(self, portions):
ratio = portions / self.standardPortions
result = {'Antall cookies': portions}
for ingredient in self.ingredients:
result[ingredient] = self.ingredients[ingredient]*ratio
return result
cookies = recipe(
ingredients={
'sukker(g)': 400,
'smør(g)': 320,
'sjokolade(g)': 500,
'egg': 2,
'hvetemel(g)': 460
},
standardPortions=48,
)
def columnLength(columnElements):
elementLengths = list(map(lambda element: len(str(element)), columnElements))
return max(elementLengths) + COLUMN_PADDING
# lshift colum by column
# concatenate columns into rows
# foreach row print(row)
cookieQuestionList = [
'Hvor mange cookies vil du lage? ',
'Hvor mange cookies vil du lage nå? ',
'Hvor mange cookies vil du lage til slutt? '
]
cookieNumbers = list(map(lambda question: int(input(question)), cookieQuestionList))
# Make recipe maps from every cookie number
cookieList = list(map(lambda cNum: cookies.toMap(cNum), cookieNumbers))
# Define columns
columns = ['Antall cookies', 'sukker(g)', 'sjokolade(g)']
rawColumnData = {}
for column in columns:
rawColumnData[column] = [column]
rawColumnData[column].extend(list(map(lambda cookie: cookie[column], cookieList)))
columnLengths = dict(map(lambda column: (column[0], columnLength(column[1])), rawColumnData.items()))
formattedColumnData = {}
for column in columns:
formattedColumnData[column] = []
for entry in list(rawColumnData[column]):
remainingSpaces = columnLengths[column]
formattedColumnData[column].append(str(entry).ljust(remainingSpaces))
for row in range(0, len(columns) + 1):
print(formattedColumnData['Antall cookies'][row] + formattedColumnData['sukker(g)'][row])