From 7a7d033ecfdd7ad08ff509970bddc65464f9832f Mon Sep 17 00:00:00 2001 From: jrtechs Date: Fri, 30 Nov 2018 22:14:32 -0500 Subject: [PATCH] Updated the quote system to use dynamic programming to print a better formatted message. --- src/.zshrc | 3 + src/configuration.py | 2 +- src/mount_ssh_drive.py | 2 +- src/quote.py | 2 +- src/roosay.py | 5 +- src/ssh_manager.py | 2 +- src/{ => utils}/module.py | 0 src/utils/print_message_bubble.py | 186 ++++++++++++++++++++++++++++++ 8 files changed, 197 insertions(+), 5 deletions(-) rename src/{ => utils}/module.py (100%) create mode 100644 src/utils/print_message_bubble.py diff --git a/src/.zshrc b/src/.zshrc index 89d7809..b10d6ee 100644 --- a/src/.zshrc +++ b/src/.zshrc @@ -1,3 +1,6 @@ +#make sure that scripts don't use changes +[[ $- != *i* ]] && return + # If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin:$PATH diff --git a/src/configuration.py b/src/configuration.py index 3df1245..f22ab30 100644 --- a/src/configuration.py +++ b/src/configuration.py @@ -16,7 +16,7 @@ config dictionary import os.path -import module +from utils import module CONFIG_FILE = os.path.dirname(__file__) + "/config.txt" diff --git a/src/mount_ssh_drive.py b/src/mount_ssh_drive.py index 96195fc..ea66502 100644 --- a/src/mount_ssh_drive.py +++ b/src/mount_ssh_drive.py @@ -6,7 +6,7 @@ Jeffery Russell import subprocess import sys -import module +from utils import module import configuration diff --git a/src/quote.py b/src/quote.py index b8dddbc..2c0222e 100644 --- a/src/quote.py +++ b/src/quote.py @@ -9,7 +9,7 @@ Jeffery Russell import random import sys -import module +from utils import module import roosay import configuration diff --git a/src/roosay.py b/src/roosay.py index 43a0b14..de8edde 100644 --- a/src/roosay.py +++ b/src/roosay.py @@ -6,6 +6,8 @@ Jeffery Russell import sys +from utils import print_message_bubble + def main(): """ @@ -32,7 +34,8 @@ def roo_say(message): :param message: the message to print :return: none """ - print_message(message) + print_message_bubble.print_message(message) + #print_message(message) print_roo() diff --git a/src/ssh_manager.py b/src/ssh_manager.py index 88514d7..cfec005 100644 --- a/src/ssh_manager.py +++ b/src/ssh_manager.py @@ -6,7 +6,7 @@ Jeffery Russell import subprocess import collections -import module +from utils import module import configuration import mount_ssh_drive diff --git a/src/module.py b/src/utils/module.py similarity index 100% rename from src/module.py rename to src/utils/module.py diff --git a/src/utils/print_message_bubble.py b/src/utils/print_message_bubble.py new file mode 100644 index 0000000..bfe8422 --- /dev/null +++ b/src/utils/print_message_bubble.py @@ -0,0 +1,186 @@ +import math + + +""" +Python script to print the message blurb above +the asci actor for quotes + +11-3-18 +Jeffery Russell +""" + +MAX_MESSAGE_WIDTH = 35 + + +def extraSpace(S, M, i, j): + """ + Computes the number of extra characters at the end of + the line. + Between each word there is only once space. + + :param S: List of words + :param M: Max length of line + :param i: start word index + :param j: end word index + """ + extraSpaces = M - j + i + for x in range(i, j + 1): + extraSpaces -= len(S[x]) + return extraSpaces + + +def badnessLine(S, M, i, j): + """ + Computes Line badness. This is the number of + extra spaces or infinity if the length exceeds M + + :param S: List of words + :param M: Max length of line + :param i: start word index + :param j: end word index + """ + es = extraSpace(S, M, i, j) + if es < 0: + return math.inf + return es + + +def minBadDynamicChoice(S, M): + """ + Write a procedure minBadDynamicChoice that implements + the function mb' using dynamic + programming. In addition to returning mb(S, M ), it + should also return the choices made + + :param S: List of words + :param M: Max length of line + """ + cost = [math.inf for i in range(len(S))] + + # List of tuples indicating start/end indices of line + choice = [[] for i in range(len(S))] + + for i in range(0, len(S)): + if badnessLine(S, M, 0, i) != math.inf: + cost[i] = badnessLine(S, M, 0, i) + choice[i] = [(0, i)] + if i == len(S) - 1: + return 0, [(0,i)] # One line + else: + min = math.inf + choiceCanidate = [] + for k in range(0, i): # Finds the optimal solution + before = cost[k] # Previously computed minimum + after = badnessLine(S, M, k + 1, i) # Badness of new slice + if i == len(S) - 1 and badnessLine(S, M, k+1, i) != math.inf: + after = 0 # Last line + max = before if before > after else after + if min > max: + # Captures where slice is being taken + choiceCanidate = choice[k] + [(k+1, i)] + min = max + choice[i] = choiceCanidate + cost[i] = min + return cost[len(S) -1], choice[len(S) -1] + + +def getMaxLineWidth(S, choice): + """ + Computes the max width of a solution given + :param S: List of words + :param choice: tuples of the start/end indecies of the words to print + :return: max width + """ + max = -1 + for i in range(0, len(choice)): + print_size = 0 + for x in range(choice[i][0], choice[i][1] + 1): + print_size += len(str(S[x])) + if not x == choice[i][1]: + print_size += 1 + if max < print_size: + max = print_size + return max + + +def getBestSolution(S): + """ + Computes the best way to print the message by varying the + max width at most 10 spaces. + :param S: List of words + :return: choices to print optimal solution + """ + bestCost = math.inf + bestSolution = None + for i in range(MAX_MESSAGE_WIDTH -5, MAX_MESSAGE_WIDTH + 5): + cost, choice = minBadDynamicChoice(S, i) + if cost < bestCost: + bestSolution = choice + bestCost = cost + return bestSolution + + +def printParagraph(S): + """ + This will print the message in the optimal way + which reduces the maximum number of blank spaces + at the end of a line (excluding last line). + The message will be printed in the form of a + message bubble. + + ------------------------------------- + / One day I will find the right words \ + \ and they will be simple. / + ------------------------------------- + + ------------------------ + < No rest for the wicked > + ------------------------ + + ---------------------------------- + / A bug is never just a mistake. \ + | It represents something bigger. | + | An error of thinking. That makes | + \ you who you are. / + ---------------------------------- + + :param S: List of words + """ + choice = getBestSolution(S) + + max_line_width = getMaxLineWidth(S, choice) + + "Print top" + print(" " + "-" * (2 + max_line_width)) + for i in range(0, len(choice)): + if len(choice) == 1: print("< ", end="") + elif i == 0: print("/ ", end="") + elif i == len(choice) - 1: print("\\ ", end="") + else: print("| ", end="") + + print_size = 0 + for x in range(choice[i][0], choice[i][1] + 1): + print_size += len(str(S[x])) + print(str(S[x]), end="") + if not x == choice[i][1]: + print_size += 1 + print(" ", end="") + + "print ending padding" + print(" " * (max_line_width - print_size), end="") + + if len(choice) == 1: print(" >", end="") + elif i == 0: print(" \\", end="") + elif i == len(choice) -1: print(" /", end="") + else: print(" |", end="") + print() + "print bottom" + print(" " + "-" * (2 + max_line_width)) + + + +def print_message(message): + if len(message) > 0: + printParagraph(message.split(" ")) + else: + print("Please pass in a message parameter") \ No newline at end of file