Repository navigation
Manchester | 26-SDC-Jun | Fithi Teklom | Sprint 2 | Implement Skip List #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
|
|
||
| import random | ||
|
|
||
| class Node: | ||
| __slots__ = ("value", "forward") | ||
|
|
||
| def __init__(self, value: int, level: int) -> None: | ||
| self.value = value | ||
| self.forward = [None] * (level + 1) | ||
|
|
||
|
|
||
| class SkipList: | ||
| def __init__(self) -> None: | ||
| self.max_level = 4 | ||
| self.level = 0 | ||
| self.head = Node(0, self.max_level) | ||
|
|
||
| def _random_level(self) -> int: | ||
| level = 0 | ||
|
|
||
| while level < self.max_level and random.random() < 0.5: | ||
| level += 1 | ||
|
|
||
| return level | ||
| def insert(self, value: int) -> None: | ||
|
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a blank line to separate two logically independent blocks of code could improve readability. |
||
| update = [self.head] * (self.max_level + 1) | ||
| current = self.head | ||
|
|
||
| for level in range(self.level, -1, -1): | ||
| while current.forward[level] is not None: | ||
| if current.forward[level].value < value: | ||
| current = current.forward[level] | ||
| else: | ||
| break | ||
|
Comment on lines
+30
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: Code on line 30-34 could be simplified into a while-loop without using if-else. The same also applies to the while loop in |
||
|
|
||
| update[level] = current | ||
|
|
||
| node_level = self._random_level() | ||
| new_node = Node(value, node_level) | ||
|
|
||
| if node_level > self.level: | ||
| for i in range(self.level + 1, node_level + 1): | ||
| update[i] = self.head | ||
| self.level = node_level | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code on line 44 does not seem to depend on the loop. |
||
|
|
||
| for i in range(node_level + 1): | ||
| new_node.forward[i] = update[i].forward[i] | ||
| update[i].forward[i] = new_node | ||
|
|
||
| def contains(self, value: int | str) -> bool: | ||
| current = self.head | ||
|
|
||
| for level in range(self.level, -1, -1): | ||
| while current.forward[level] is not None: | ||
| if current.forward[level].value < value: | ||
| current = current.forward[level] | ||
| else: | ||
| break | ||
|
|
||
| current = current.forward[0] | ||
|
|
||
| return current is not None and current.value == value | ||
|
|
||
| def __contains__(self, value: int | str) -> bool: | ||
| return self.contains(value) | ||
|
|
||
| def to_list(self) -> list[int | str]: | ||
| result = [] | ||
| current = self.head.forward[0] | ||
|
|
||
| while current is not None: | ||
| result.append(current.value) | ||
| current = current.forward[0] | ||
|
|
||
| return result | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why restrict the value type to
int?Have you performed static type checking on the python script?