Compare commits

..

No commits in common. "a8d82b180a48ea9f8d4c2336be21dbf6167a6ec8" and "3f369e1e70d79379357584f4c1c5568acfc69f44" have entirely different histories.

5 changed files with 1 additions and 128 deletions

1
leetcode/.gitignore vendored
View File

@ -1 +0,0 @@
.envrc

View File

@ -25,8 +25,7 @@ classifiers = [
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"ipython",
"ipdb"
"ipython"
]
[project.urls]

View File

@ -1,9 +0,0 @@
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

View File

@ -1,7 +1,5 @@
import typing
import stdlib
def yep(s: str) -> bool:
return s.strip().lower().startswith("y")
@ -160,42 +158,3 @@ class MinStack:
def getMin(self) -> int: # no qa
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, key=lambda v: v[0]):
if cur is None:
cur = ret = node
continue
cur.next = node
cur = cur.next
if cur is not None:
cur.next = None
return ret

View File

@ -1,7 +1,6 @@
import pytest
import stuff
import stdlib
@pytest.mark.parametrize(
@ -78,77 +77,3 @@ def test_min_stack(ops: list[tuple[str] | tuple[str, int]], expected: list[int |
returned.append(getattr(inst, method)(arg))
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)))
),
),
(
stdlib.ListNode(
4,
stdlib.ListNode(
19,
stdlib.ListNode(
14,
stdlib.ListNode(
5,
stdlib.ListNode(
-3,
stdlib.ListNode(
1,
stdlib.ListNode(
8,
stdlib.ListNode(
5, stdlib.ListNode(11, stdlib.ListNode(15))
),
),
),
),
),
),
),
),
stdlib.ListNode(
-3,
stdlib.ListNode(
1,
stdlib.ListNode(
4,
stdlib.ListNode(
5,
stdlib.ListNode(
5,
stdlib.ListNode(
8,
stdlib.ListNode(
11,
stdlib.ListNode(
14, stdlib.ListNode(15, stdlib.ListNode(19))
),
),
),
),
),
),
),
),
),
],
)
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)