function
stringlengths
18
3.86k
intent_category
stringlengths
5
24
def is_empty(self) -> bool: return self.head is None
data_structures
def __str__(self) -> str: if self.is_empty(): return "" else: iterate = self.head item_str = "" item_list: list[str] = [] while iterate: item_list.append(str(iterate.item)) iterate = iterate.next item_str = " --> ".join(item_list) return item_str
data_structures
def __init__(self, data: int) -> None: self.data = data self.next = None
data_structures
def __init__(self): self.head = None
data_structures
def push(self, new_data: int) -> int: new_node = Node(new_data) new_node.next = self.head self.head = new_node return self.head.data
data_structures
def middle_element(self) -> int | None: slow_pointer = self.head fast_pointer = self.head if self.head: while fast_pointer and fast_pointer.next: fast_pointer = fast_pointer.next.next slow_pointer = slow_pointer.next return slow_pointer.data else: print("No element found.") return None
data_structures
def __init__(self, data=None): self.data = data self.next = None
data_structures
def __init__(self, key: KT | str = "root", value: VT | None = None): self.key = key self.value = value self.forward: list[Node[KT, VT]] = []
data_structures
def __repr__(self) -> str: return f"Node({self.key}: {self.value})"
data_structures
def level(self) -> int: return len(self.forward)
data_structures
def __init__(self, p: float = 0.5, max_level: int = 16): self.head: Node[KT, VT] = Node[KT, VT]() self.level = 0 self.p = p self.max_level = max_level
data_structures
def __str__(self) -> str: items = list(self) if len(items) == 0: return f"SkipList(level={self.level})" label_size = max((len(str(item)) for item in items), default=4) label_size = max(label_size, 4) + 4 node = self.head lines = [] forwards = node.forward.copy() lines.append(f"[{node.key}]".ljust(label_size, "-") + "* " * len(forwards)) lines.append(" " * label_size + "| " * len(forwards)) while len(node.forward) != 0: node = node.forward[0] lines.append( f"[{node.key}]".ljust(label_size, "-") + " ".join(str(n.key) if n.key == node.key else "|" for n in forwards) ) lines.append(" " * label_size + "| " * len(forwards)) forwards[: node.level] = node.forward lines.append("None".ljust(label_size) + "* " * len(forwards)) return f"SkipList(level={self.level})\n" + "\n".join(lines)
data_structures
def __iter__(self): node = self.head while len(node.forward) != 0: yield node.forward[0].key node = node.forward[0]
data_structures
def random_level(self) -> int: level = 1 while random() < self.p and level < self.max_level: level += 1 return level
data_structures
def _locate_node(self, key) -> tuple[Node[KT, VT] | None, list[Node[KT, VT]]]: # Nodes with refer or should refer to output node update_vector = [] node = self.head for i in reversed(range(self.level)): # i < node.level - When node level is lesser than `i` decrement `i`. # node.forward[i].key < key - Jumping to node with key value higher # or equal to searched key would result # in skipping searched key. while i < node.level and node.forward[i].key < key: node = node.forward[i] # Each leftmost node (relative to searched node) will potentially have to # be updated. update_vector.append(node) update_vector.reverse() # Note that we were inserting values in reverse order. # len(node.forward) != 0 - If current node doesn't contain any further # references then searched key is not present. # node.forward[0].key == key - Next node key should be equal to search key # if key is present. if len(node.forward) != 0 and node.forward[0].key == key: return node.forward[0], update_vector else: return None, update_vector
data_structures
def delete(self, key: KT): node, update_vector = self._locate_node(key) if node is not None: for i, update_node in enumerate(update_vector): # Remove or replace all references to removed node. if update_node.level > i and update_node.forward[i].key == key: if node.level > i: update_node.forward[i] = node.forward[i] else: update_node.forward = update_node.forward[:i]
data_structures
def insert(self, key: KT, value: VT): node, update_vector = self._locate_node(key) if node is not None: node.value = value else: level = self.random_level() if level > self.level: # After level increase we have to add additional nodes to head. for _ in range(self.level - 1, level): update_vector.append(self.head) self.level = level new_node = Node(key, value) for i, update_node in enumerate(update_vector[:level]): # Change references to pass through new node. if update_node.level > i: new_node.forward.append(update_node.forward[i]) if update_node.level < i + 1: update_node.forward.append(new_node) else: update_node.forward[i] = new_node
data_structures
def find(self, key: VT) -> VT | None: node, _ = self._locate_node(key) if node is not None: return node.value return None
data_structures
def test_insert(): skip_list = SkipList() skip_list.insert("Key1", 3) skip_list.insert("Key2", 12) skip_list.insert("Key3", 41) skip_list.insert("Key4", -19) node = skip_list.head all_values = {} while node.level != 0: node = node.forward[0] all_values[node.key] = node.value assert len(all_values) == 4 assert all_values["Key1"] == 3 assert all_values["Key2"] == 12 assert all_values["Key3"] == 41 assert all_values["Key4"] == -19
data_structures
def test_insert_overrides_existing_value(): skip_list = SkipList() skip_list.insert("Key1", 10) skip_list.insert("Key1", 12) skip_list.insert("Key5", 7) skip_list.insert("Key7", 10) skip_list.insert("Key10", 5) skip_list.insert("Key7", 7) skip_list.insert("Key5", 5) skip_list.insert("Key10", 10) node = skip_list.head all_values = {} while node.level != 0: node = node.forward[0] all_values[node.key] = node.value if len(all_values) != 4: print() assert len(all_values) == 4 assert all_values["Key1"] == 12 assert all_values["Key7"] == 7 assert all_values["Key5"] == 5 assert all_values["Key10"] == 10
data_structures
def test_searching_empty_list_returns_none(): skip_list = SkipList() assert skip_list.find("Some key") is None
data_structures
def test_search(): skip_list = SkipList() skip_list.insert("Key2", 20) assert skip_list.find("Key2") == 20 skip_list.insert("Some Key", 10) skip_list.insert("Key2", 8) skip_list.insert("V", 13) assert skip_list.find("Y") is None assert skip_list.find("Key2") == 8 assert skip_list.find("Some Key") == 10 assert skip_list.find("V") == 13
data_structures
def test_deleting_item_from_empty_list_do_nothing(): skip_list = SkipList() skip_list.delete("Some key") assert len(skip_list.head.forward) == 0
data_structures
def test_deleted_items_are_not_founded_by_find_method(): skip_list = SkipList() skip_list.insert("Key1", 12) skip_list.insert("V", 13) skip_list.insert("X", 14) skip_list.insert("Key2", 15) skip_list.delete("V") skip_list.delete("Key2") assert skip_list.find("V") is None assert skip_list.find("Key2") is None
data_structures
def test_delete_removes_only_given_key(): skip_list = SkipList() skip_list.insert("Key1", 12) skip_list.insert("V", 13) skip_list.insert("X", 14) skip_list.insert("Key2", 15) skip_list.delete("V") assert skip_list.find("V") is None assert skip_list.find("X") == 14 assert skip_list.find("Key1") == 12 assert skip_list.find("Key2") == 15 skip_list.delete("X") assert skip_list.find("V") is None assert skip_list.find("X") is None assert skip_list.find("Key1") == 12 assert skip_list.find("Key2") == 15 skip_list.delete("Key1") assert skip_list.find("V") is None assert skip_list.find("X") is None assert skip_list.find("Key1") is None assert skip_list.find("Key2") == 15 skip_list.delete("Key2") assert skip_list.find("V") is None assert skip_list.find("X") is None assert skip_list.find("Key1") is None assert skip_list.find("Key2") is None
data_structures
def traverse_keys(node): yield node.key for forward_node in node.forward: yield from traverse_keys(forward_node)
data_structures
def is_sorted(lst): return all(next_item >= item for item, next_item in zip(lst, lst[1:]))
data_structures
def pytests(): for _ in range(100): # Repeat test 100 times due to the probabilistic nature of skip list # random values == random bugs test_insert() test_insert_overrides_existing_value() test_searching_empty_list_returns_none() test_search() test_deleting_item_from_empty_list_do_nothing() test_deleted_items_are_not_founded_by_find_method() test_delete_removes_only_given_key() test_delete_doesnt_leave_dead_nodes() test_iter_always_yields_sorted_values()
data_structures
def main(): skip_list = SkipList() skip_list.insert(2, "2") skip_list.insert(4, "4") skip_list.insert(6, "4") skip_list.insert(4, "5") skip_list.insert(8, "4") skip_list.insert(9, "4") skip_list.delete(4) print(skip_list)
data_structures
def __init__(self, data: Any): self.data: Any = data self.next: Node | None = None
data_structures
def __init__(self): self.head = None self.tail = None
data_structures
def __iter__(self) -> Iterator[Any]: node = self.head while self.head: yield node.data node = node.next if node == self.head: break
data_structures
def __len__(self) -> int: return len(tuple(iter(self)))
data_structures
def __repr__(self): return "->".join(str(item) for item in iter(self))
data_structures
def insert_tail(self, data: Any) -> None: self.insert_nth(len(self), data)
data_structures
def insert_head(self, data: Any) -> None: self.insert_nth(0, data)
data_structures
def insert_nth(self, index: int, data: Any) -> None: if index < 0 or index > len(self): raise IndexError("list index out of range.") new_node = Node(data) if self.head is None: new_node.next = new_node # first node points itself self.tail = self.head = new_node elif index == 0: # insert at head new_node.next = self.head self.head = self.tail.next = new_node else: temp = self.head for _ in range(index - 1): temp = temp.next new_node.next = temp.next temp.next = new_node if index == len(self) - 1: # insert at tail self.tail = new_node
data_structures
def delete_front(self): return self.delete_nth(0)
data_structures
def delete_tail(self) -> Any: return self.delete_nth(len(self) - 1)
data_structures
def delete_nth(self, index: int = 0) -> Any: if not 0 <= index < len(self): raise IndexError("list index out of range.") delete_node = self.head if self.head == self.tail: # just one node self.head = self.tail = None elif index == 0: # delete head node self.tail.next = self.tail.next.next self.head = self.head.next else: temp = self.head for _ in range(index - 1): temp = temp.next delete_node = temp.next temp.next = temp.next.next if index == len(self) - 1: # delete at tail self.tail = temp return delete_node.data
data_structures
def is_empty(self) -> bool: return len(self) == 0
data_structures
def test_circular_linked_list() -> None: circular_linked_list = CircularLinkedList() assert len(circular_linked_list) == 0 assert circular_linked_list.is_empty() is True assert str(circular_linked_list) == "" try: circular_linked_list.delete_front() raise AssertionError() # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_tail() raise AssertionError() # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_nth(-1) raise AssertionError() except IndexError: assert True try: circular_linked_list.delete_nth(0) raise AssertionError() except IndexError: assert True assert circular_linked_list.is_empty() is True for i in range(5): assert len(circular_linked_list) == i circular_linked_list.insert_nth(i, i + 1) assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6)) circular_linked_list.insert_tail(6) assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 7)) circular_linked_list.insert_head(0) assert str(circular_linked_list) == "->".join(str(i) for i in range(0, 7)) assert circular_linked_list.delete_front() == 0 assert circular_linked_list.delete_tail() == 6 assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6)) assert circular_linked_list.delete_nth(2) == 3 circular_linked_list.insert_nth(2, 3) assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6)) assert circular_linked_list.is_empty() is False
data_structures
def __init__(self, data: Any): self.data = data self.next = None
data_structures
def __init__(self): self.head = None
data_structures
def print_list(self): temp = self.head while temp is not None: print(temp.data, end=" ") temp = temp.next print()
data_structures
def push(self, new_data: Any): new_node = Node(new_data) new_node.next = self.head self.head = new_node
data_structures
def swap_nodes(self, node_data_1, node_data_2): if node_data_1 == node_data_2: return else: node_1 = self.head while node_1 is not None and node_1.data != node_data_1: node_1 = node_1.next node_2 = self.head while node_2 is not None and node_2.data != node_data_2: node_2 = node_2.next if node_1 is None or node_2 is None: return node_1.data, node_2.data = node_2.data, node_1.data
data_structures
def __init__(self, data: int, previous=None, next_node=None): self.data = data self.previous = previous self.next = next_node
data_structures
def __str__(self) -> str: return f"{self.data}"
data_structures
def get_data(self) -> int: return self.data
data_structures
def get_next(self): return self.next
data_structures
def get_previous(self): return self.previous
data_structures
def __init__(self, head): self.current = head
data_structures
def __iter__(self): return self
data_structures
def __next__(self): if not self.current: raise StopIteration else: value = self.current.get_data() self.current = self.current.get_next() return value
data_structures
def __init__(self): self.head = None # First node in list self.tail = None # Last node in list
data_structures
def __str__(self): current = self.head nodes = [] while current is not None: nodes.append(current.get_data()) current = current.get_next() return " ".join(str(node) for node in nodes)
data_structures
def __contains__(self, value: int): current = self.head while current: if current.get_data() == value: return True current = current.get_next() return False
data_structures
def __iter__(self): return LinkedListIterator(self.head)
data_structures
def get_head_data(self): if self.head: return self.head.get_data() return None
data_structures
def get_tail_data(self): if self.tail: return self.tail.get_data() return None
data_structures
def set_head(self, node: Node) -> None: if self.head is None: self.head = node self.tail = node else: self.insert_before_node(self.head, node)
data_structures
def set_tail(self, node: Node) -> None: if self.head is None: self.set_head(node) else: self.insert_after_node(self.tail, node)
data_structures
def insert(self, value: int) -> None: node = Node(value) if self.head is None: self.set_head(node) else: self.set_tail(node)
data_structures
def insert_before_node(self, node: Node, node_to_insert: Node) -> None: node_to_insert.next = node node_to_insert.previous = node.previous if node.get_previous() is None: self.head = node_to_insert else: node.previous.next = node_to_insert node.previous = node_to_insert
data_structures
def insert_after_node(self, node: Node, node_to_insert: Node) -> None: node_to_insert.previous = node node_to_insert.next = node.next if node.get_next() is None: self.tail = node_to_insert else: node.next.previous = node_to_insert node.next = node_to_insert
data_structures
def insert_at_position(self, position: int, value: int) -> None: current_position = 1 new_node = Node(value) node = self.head while node: if current_position == position: self.insert_before_node(node, new_node) return current_position += 1 node = node.next self.insert_after_node(self.tail, new_node)
data_structures
def get_node(self, item: int) -> Node: node = self.head while node: if node.get_data() == item: return node node = node.get_next() raise Exception("Node not found")
data_structures
def delete_value(self, value): if (node := self.get_node(value)) is not None: if node == self.head: self.head = self.head.get_next() if node == self.tail: self.tail = self.tail.get_previous() self.remove_node_pointers(node)
data_structures
def remove_node_pointers(node: Node) -> None: if node.get_next(): node.next.previous = node.previous if node.get_previous(): node.previous.next = node.next node.next = None node.previous = None
data_structures
def is_empty(self): return self.head is None
data_structures
def create_linked_list() -> None:
data_structures
def __init__(self, data): self.data = data self.previous = None self.next = None
data_structures
def __str__(self): return f"{self.data}"
data_structures
def __init__(self): self.head = None self.tail = None
data_structures
def __iter__(self): node = self.head while node: yield node.data node = node.next
data_structures
def __str__(self): return "->".join([str(item) for item in self])
data_structures
def __len__(self): return len(tuple(iter(self)))
data_structures
def insert_at_head(self, data): self.insert_at_nth(0, data)
data_structures
def insert_at_tail(self, data): self.insert_at_nth(len(self), data)
data_structures
def insert_at_nth(self, index: int, data): if not 0 <= index <= len(self): raise IndexError("list index out of range") new_node = Node(data) if self.head is None: self.head = self.tail = new_node elif index == 0: self.head.previous = new_node new_node.next = self.head self.head = new_node elif index == len(self): self.tail.next = new_node new_node.previous = self.tail self.tail = new_node else: temp = self.head for _ in range(0, index): temp = temp.next temp.previous.next = new_node new_node.previous = temp.previous new_node.next = temp temp.previous = new_node
data_structures
def delete_head(self): return self.delete_at_nth(0)
data_structures
def delete_tail(self): return self.delete_at_nth(len(self) - 1)
data_structures
def delete_at_nth(self, index: int): if not 0 <= index <= len(self) - 1: raise IndexError("list index out of range") delete_node = self.head # default first node if len(self) == 1: self.head = self.tail = None elif index == 0: self.head = self.head.next self.head.previous = None elif index == len(self) - 1: delete_node = self.tail self.tail = self.tail.previous self.tail.next = None else: temp = self.head for _ in range(0, index): temp = temp.next delete_node = temp temp.next.previous = temp.previous temp.previous.next = temp.next return delete_node.data
data_structures
def delete(self, data) -> str: current = self.head while current.data != data: # Find the position to delete if current.next: current = current.next else: # We have reached the end an no value matches raise ValueError("No data matching given value") if current == self.head: self.delete_head() elif current == self.tail: self.delete_tail() else: # Before: 1 <--> 2(current) <--> 3 current.previous.next = current.next # 1 --> 3 current.next.previous = current.previous # 1 <--> 3 return data
data_structures
def is_empty(self): return len(self) == 0
data_structures
def test_doubly_linked_list() -> None: linked_list = DoublyLinkedList() assert linked_list.is_empty() is True assert str(linked_list) == "" try: linked_list.delete_head() raise AssertionError() # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError() # This should not happen. except IndexError: assert True # This should happen. for i in range(10): assert len(linked_list) == i linked_list.insert_at_nth(i, i + 1) assert str(linked_list) == "->".join(str(i) for i in range(1, 11)) linked_list.insert_at_head(0) linked_list.insert_at_tail(11) assert str(linked_list) == "->".join(str(i) for i in range(0, 12)) assert linked_list.delete_head() == 0 assert linked_list.delete_at_nth(9) == 10 assert linked_list.delete_tail() == 11 assert len(linked_list) == 9 assert str(linked_list) == "->".join(str(i) for i in range(1, 10))
data_structures
def __init__(self, data=None): self.data = data self.next = None
data_structures
def __repr__(self):
data_structures
def __init__(self, data: Any) -> None: self.data: Any = data self.next_node: Node | None = None
data_structures
def __iter__(self): node = self visited = [] while node: if node in visited: raise ContainsLoopError visited.append(node) yield node.data node = node.next_node
data_structures
def has_loop(self) -> bool: try: list(self) return False except ContainsLoopError: return True
data_structures
def __init__(self, data: Any): self.data = data self.next = None
data_structures
def __repr__(self) -> str: return f"Node({self.data})"
data_structures
def __init__(self): self.head = None
data_structures
def __iter__(self) -> Any: node = self.head while node: yield node.data node = node.next
data_structures
def __len__(self) -> int: return len(tuple(iter(self)))
data_structures
def __repr__(self) -> str: return "->".join([str(item) for item in self])
data_structures
def __getitem__(self, index: int) -> Any: if not 0 <= index < len(self): raise ValueError("list index out of range.") for i, node in enumerate(self): if i == index: return node return None
data_structures
def __setitem__(self, index: int, data: Any) -> None: if not 0 <= index < len(self): raise ValueError("list index out of range.") current = self.head for _ in range(index): current = current.next current.data = data
data_structures