refactor
This commit is contained in:
parent
60c6d18d58
commit
ee06c0989a
|
@ -1,3 +1,6 @@
|
|||
from typing import List, Dict, Union
|
||||
from common import inputTypeCheck
|
||||
|
||||
COLUMN_PADDING = 10
|
||||
|
||||
class recipe:
|
||||
|
@ -24,39 +27,53 @@ cookies = recipe(
|
|||
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 = [
|
||||
columnsToPrint = ['Antall cookies', 'sukker(g)', 'sjokolade(g)']
|
||||
questionList = [
|
||||
'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)']
|
||||
ask = lambda question: inputTypeCheck(
|
||||
message=question,
|
||||
type=float,
|
||||
errorMessage='Beklager, det du skrev inn er ikke et tall. Prøv igjen\n'
|
||||
)
|
||||
|
||||
rawColumnData = {}
|
||||
for column in columns:
|
||||
rawColumnData[column] = [column]
|
||||
rawColumnData[column].extend(list(map(lambda cookie: cookie[column], cookieList)))
|
||||
cookieAmounts = list(map(ask, questionList))
|
||||
cookieObjects = list(map(lambda cNum: cookies.toMap(cNum), cookieAmounts))
|
||||
|
||||
columnLengths = dict(map(lambda column: (column[0], columnLength(column[1])), rawColumnData.items()))
|
||||
def createColumns(columnsToPrint) -> Dict[str, List[Union[str, int]]]:
|
||||
rawColumnData = {}
|
||||
for column in columnsToPrint:
|
||||
rawColumnData[column] = [column]
|
||||
recipeRows = list(map(lambda cookie: cookie[column], cookieObjects))
|
||||
rawColumnData[column].extend(recipeRows)
|
||||
return rawColumnData
|
||||
|
||||
formattedColumnData = {}
|
||||
for column in columns:
|
||||
formattedColumnData[column] = []
|
||||
for entry in list(rawColumnData[column]):
|
||||
remainingSpaces = columnLengths[column]
|
||||
formattedColumnData[column].append(str(entry).ljust(remainingSpaces))
|
||||
rawColumns = createColumns(columnsToPrint)
|
||||
|
||||
for row in range(0, len(columns) + 1):
|
||||
print(formattedColumnData['Antall cookies'][row] + formattedColumnData['sukker(g)'][row])
|
||||
def getColumnLength(column, padding) -> int:
|
||||
lengths = list(map(lambda element: len(str(element)), column))
|
||||
return max(lengths) + padding
|
||||
|
||||
def formatRows(column) -> List[str]:
|
||||
columnLength = getColumnLength(column, COLUMN_PADDING)
|
||||
formattedColumn = []
|
||||
for entry in column:
|
||||
formattedColumn.append(str(entry).ljust(columnLength))
|
||||
return formattedColumn
|
||||
|
||||
def formatColumns(unFormattedColumns, columnsToPrint) -> Dict[str, List[str]]:
|
||||
formattedColumns = {}
|
||||
for column in columnsToPrint:
|
||||
formattedColumns[column] = formatRows(unFormattedColumns[column])
|
||||
return formattedColumns
|
||||
|
||||
formattedColumns = formatColumns(rawColumns, columnsToPrint)
|
||||
|
||||
for row in range(0, len(formattedColumns['Antall cookies'])):
|
||||
thisRow=[]
|
||||
for column in formattedColumns:
|
||||
thisRow.append(formattedColumns[column][row])
|
||||
print(''.join(thisRow))
|
||||
|
|
Loading…
Reference in New Issue