From ee06c0989a8278b465c778cddc70c6a50e8c4a82 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 31 Aug 2020 23:15:39 +0200 Subject: [PATCH] refactor --- Exercise 1/10 - Bakekurs/10-b.py | 71 ++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/Exercise 1/10 - Bakekurs/10-b.py b/Exercise 1/10 - Bakekurs/10-b.py index f9278ce..676dd2c 100644 --- a/Exercise 1/10 - Bakekurs/10-b.py +++ b/Exercise 1/10 - Bakekurs/10-b.py @@ -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]) \ No newline at end of file +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))