Linked list sorting (?)
This commit is contained in:
parent
3f369e1e70
commit
337a795ad0
1
leetcode/.gitignore
vendored
Normal file
1
leetcode/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.envrc
|
@ -25,7 +25,8 @@ classifiers = [
|
|||||||
"Programming Language :: Python :: Implementation :: PyPy",
|
"Programming Language :: Python :: Implementation :: PyPy",
|
||||||
]
|
]
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ipython"
|
"ipython",
|
||||||
|
"ipdb"
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.urls]
|
[project.urls]
|
||||||
|
9
leetcode/stdlib.py
Normal file
9
leetcode/stdlib.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import typing
|
||||||
|
|
||||||
|
|
||||||
|
class ListNode:
|
||||||
|
"""ListNode is the leetcode "standard library" type used in linked lists"""
|
||||||
|
|
||||||
|
def __init__(self, val=0, next: typing.Optional["ListNode"] = None): # no qa
|
||||||
|
self.val = val
|
||||||
|
self.next = next
|
@ -1,5 +1,7 @@
|
|||||||
import typing
|
import typing
|
||||||
|
|
||||||
|
import stdlib
|
||||||
|
|
||||||
|
|
||||||
def yep(s: str) -> bool:
|
def yep(s: str) -> bool:
|
||||||
return s.strip().lower().startswith("y")
|
return s.strip().lower().startswith("y")
|
||||||
@ -158,3 +160,40 @@ class MinStack:
|
|||||||
|
|
||||||
def getMin(self) -> int: # no qa
|
def getMin(self) -> int: # no qa
|
||||||
return self._min[-1]
|
return self._min[-1]
|
||||||
|
|
||||||
|
|
||||||
|
def linked_list_to_list(head: stdlib.ListNode | None) -> list[int]:
|
||||||
|
seen: set[int] = set()
|
||||||
|
ret: list[int] = []
|
||||||
|
|
||||||
|
while head is not None:
|
||||||
|
if hash(head) in seen:
|
||||||
|
return ret
|
||||||
|
|
||||||
|
seen.add(hash(head))
|
||||||
|
ret.append(head.val)
|
||||||
|
head = head.next
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def sort_linked_list(head: stdlib.ListNode | None) -> stdlib.ListNode | None:
|
||||||
|
by_val: list[tuple[int, stdlib.ListNode]] = []
|
||||||
|
ret: stdlib.ListNode | None = None
|
||||||
|
|
||||||
|
while head is not None:
|
||||||
|
by_val.append((head.val, head))
|
||||||
|
head = head.next
|
||||||
|
|
||||||
|
cur = ret
|
||||||
|
|
||||||
|
for _, node in sorted(by_val):
|
||||||
|
if cur is None:
|
||||||
|
cur = ret = stdlib.ListNode(node.val)
|
||||||
|
continue
|
||||||
|
|
||||||
|
next_node = stdlib.ListNode(node.val)
|
||||||
|
cur.next = next_node
|
||||||
|
cur = next_node
|
||||||
|
|
||||||
|
return ret
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import stuff
|
import stuff
|
||||||
|
import stdlib
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -77,3 +78,27 @@ def test_min_stack(ops: list[tuple[str] | tuple[str, int]], expected: list[int |
|
|||||||
returned.append(getattr(inst, method)(arg))
|
returned.append(getattr(inst, method)(arg))
|
||||||
|
|
||||||
assert returned == expected
|
assert returned == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("head", "expected"),
|
||||||
|
[
|
||||||
|
(None, None),
|
||||||
|
(
|
||||||
|
stdlib.ListNode(
|
||||||
|
4, stdlib.ListNode(2, stdlib.ListNode(1, stdlib.ListNode(3)))
|
||||||
|
),
|
||||||
|
stdlib.ListNode(
|
||||||
|
1, stdlib.ListNode(2, stdlib.ListNode(3, stdlib.ListNode(4)))
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_sort_linked_list(head: stdlib.ListNode | None, expected: stdlib.ListNode | None):
|
||||||
|
if head is None:
|
||||||
|
assert stuff.sort_linked_list(head) == expected
|
||||||
|
return
|
||||||
|
|
||||||
|
assert stuff.linked_list_to_list(
|
||||||
|
stuff.sort_linked_list(head)
|
||||||
|
) == stuff.linked_list_to_list(expected)
|
||||||
|
Loading…
Reference in New Issue
Block a user