Skip to content

Instantly share code, notes, and snippets.

View Per48edjes's full-sized avatar
👉
This is a good point.

Ravi Dayabhai Per48edjes

👉
This is a good point.
View GitHub Profile
@Per48edjes
Per48edjes / init.lua
Created September 13, 2023 20:10
Debugging HLS/Neovim/InsertLeave/FoldRange issue
-- Minimal nvim config with lazy
-- Assumes a directory in $NVIM_DATA_MINIMAL
-- Start with
--
-- export NVIM_DATA_MINIMAL=$(mktemp -d)
-- export NVIM_APP_NAME="nvim-ht-minimal"
-- nvim -u minimal.lua
--
-- Then exit out of neovim and start again.
@Per48edjes
Per48edjes / minheap_pq.py
Last active May 19, 2023 03:17
Python implementation of MinHeap priority queue
from typing import Optional
class MinHeap:
"""
A MinHeap is a complete binary tree where the value of each node is less than or equal
to the value of its children. (The root node is the minimum element in the heap.)
This implementation supports the following operations:
@Per48edjes
Per48edjes / randomized_quickselect.py
Last active April 18, 2023 03:03
Leetcode 215. Kth Largest Element in Array
# This is the same thing as finding the (len(nums) - (k-1))th order statistic.
# - Note: We need to transform "Kth Largest, 1-indexed" into "Kth Smallest, 0-indexed"
# Idea: Use randomized quickselect! It has O(n) expected time
import random
class Solution:
# O(n) time, O(1) space
@Per48edjes
Per48edjes / randomized_quicksort.py
Created April 14, 2023 20:17
Leetcode 912. Sort an Array (Randomized QuickSort)
# Let's implement Randomized QuickSort!
import random
class Solution:
# O(n)
def partition(self, nums: List[int], l: int, r: int) -> int:
# Choose random pivot, put it first in the array
random_idx = random.choice(range(l, r + 1))
nums[l], nums[random_idx] = nums[random_idx], nums[l]
@Per48edjes
Per48edjes / cis194_hw1.hs
Created March 8, 2023 21:47
UPenn CIS 194 HW 1
main :: IO ()
main = undefined
toDigitsRev :: Integer -> [Integer]
toDigitsRev n
| n <= 0 = []
| otherwise = (n `mod` 10) : toDigitsRev (n `div` 10)
@Per48edjes
Per48edjes / linked_list_cycle_detection.py
Created November 30, 2022 02:32
LeetCode 141. Linked List Cycle
from typing import Optional
class ListNode:
def __init__(self, val, next=None):
self.val = val
self.next = next
def hasCycle(head: Optional[ListNode]) -> bool:
@Per48edjes
Per48edjes / reverse_linked_list.py
Created November 29, 2022 19:51
LeetCode 206. Reverse Linked List
from typing import Optional
"""
LeetCode 206. Reverse Linked List
"""
class ListNode:
def __init__(self, val, next=None):
@Per48edjes
Per48edjes / rotated_sorted_array.py
Last active November 30, 2022 19:24
Count number of times sorted array has been rotated
def num_rotations(nums: list) -> int:
"""
Returns the number of times a sorted array has been rotated to the right
with the numbers wrapping to the beginning when they are rotated at the end
of the list
>>> nums = [3, 4, 5, 1, 2]
>>> num_rotations(nums)
3
>>> nums2 = [1, 2, 3, 4, 5]
@Per48edjes
Per48edjes / happy_number.py
Created November 29, 2022 03:03
Leetcode 202. Happy Number
def isHappy(n: int) -> bool:
"""
Leetcode 202. Happy number
>>> isHappy(7)
True
>>> isHappy(19)
True
>>> isHappy(2)
False
@Per48edjes
Per48edjes / count_array_inversions.py
Created November 26, 2022 21:48
Implement 'MergeSort + Count Inversions' algorithm on a 1-D array
#!/usr/bin/env python3
def merge_count_split_inversions(left_sorted: list, right_sorted: list, lst_len: int) -> tuple:
"""
Perform merge of two sorted lists and count inversions while merging
>>> merge_count_split_inversions([2], [1], 2)
([1, 2], 1)
>>> merge_count_split_inversions([1, 3, 5], [2, 4, 6], 6)