Advertisement
makispaiktis

Exetastiki2020 - Code 2 - Quality and Text

Jul 13th, 2020 (edited)
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. from quality import quality
  2.  
  3. # Question 17
  4. # print(quality("one easy way to con serve water"))
  5.  
  6. # Questions 18, 19 and 20
  7. def split(text):
  8.     # Data
  9.     n = len(text)
  10.     q = [0 for _ in range(n+1)]
  11.     split_points = [None for _ in range(n+1)]
  12.     q[0] = 0# αρχική τιμή του αναδρομικού τύπου
  13.     split_points[0] = None
  14.  
  15.     # For-loops
  16.     for i in range(1, n+1):
  17.         maxI = -float('inf')
  18.         split_point = None
  19.         for j in range(0, i):
  20.             value = q[j] + quality(text[j:i])
  21.             if value > maxI:
  22.                 maxI = value
  23.                 split_point = j
  24.         q[i] = maxI
  25.         split_points[i] = split_point
  26.  
  27.  
  28.     return q[n], split_points
  29.  
  30.  
  31. # Function 2
  32. def split2str(text, split_points, i):
  33.     j = split_points[i]# συμπληρώστε
  34.     if j==0:
  35.         return text[0:i]
  36.     word = text[j:i]
  37.     return split2str(text, split_points, i-1)+" "+word # συμπληρώστε το ???
  38.  
  39.  
  40.  
  41.  
  42. # MAIN FUNCTION
  43. text = "lee'ssummitisanamtraktrainstationinlee'ssummitmissouriunitedstatesthestationwasoriginallybuiltin1905bythemissouripacificrailroadalthoughthestationhouseremainsintactpassengerboardthetrainsfromanall-glassrailhouseacrossthetracks"
  44. qual, split_points = split(text)
  45. print(qual)
  46. print(split2str(text, split_points, len(text)-1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement