WiP: solution for neetcode task
This commit is contained in:
parent
217b953b70
commit
6b3fa864f0
4 changed files with 176 additions and 0 deletions
74
leetcode/linked_lists/0206_reverse_linked_list.py
Normal file
74
leetcode/linked_lists/0206_reverse_linked_list.py
Normal file
|
@ -0,0 +1,74 @@
|
|||
from typing import Optional
|
||||
import pytest
|
||||
# Definition for singly-linked list.
|
||||
|
||||
|
||||
class ListNode:
|
||||
def __init__(self, val=0, next=None):
|
||||
self.val = val
|
||||
self.next = next
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Val: {self.val}"
|
||||
|
||||
|
||||
# Not solved by me
|
||||
class Solution:
|
||||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
|
||||
rl = []
|
||||
if not head:
|
||||
return
|
||||
|
||||
def rr(cn: ListNode):
|
||||
if cn.next:
|
||||
rr(cn.next)
|
||||
cn.next.next = cn
|
||||
rl.append(cn)
|
||||
else:
|
||||
rl.append(cn)
|
||||
|
||||
rr(head)
|
||||
head.next = None
|
||||
|
||||
class SolutionFromLeetCode:
|
||||
def reverseList(self, head: ListNode) -> ListNode:
|
||||
# Initialize pointers
|
||||
prev = None # Previous node starts as None
|
||||
curr = head # Current node starts at the head
|
||||
|
||||
# Traverse the list
|
||||
while curr is not None:
|
||||
next_node = curr.next # Save the next node
|
||||
|
||||
curr.next = prev # Reverse the link
|
||||
|
||||
# Move pointers forward
|
||||
prev = curr # Move prev to the current node
|
||||
curr = next_node # Move curr to the next node
|
||||
|
||||
# prev is now the new head of the reversed list
|
||||
return prev
|
||||
|
||||
|
||||
def test_solution():
|
||||
l5 = ListNode(val=5, next=None)
|
||||
l4 = ListNode(val=4, next=l5)
|
||||
l3 = ListNode(val=3, next=l4)
|
||||
l2 = ListNode(val=2, next=l3)
|
||||
l1 = ListNode(val=1, next=l2)
|
||||
sol = SolutionFromLeetCode()
|
||||
sol.reverseList(head=l1)
|
||||
assert l1.next is None
|
||||
assert l2.next == l1
|
||||
assert l3.next == l2
|
||||
assert l4.next == l3
|
||||
assert l5.next == l4
|
||||
|
||||
|
||||
def test_solution_1():
|
||||
l2 = ListNode(val=2, next=None)
|
||||
l1 = ListNode(val=1, next=l2)
|
||||
sol = SolutionFromLeetCode()
|
||||
sol.reverseList(head=l1)
|
||||
assert l1.next is None
|
||||
assert l2.next == l1
|
49
neetcode/arrays/encode_decode_strings.py
Normal file
49
neetcode/arrays/encode_decode_strings.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
##################### Section ##############################
|
||||
# Section topic: Arrays
|
||||
#
|
||||
# Design an algorithm to encode a list of strings to a single string. The encoded string is then decoded back to the original list of strings.
|
||||
#
|
||||
# Please implement encode and decode
|
||||
#
|
||||
# Example 1:
|
||||
#
|
||||
# Input: ["neet","code","love","you"]
|
||||
#
|
||||
# Output:["neet","code","love","you"]
|
||||
# Example 2:
|
||||
#
|
||||
# Input: ["we","say",":","yes"]
|
||||
#
|
||||
# Output: ["we","say",":","yes"]
|
||||
# Constraints:
|
||||
#
|
||||
# 0 <= strs.length < 100
|
||||
# 0 <= strs[i].length < 200
|
||||
# strs[i] contains only UTF-8 characters.
|
||||
#
|
||||
############################################################
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def encode(self, strs: List[str]) -> str:
|
||||
delim: str = "-+-"
|
||||
res: str = ""
|
||||
counts: str = ""
|
||||
encoded: str = ""
|
||||
for elem in strs:
|
||||
counts += f"{len(elem)}{delim}"
|
||||
encoded += elem
|
||||
counts += delim
|
||||
res = counts + encoded
|
||||
return encoded
|
||||
|
||||
|
||||
def decode(self, s: str) -> List[str]:
|
||||
pass
|
||||
|
||||
|
||||
s = Solution()
|
||||
|
||||
print(s.encode(strs=["we", "say", ":", "yes"]))
|
||||
# s.decode("we say: yes")
|
0
neetcode/arrays/encode_decode_strings/README.md
Normal file
0
neetcode/arrays/encode_decode_strings/README.md
Normal file
|
@ -0,0 +1,53 @@
|
|||
##################### Section ##############################
|
||||
# Section topic: Arrays
|
||||
#
|
||||
# Design an algorithm to encode a list of strings to a single string. The encoded string is then decoded back to the original list of strings.
|
||||
#
|
||||
# Please implement encode and decode
|
||||
#
|
||||
# Example 1:
|
||||
#
|
||||
# Input: ["neet","code","love","you"]
|
||||
#
|
||||
# Output:["neet","code","love","you"]
|
||||
# Example 2:
|
||||
#
|
||||
# Input: ["we","say",":","yes"]
|
||||
#
|
||||
# Output: ["we","say",":","yes"]
|
||||
# Constraints:
|
||||
#
|
||||
# 0 <= strs.length < 100
|
||||
# 0 <= strs[i].length < 200
|
||||
# strs[i] contains only UTF-8 characters.
|
||||
#
|
||||
############################################################
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
delim: str = "-+-"
|
||||
|
||||
def encode(self, strs: List[str]) -> str:
|
||||
delim: str = self.delim
|
||||
res: str = ""
|
||||
counts: str = ""
|
||||
encoded: str = ""
|
||||
for elem in strs:
|
||||
counts += f"{len(elem)}{delim}"
|
||||
encoded += elem
|
||||
counts += delim
|
||||
res = counts + encoded
|
||||
return res
|
||||
|
||||
def decode(self, s: str) -> List[str]:
|
||||
splitted = s.split(f"{self.delim}{self.delim}")
|
||||
breaks = [int(s) for s in splitted[0].split(self.delim)]
|
||||
res = []
|
||||
return breaks
|
||||
|
||||
|
||||
s = Solution()
|
||||
|
||||
en = s.encode(strs=["we", "say", ":", "yes"])
|
||||
print(s.decode(en))
|
Loading…
Reference in a new issue