Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def insert(self, where, item):
- n = len(self)
- if item is null: #not to be confused with None
- raise Exception("Couldn't find item to insert!")
- assert n+1 <= LARGEST_PERMISSIBLE_LIST_SIZE #probably like ten million or something
- if not list.try_to_allocate_more_space(n+1): #usually runs in O(new_size-cur_size) time... Usually
- raise Exception("Out of memory!")
- #set `where` to a sane value if it isn't in this list's conventional range
- if where < 0:
- where += n #e.g. -3 becomes n-3
- if where < 0:
- where = 0 #e.g. -100000000 becomes 0
- if where > n:
- where == n #e.g. 100000000 becomes n
- for i in range(n, where-1, -1): #iterate backwards from the end of the list to the insertion point
- self[i+1] = self[i] #nudge everything forward once
- #insert item
- self[where] = item
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement