Browse Source

Updated the quote system to use dynamic programming to print a better formatted message.

pull/17/head
jrtechs 6 years ago
parent
commit
7a7d033ecf
8 changed files with 197 additions and 5 deletions
  1. +3
    -0
      src/.zshrc
  2. +1
    -1
      src/configuration.py
  3. +1
    -1
      src/mount_ssh_drive.py
  4. +1
    -1
      src/quote.py
  5. +4
    -1
      src/roosay.py
  6. +1
    -1
      src/ssh_manager.py
  7. +0
    -0
      src/utils/module.py
  8. +186
    -0
      src/utils/print_message_bubble.py

+ 3
- 0
src/.zshrc View File

@ -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. # If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH # export PATH=$HOME/bin:/usr/local/bin:$PATH

+ 1
- 1
src/configuration.py View File

@ -16,7 +16,7 @@ config dictionary
import os.path import os.path
import module
from utils import module
CONFIG_FILE = os.path.dirname(__file__) + "/config.txt" CONFIG_FILE = os.path.dirname(__file__) + "/config.txt"

+ 1
- 1
src/mount_ssh_drive.py View File

@ -6,7 +6,7 @@ Jeffery Russell
import subprocess import subprocess
import sys import sys
import module
from utils import module
import configuration import configuration

+ 1
- 1
src/quote.py View File

@ -9,7 +9,7 @@ Jeffery Russell
import random import random
import sys import sys
import module
from utils import module
import roosay import roosay
import configuration import configuration

+ 4
- 1
src/roosay.py View File

@ -6,6 +6,8 @@ Jeffery Russell
import sys import sys
from utils import print_message_bubble
def main(): def main():
""" """
@ -32,7 +34,8 @@ def roo_say(message):
:param message: the message to print :param message: the message to print
:return: none :return: none
""" """
print_message(message)
print_message_bubble.print_message(message)
#print_message(message)
print_roo() print_roo()

+ 1
- 1
src/ssh_manager.py View File

@ -6,7 +6,7 @@ Jeffery Russell
import subprocess import subprocess
import collections import collections
import module
from utils import module
import configuration import configuration
import mount_ssh_drive import mount_ssh_drive

src/module.py → src/utils/module.py View File


+ 186
- 0
src/utils/print_message_bubble.py View File

@ -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")

Loading…
Cancel
Save