Skip to content

Instantly share code, notes, and snippets.

from typing import List
def reverse_preserve(input_str: List[str]) -> None:
index0 = 0
index1 = len(input_str) - 1
while index0 < index1:
left = input_str[index0]
right = input_str[index1]
if left.isalpha() and right.isalpha():
user0 = ["/start", "/pink", "/register", "/orange", "/red", "a"]
user1 = ["/start", "/green", "/blue", "/pink", "/register", "/orange", "/one/two"]
user2 = ["a", "/one", "/two"]
user3 = ["/pink", "/orange", "/yellow", "/plum", "/blue", "/tan", "/red", "/amber", "/HotRodPink", "/CornflowerBlue", "/LightGoldenRodYellow", "/BritishRacingGreen"]
user4 = ["/pink", "/orange", "/amber", "/BritishRacingGreen", "/plum", "/blue", "/tan", "/red", "/lavender", "/HotRodPink", "/CornflowerBlue", "/LightGoldenRodYellow"]
user5 = ["a"]
@pyrofolium
pyrofolium / triplets.py
Last active May 8, 2020 20:13
triplets.py
"""
Given a log file of user_id, timestamp & page visited find the 10 most common triplets, where a triplet is an occurrence of 3 pages visited sequentially by the same user.
1, 1, /home
1, 2, /shipments
1, 3, /labels
1, 4 /something
1, 5 /something
2, 4, /home
...
def find_min3(x):
# print(x)
if len(x) == 1 or x[0] < x[-1]:
return x[0]
elif x[0] == x[-1]:
return find_min3(x[1:]) #eliminate the duplicate
split_index = len(x)//2
left = x[:split_index]
right = x[split_index:]
def topKFrequent(nums: List[int], k: int) -> List[int]:
freq_by_num = {}
max_freq = float("-inf")
for num in nums:
freq_by_num[num] = freq_by_num[num] + 1 if num in freq_by_num else 1
max_freq = max(max_freq, freq_by_num[num])
nums_by_freq = {}
for num, freq in freq_by_num.items():
nums_by_freq[freq] = nums_by_freq[freq] + [num] if freq in nums_by_freq else [num]
# [[A, B]] where A dependes on B -> [B, A] execution order
def traverse(x: List[Tuple[int, int]]) -> Optional[List[int]]:
if len(x) == 0:
return []
# any value that is on the left has a dependency and is not a leaf
not_leaves = {l: True for (l, r) in x if r is not None}
# every other value that is not in (not_leaves) must be a leaf.
leaves = {i: True for j in x for i in j if i not in not_leaves and i is not None}
def traverse(x: List[Tuple[int, int]]) -> Optional[List[int]]:
if len(x) == 0:
return []
not_leaves = {l: True for (l, r) in x if r is not None}
leaves = {i: True for j in x for i in j if i not in not_leaves and i is not None}
new_x = [(l, None if r in leaves else r) for (l, r) in x if l in not_leaves]
recursion = traverse(new_x)
return [i for i in leaves] + recursion if len(new_x) < x and recursion is not None else None
unchangingData: int = 1
def addOne(x: int) -> int:
return x + 1
newData: int = addOne(unchangingData)
#in a matrix of integers find the length of the longest path of increaseing numbers
#https://leetcode.com/problems/longest-increasing-path-in-a-matrix/description/
class Solution(object):
def __init__(self):
self.memo = None
def is_bounded(self,i,j,matrix):
return not(i < 0 or j < 0 or i >= len(matrix) or j >= len(matrix[0]))
#find the length of the longest increasing path from a starting point.
@pyrofolium
pyrofolium / helloworldc
Last active December 16, 2017 16:06
haskell hello world asm output
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 13
.globl _main
.p2align 4, 0x90
_main: ## @main
.cfi_startproc
## BB#0:
pushq %rbp
Lcfi0:
.cfi_def_cfa_offset 16