From 07d432ed39f1f63abfd8d8fc4580ca8646cbe99f Mon Sep 17 00:00:00 2001 From: brodriguez8774 <brodriguez8774@gmail.com> Date: Thu, 26 Sep 2019 22:26:01 -0400 Subject: [PATCH] Improve handling of display output, particularly with final results --- documents/references.md | 3 +++ resources/interface.py | 18 +++++++------- resources/knapsack.py | 52 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/documents/references.md b/documents/references.md index 3dff948..4585f73 100644 --- a/documents/references.md +++ b/documents/references.md @@ -20,6 +20,9 @@ Used to move to the "Script's Directory", regardless of where the script was lau ### Reading/Parsing JSON File with Python <https://linuxconfig.org/how-to-parse-data-from-json-into-python> +## Padding Console Output in Python +<https://stackoverflow.com/a/5676884> + ## Older Relevant References References linked in previous assignments. diff --git a/resources/interface.py b/resources/interface.py index 8ac7fda..c0b1e7e 100644 --- a/resources/interface.py +++ b/resources/interface.py @@ -43,7 +43,7 @@ class UserInterface(): self.display_program_values() elif user_input == '3': self.solve_knapsack_problem() - elif user_input == '4': + elif user_input == '0': self._run_program_bool_ = False logger.info('Exiting Program') else: @@ -59,7 +59,7 @@ class UserInterface(): logger.info(' 1) Set program values.') logger.info(' 2) Display current program values.') logger.info(' 3) Solve Knapsack Problem with current program values.') - logger.info(' 4) Exit program.') + logger.info(' 0) Exit program.') #region Program Values Menu @@ -86,7 +86,7 @@ class UserInterface(): self.add_to_item_set() elif user_input == '5': self.clear_item_set() - elif user_input == '6': + elif user_input == '0': stay_in_menu = False else: logger.info('Unrecognized user input. Please try again.') @@ -103,7 +103,7 @@ class UserInterface(): logger.info(' 3) Set "Item Set" by JSON import.') logger.info(' 4) Add value to "Item Set" manually.') logger.info(' 5) Clear entire "Item Set".') - logger.info(' 6) Back') + logger.info(' 0) Back') def set_max_weight(self): """ @@ -207,10 +207,12 @@ class UserInterface(): Solves knapsack with current problems and displays result to user. """ try: - self._knapsack_algorithm.display_max_weight() - self._knapsack_algorithm.display_item_set() + # Calculate results. results = self._knapsack_algorithm.calculate_fractional_knapsack() - logger.info('Optimal backpack contents, by weight of each item to take:') - logger.info(results) + + # Print program values and results. + logger.info('') + self._knapsack_algorithm.display_max_weight() + self._knapsack_algorithm.display_item_set(results) except ValueError as err: logger.info(err) diff --git a/resources/knapsack.py b/resources/knapsack.py index e2669e0..1553d0b 100644 --- a/resources/knapsack.py +++ b/resources/knapsack.py @@ -155,11 +155,59 @@ class FractionalKnapsackAlgorithm(): self._max_weight_ = 0 self._weight_populated_ = False - def display_item_set(self): + def display_item_set(self, result_array=None): """ Prints current item set with logging. + :param result_array: Optional array of results. """ - logger.info('Current Item Set: {0}'.format(self._item_set_)) + if len(self._item_set_) > 0: + # Handle for item set having values. + logger.info('Current Item Set:') + + # Print top bar of table. + if result_array is None: + logger.info('================================================================') + logger.info('| Item # | Total Benefit | Total Amount | Value Per One Weight |') + logger.info('================================================================') + else: + logger.info('===============================================================================') + logger.info('| Item # | Total Benefit | Total Amount | Value Per One Weight | Amount Taken |') + logger.info('===============================================================================') + + # Print table contents. + for index in range(len(self._item_set_)): + + item_benefit = self._item_set_[index]['benefit'] + item_cost = self._item_set_[index]['cost'] + if item_cost != 0: + item_ratio = item_benefit / item_cost + else: + item_ratio = item_benefit + + if result_array is None: + logger.info('| {0:>6} | {1:>13} | {2:>12} | {3:>20} |'.format( + index + 1, + item_benefit, + item_cost, + item_ratio, + )) + else: + logger.info('| {0:>6} | {1:>13} | {2:>12} | {3:>20} | {4:>12} |'.format( + index + 1, + item_benefit, + item_cost, + item_ratio, + result_array[index], + )) + + # Print bottom bar of table. + if result_array is None: + logger.info('================================================================') + else: + logger.info('===============================================================================') + else: + # Handle for item set being empty. + logger.info('Current Item Set: []') def display_max_weight(self): """ -- GitLab