Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1.  
  2.  
  3. class Rope:
  4.  
  5. # Initializes the Rope to contain the provided string
  6. def __init__(self, data=""):
  7. self.data = data
  8. self.weight = len(data)
  9. self.right_child = None
  10. self.left_child = None
  11.  
  12. # Adds the provided string to the end of this rope
  13. def __add__(self, other_rope):
  14. # Creates the new Rope that stores the new String
  15. new_rope = Rope()
  16.  
  17. # Puts the two provided ropes as the children of the new rope
  18. new_rope.left_child = self
  19. new_rope.right_child = other_rope
  20.  
  21. # Sets the weight of the new rope
  22. new_rope.weight = len(self)
  23.  
  24. return new_rope
  25.  
  26. # Finds the length of the current Rope
  27. def __len__(self):
  28. # Uses the fact that weight is the length of the left child
  29. # and adds it to the length of the right child recursively
  30. if (self.right_child):
  31. return self.weight + len(self.right_child)
  32. # Provides the base case for when there is no right child
  33. else:
  34. return self.weight
  35.  
  36. # Returns the character at the given index
  37. def get(self, index):
  38. # If the weight of the current node is less than the index,
  39. # it is in the right subtree if it exists
  40. if (self.weight <= index and self.right_child):
  41. return self.right_child.get(index - self.weight)
  42. # Otherwise it is in the left subtree if it exists
  43. elif (self.left_child):
  44. return self.left_child.get(index)
  45. # If the node is a leaf node
  46. else:
  47. return self.data[index]
  48.  
  49. # Returns the rope as a string
  50. def __str__(self):
  51. # Adds the left and right subtrees as strings recursively
  52. if (self.left_child and self.right_child):
  53. return self.left_child.__str__() + self.right_child.__str__()
  54. # The string representation of this subtree is equal to that of its left subtree
  55. elif (self.left_child):
  56. return self.left_child.__str__()
  57. # The string representation of this subtree is equal to that of its right subtree
  58. elif (self.right_child):
  59. return self.right_child.__str__()
  60. # It is a leaf node, so its string representation is just its data
  61. else:
  62. return self.data
  63.  
  64. def lsplit(self, index):
  65. """
  66. returns only the left side of a split at $index
  67. """
  68. if index == 0:
  69. return Rope() # Empty rope, nothing to the left of index 0
  70. if index == len(self):
  71. return self # Entire rope, nothing to the right of the index
  72. if index == self.weight and self.left_child:
  73. return self.left_child # the index is splitting at this weight, return just the left child
  74. if index > self.weight and self.right_child:
  75. if self.left_child: # if index greater than weight,
  76. return self.left_child + self.right_child.lsplit(index - self.weight) # split is to the right of weight
  77. else: # so we call lsplit on right
  78. return self.right_child.lsplit(index - self.weight) # child with our current weight
  79. if index < self.weight and self.left_child: # removed
  80. return self.left_child.lsplit(index)
  81. else:
  82. return Rope(self.data[:index])
  83.  
  84. def rsplit(self, index):
  85. """
  86. returns only the right side of a split at $index
  87. the entire function is the same as lsplit but for the right side
  88. """
  89. if index == 0:
  90. return self
  91. if index == len(self):
  92. return Rope()
  93. if index == self.weight and self.right_child:
  94. return self.right_child
  95. if index > self.weight and self.right_child:
  96. return self.right_child.rsplit(index - self.weight)
  97. if index < self.weight and self.left_child:
  98. if self.right_child:
  99. return self.left_child.rsplit(index) + self.right_child
  100. else:
  101. return self.left_child.rsplit(index)
  102. else:
  103. return Rope(self.data[index:])
  104.  
  105. # Deletes the elements from index1 to index2
  106. def delete(self, index1, index2=None):
  107. if not index2:
  108. index2 = index1 + 1
  109. left = self.lsplit(index1)
  110. right = self.rsplit(index2)
  111. return left + right
  112.  
  113.  
  114. # rope_1 = Rope("Davis MacDonald")
  115. # rope_1.delete(5, 7)
  116. # print(rope_1)
  117.  
  118. r2 = Rope("0123")
  119. r3 = Rope("456")
  120. r4 = r2 + r3
  121. r5 = Rope("789")
  122. r6 = Rope("0")
  123. r7 = r5 + r6
  124. r8 = r4 + r7
  125. print(r8)
  126. print(r8.delete(0))
  127. print(r8.delete(1))
  128. print(r8.delete(2))
  129. print(r8.delete(3))
  130. print(r8.delete(4))
  131. print(r8.delete(5))
  132. print(r8.delete(6))
  133. print(r8.delete(7))
  134. print(r8.delete(8))
  135. print(r8.delete(0, 8))
  136. print(r8.delete(1, 8))
  137. print(r8.delete(3, 8))
  138. print(r8.delete(2, 6))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement