Repository where I mostly put random python scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.5 KiB

  1. import math
  2. """
  3. Jeffery Russell
  4. 11-13-18
  5. """
  6. def extraSpace(S, M, i, j):
  7. """
  8. Computes the number of extra characters at the end of
  9. the line.
  10. Between each word there is only once space.
  11. :param S: List of words
  12. :param M: Max length of line
  13. :param i: start word index
  14. :param j: end word index
  15. """
  16. extraSpaces = M - j + i
  17. for x in range(i, j + 1):
  18. extraSpaces -= len(S[x])
  19. return extraSpaces
  20. def badnessLine(S, M, i, j):
  21. """
  22. Computes Line badness. This is the number of
  23. extra spaces or infinity if the length exceeds M
  24. :param S: List of words
  25. :param M: Max length of line
  26. :param i: start word index
  27. :param j: end word index
  28. """
  29. es = extraSpace(S, M, i, j)
  30. if es < 0:
  31. return math.inf
  32. return es
  33. def minBad(S, M, i):
  34. """
  35. Computes the badness of a paragraph as the
  36. badness of the worst line in the paragraph not
  37. including the last line.
  38. *this is recursive
  39. :param S: List of words
  40. :param M: Max length of line
  41. :param i: start word index
  42. """
  43. if badnessLine(S, M, i, len(S) -1) != math.inf:
  44. return 0
  45. min = math.inf
  46. for k in range(i + 1, len(S)):
  47. end = minBad(S, M, k)
  48. front = badnessLine(S, M, i, k - 1)
  49. max = end if end > front else front
  50. min = min if min < max else max
  51. return min
  52. def minBadDynamic(S, M):
  53. """
  54. Write a procedure minBadDynamic that implements
  55. the function mb' using dynamic program-
  56. ming. It should take only two parameters: S and M
  57. :param S: List of words
  58. :param M: Max length of line
  59. """
  60. cost = [math.inf for i in range(len(S))]
  61. for i in range(0, len(S)):
  62. if badnessLine(S, M, 0, i) != math.inf:
  63. cost[i] = badnessLine(S, M, 0, i)
  64. if i == len(S) -1:
  65. return 0 #Everything fit on one line
  66. else:
  67. min = math.inf
  68. for k in range(0, i):
  69. before = cost[k]
  70. after = badnessLine(S, M, k + 1, i)
  71. if i == len(S) -1 and badnessLine(S, M, k+1, i) != math.inf:
  72. after = 0 # Last Line
  73. max = before if before > after else after
  74. min = min if min < max else max
  75. cost[i] = min
  76. return cost[len(S) -1]
  77. def minBadDynamicChoice(S, M):
  78. """
  79. Write a procedure minBadDynamicChoice that implements
  80. the function mb' using dynamic
  81. programming. In addition to returning mb(S, M ), it
  82. should also return the choices made
  83. :param S: List of words
  84. :param M: Max length of line
  85. """
  86. cost = [math.inf for i in range(len(S))]
  87. # List of tuples indicating start/end indices of line
  88. choice = [[] for i in range(len(S))]
  89. for i in range(0, len(S)):
  90. if badnessLine(S, M, 0, i) != math.inf:
  91. cost[i] = badnessLine(S, M, 0, i)
  92. choice[i] = [(0, i)]
  93. if i == len(S) - 1:
  94. return 0, [(0,i)] # One line
  95. else:
  96. min = math.inf
  97. choiceCanidate = []
  98. for k in range(0, i): # Finds the optimal solution
  99. before = cost[k] # Previously computed minimum
  100. after = badnessLine(S, M, k + 1, i) # Badness of new slice
  101. if i == len(S) - 1 and badnessLine(S, M, k+1, i) != math.inf:
  102. after = 0 # Last line
  103. max = before if before > after else after
  104. if min > max:
  105. # Captures where slice is being taken
  106. choiceCanidate = choice[k] + [(k+1, i)]
  107. min = max
  108. choice[i] = choiceCanidate
  109. cost[i] = min
  110. return cost[len(S) -1], choice[len(S) -1]
  111. def printParagraph(S, M):
  112. """
  113. which takes two parameters: S and M that displays the words in S on
  114. the screen using the choices of minBadDynamicChoice.
  115. What is the asymptotic running time of your
  116. algorithm?
  117. This program will run asymptotically in O(n^2) due
  118. to the characteristic of calculating the minBad
  119. for each sub sequence and then looping through a
  120. maximum of |S| to find the optimal solution.
  121. :param S: List of words
  122. :param M: Max length of line
  123. """
  124. cost, choice = minBadDynamicChoice(S, M)
  125. print()
  126. for i in range(0, len(choice)):
  127. for x in range(choice[i][0], choice[i][1] + 1):
  128. print(str(S[x]), end=" ")
  129. print()
  130. print(minBadDynamicChoice(["aaa", "aaa"], 6))
  131. printParagraph(["jjjjjj","aaa", "bb", "cc", "ff", "mm", "ddddd", "ddddd"], 6)
  132. printParagraph(["jjjjjj"], 6)