Skip to content

Instantly share code, notes, and snippets.

View codecakes's full-sized avatar
💭
I may be slow to respond.

codecakes codecakes

💭
I may be slow to respond.
View GitHub Profile
@codecakes
codecakes / remove_duplicates_sorted_arr.py
Created July 22, 2024 10:19
Remove Duplicates from Sorted Array in place
# naive solution
class Solution:
POS_NUM = 1000
def removeDuplicates(self, nums: List[int]) -> int:
start = 0
n = len(nums)
end = n - 1
for start in iter(lambda: start, end):
if start > end:
break
@codecakes
codecakes / natorize.py
Created July 4, 2024 06:41
speak in nato phonetic tongue
NATO_BAG = {
'A': 'Alpha',
'B': 'Bravo',
'C': 'Charlie',
'D': 'Delta',
'E': 'Echo',
'F': 'Foxtrot',
'G': 'Golf',
'H': 'Hotel',
'I': 'India',
@codecakes
codecakes / level_order_traversal.ex
Created June 27, 2024 13:32
level order traversal
# Definition for a binary tree node.
#
# defmodule TreeNode do
# @type t :: %__MODULE__{
# val: integer,
# left: TreeNode.t() | nil,
# right: TreeNode.t() | nil
# }
# defstruct val: 0, left: nil, right: nil
# end
@codecakes
codecakes / subset_combination.py
Created June 9, 2024 10:47
array nums of unique integers, return all possible combination of subsets of nums.
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
def do_generate(result, slate, subset):
result += [slate]
for idx, num in enumerate(subset):
new_subset = subset[idx+1:]
do_generate(result, slate + [num], new_subset)
return result
return do_generate([], [], nums)
@codecakes
codecakes / recursive_combination.py
Created June 8, 2024 10:06
N choose K combinations
def find_combinations(n, k):
"""
Args:
n(int32)
k(int32)
Returns:
list_list_int32
"""
array = list(range(1,n+1))
result = set()
@codecakes
codecakes / fib_linear_recursion.py
Created June 4, 2024 10:26
A linear recursive fibonacci series
def fib(n):
# print(f"for fib {n}")
a, b = 0, 1
if n == a: return a
if n == b: return b
return do_fib(n-1, b, a+b)
def do_fib(n, /, a=0, b=1):
if n == 0: return a
@codecakes
codecakes / best_practices.md
Last active April 15, 2024 22:50
some best practices
  • Pattern match.
  • Keep consistent level of abstraction per function.
  • Don't mix pure functions with IO functions. Segregate.
  • Build upon ubiquituous domain language.
  • Define Interfaces for service layer around infrastructure and application.
  • Partial function composition == Dependency Injection. But it is not a preferred functional pattern.
  • Don't YAGNI
  • If you find yourself passing parameters and adding conditional..the abstraction is incorrect.
  • Build upon these foundational low-level architectural design patterns:
  • Value objects, Business Domains, Service Layers
@codecakes
codecakes / designing_ha_systems.md
Created April 5, 2024 15:11
How to design Horizontally scalable distributed systems?

HA principles: CAP done right

  • Design in isolation (Partition)
    • 2 business modules must communicate over message pattern. They are not considered business modules if one requires the other, in which case they don’t need to communicate over messages if a business process has to die, all interconnected business modules must die
    • process are stateless
    • fault tolerant implies scalability implies concurrency and concurrent business processes
  • Availability
    • its better to die on fault and restart, then drag and build up the pile of error
  • when a process dies, fault detection and communicating fault is important
@codecakes
codecakes / to_hex.ex
Created April 4, 2024 15:50
Convert to Hex and back
defmodule ToHex do
@hex_base 16
def to_hex(0), do: 0
def to_hex(num) when is_number(num) do
div(num, @hex_base)
|> IO.inspect
|> then(& {to_hex(&1), rem(num, @hex_base)})
@codecakes
codecakes / input.py
Created March 12, 2024 16:44 — forked from nfx/input.py
x = [1,2,3]
if len(x) < 3:
return x
else:
return [4,5,6]