diff --git a/UFCFVQ-15-M Programming Task 1.ipynb b/UFCFVQ-15-M Programming Task 1.ipynb index 1bca725da34a2fd21642f3069eca8d62772daf43..65c544fe02cc05dbb48ced55873554d63a61d743 100644 --- a/UFCFVQ-15-M Programming Task 1.ipynb +++ b/UFCFVQ-15-M Programming Task 1.ipynb @@ -454,7 +454,7 @@ "Result = {}\n", "ListOfData = []\n", "\n", - "#Explanation 1 and 2 at the end\n", + "#View explanation 1 and 2 at the end\n", "for x in range(len(CloumnName)):\n", " AllValues = [subList.strip(\"\\n\").split(',')[x] for subList in EachLine] \n", " test = AllValues[1:]\n", @@ -462,11 +462,12 @@ "#passing the key and value to a variable\n", "for columnNumber in range(len(CloumnName)):\n", " keyNames = CloumnName[columnNumber]\n", - " Result[keyNames] = ListOfData[columnNumber][:5] #In case to print all result remove the (5)\n", + " Result[keyNames] = ListOfData[columnNumber][:5] #In case to print all result remove the [:5]\n", "\n", "\n", - "#For better visualization, only 5 results in each column are outputted\n", + "# >> For better visualization, only 5 results in each column are outputted <<\n", "#Print out the result\n", + "\n", "Result" ] }, @@ -587,13 +588,16 @@ " \n", " #For loop to sort the items (key,value) \n", " for name, data in items.items():\n", + " \n", " # values contain string entries so parse them to the appropriate types\n", " data = [ \n", " float(EachItem) if (len(EachItem.split('.'))) \n", " else int(EachItem) if EachItem.isnumeric()\n", " else 0 for EachItem in data\n", + " \n", " ]\n", " \n", + " \n", " #To get the result, stats have the [key] value and apply each function over ecah line of data\n", " stats[name] = [\n", " round(meanFinder(data),3),\n", @@ -602,7 +606,9 @@ " percentileFinder(data,25),\n", " percentileFinder(data,50),\n", " percentileFinder(data,75)\n", + " \n", " ]\n", + " \n", " #return the result\n", " return stats\n", "\n", @@ -651,62 +657,68 @@ } ], "source": [ + "'''\n", + "This function takes in two arguments: a dictionary of statistics and a padding value\n", + "The statistics dictionary has keys that are strings and values that are lists of strings\n", + "The padding value is an integer that determines the amount of space between the table columns\n", + "The separate argument is a string that is used to separate the columns in the table\n", + "'''\n", + "\n", "def CustomStatisticsTable(stats, padding=1, separate='*'):\n", - " # All data that is saved inside (statisticsToMemory) will be appended to a list \n", + " # Initialize an empty list to store all the data\n", " AllData = []\n", " \n", - " #For loop to save the data in (statistics Memory) in order to be read by this function correctly.\n", + " # Iterate over the key-value pairs in the stats dictionary and append the key and value to AllData\n", " for names, data in stats.items():\n", " InOrder = [names] + data \n", " AllData.append(InOrder)\n", " \n", - " # Create a list to store the widths of each column\n", + " # Initialize an empty list to store the widths of each column in the table\n", " TableData = []\n", "\n", - " # Find the maximum width of each column\n", + " # Calculate the maximum width of the elements in each list in AllData and append it to TableData\n", " for ItemInCol in AllData:\n", " max_width = max(len(str(EachItem)) for EachItem in ItemInCol)\n", " TableData.append(padding + max_width)\n", " \n", - " \n", - " # Initialize the UpperBorder variable with the appropriate padding\n", + " # Generate the upper border of the table by concatenating the separate string the specified number of times for each column\n", " UpperBorder = \" \" * (TableData[0] + 1)\n", - "\n", - " # Iterate over the range of indices for the TableData list, starting at 1\n", " for i in range(1, len(TableData)):\n", - " # Concatenate the separate character multiplied by the value at the current index\n", " UpperBorder += separate * TableData[i]\n", "\n", - " # Add the final set of separate characters to the end of the string\n", + " # Add extra padding to the upper border\n", " UpperBorder += separate * 10\n", " \n", - " #Print the upper *, which starts from 2* padding space.\n", + " # Print the upper border\n", " print(UpperBorder)\n", " \n", + " # Convert the rows in AllData to a list of lists, where each inner list represents a row in the table\n", + " rows_to_list = []\n", + " for i in range(len(AllData[0])):\n", + " row = []\n", + " for item_in_col in AllData:\n", + " row.append(item_in_col[i])\n", + " rows_to_list.append(row)\n", + "\n", + " # Generate the lower border of the table by concatenating the separate string the specified number of times for each column\n", + " lower_border = separate.join([separate*TableData[star] for star in range(len(TableData))]) + separate*2\n", " \n", - " # Save all data inside list that contains sublists\n", - " RowsToList = [[ItemInCol[i] for ItemInCol in AllData]\n", - " for i in range(len(AllData[0]))]\n", - " \n", - " # Drow (*), without any special width or padding\n", - " LowerBorder = separate.join(separate*TableData[star]\n", - " for star in range(len(TableData))) + separate*2\n", - " \n", - " #for loop to print the custom table\n", - " for RowNumber, Row in enumerate(RowsToList):\n", + " # Iterate over the rows in rows_to_list and print each row\n", + " for RowNumber, Row in enumerate(rows_to_list):\n", " if RowNumber==0:\n", - " #Print the (*) border and the key names when it is the first row \n", + " # Print the first row preceded by a space\n", " print(\" \" + separate.join(str(Row[line]).center(TableData[line])for line in range(len(Row))) + separate)\n", " else:\n", - " # To print stars between each value \n", + " # Print all other rows preceded by the separate string\n", " print(separate + separate.join(str(Row[star]).center(TableData[star])for star in range(len(Row)))+ separate) \n", " if RowNumber == 0:\n", - " #When the upper first section finished and it is still row number 1, print a border of (*)\n", - " print(LowerBorder) \n", - " print(LowerBorder) \n", + " # Print the lower border after the first row\n", + " print(lower_border) \n", + " # Print the lower border after the last row\n", + " print(lower_border) \n", "\n", - "#Call the function and pass (statisticsToMemory) values \n", - "CustomStatisticsTable(statisticsToMemory)" + "# Call the function and pass 'statisticsToMemory' value\n", + "CustomStatisticsTable(statisticsToMemory)\n" ] }, {