Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Rope:
- # Initializes the Rope to contain the provided string
- def __init__(self, data=""):
- self.data = data
- self.weight = len(data)
- self.right_child = None
- self.left_child = None
- # Adds the provided string to the end of this rope
- def __add__(self, other_rope):
- # Creates the new Rope that stores the new String
- new_rope = Rope()
- # Puts the two provided ropes as the children of the new rope
- new_rope.left_child = self
- new_rope.right_child = other_rope
- # Sets the weight of the new rope
- new_rope.weight = len(self)
- return new_rope
- # Finds the length of the current Rope
- def __len__(self):
- # Uses the fact that weight is the length of the left child
- # and adds it to the length of the right child recursively
- if (self.right_child):
- return self.weight + len(self.right_child)
- # Provides the base case for when there is no right child
- else:
- return self.weight
- # Returns the character at the given index
- def get(self, index):
- # If the weight of the current node is less than the index,
- # it is in the right subtree if it exists
- if (self.weight <= index and self.right_child):
- return self.right_child.get(index - self.weight)
- # Otherwise it is in the left subtree if it exists
- elif (self.left_child):
- return self.left_child.get(index)
- # If the node is a leaf node
- else:
- return self.data[index]
- # Returns the rope as a string
- def __str__(self):
- # Adds the left and right subtrees as strings recursively
- if (self.left_child and self.right_child):
- return self.left_child.__str__() + self.right_child.__str__()
- # The string representation of this subtree is equal to that of its left subtree
- elif (self.left_child):
- return self.left_child.__str__()
- # The string representation of this subtree is equal to that of its right subtree
- elif (self.right_child):
- return self.right_child.__str__()
- # It is a leaf node, so its string representation is just its data
- else:
- return self.data
- def lsplit(self, index):
- """
- returns only the left side of a split at $index
- """
- if index == 0:
- return Rope() # Empty rope, nothing to the left of index 0
- if index == len(self):
- return self # Entire rope, nothing to the right of the index
- if index == self.weight and self.left_child:
- return self.left_child # the index is splitting at this weight, return just the left child
- if index > self.weight and self.right_child:
- if self.left_child: # if index greater than weight,
- return self.left_child + self.right_child.lsplit(index - self.weight) # split is to the right of weight
- else: # so we call lsplit on right
- return self.right_child.lsplit(index - self.weight) # child with our current weight
- if index < self.weight and self.left_child: # removed
- return self.left_child.lsplit(index)
- else:
- return Rope(self.data[:index])
- def rsplit(self, index):
- """
- returns only the right side of a split at $index
- the entire function is the same as lsplit but for the right side
- """
- if index == 0:
- return self
- if index == len(self):
- return Rope()
- if index == self.weight and self.right_child:
- return self.right_child
- if index > self.weight and self.right_child:
- return self.right_child.rsplit(index - self.weight)
- if index < self.weight and self.left_child:
- if self.right_child:
- return self.left_child.rsplit(index) + self.right_child
- else:
- return self.left_child.rsplit(index)
- else:
- return Rope(self.data[index:])
- # Deletes the elements from index1 to index2
- def delete(self, index1, index2=None):
- if not index2:
- index2 = index1 + 1
- left = self.lsplit(index1)
- right = self.rsplit(index2)
- return left + right
- # rope_1 = Rope("Davis MacDonald")
- # rope_1.delete(5, 7)
- # print(rope_1)
- r2 = Rope("0123")
- r3 = Rope("456")
- r4 = r2 + r3
- r5 = Rope("789")
- r6 = Rope("0")
- r7 = r5 + r6
- r8 = r4 + r7
- print(r8)
- print(r8.delete(0))
- print(r8.delete(1))
- print(r8.delete(2))
- print(r8.delete(3))
- print(r8.delete(4))
- print(r8.delete(5))
- print(r8.delete(6))
- print(r8.delete(7))
- print(r8.delete(8))
- print(r8.delete(0, 8))
- print(r8.delete(1, 8))
- print(r8.delete(3, 8))
- print(r8.delete(2, 6))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement