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 / 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]
@codecakes
codecakes / sorting.py
Last active March 3, 2024 08:53
basic sorting algorithms
from numbers import Number
def selection_sort(arr: list[Number]) -> list[Number]:
n = len(arr)
swap_index = 0
for start_idx, start_num in enumerate(arr):
min_num = float('inf')
for idx in range(start_idx, n):
num = arr[idx]
@codecakes
codecakes / calc_epigram.py
Created May 9, 2023 21:53
Calculate the difference between sum of all consonant chars and sum of all vowel chars
# ==================================== #
VOWELS = ['a', 'e', 'i', 'o', 'u', "A", "E", "I", "O", "U"]
def calc_epigram(sentence: str) -> int:
return sum(ord(char) if char not in VOWELS else -ord(char) for char in filter(lambda x: x.isalpha(), sentence))
assert (calc_epigram("why and how")) == 569
@codecakes
codecakes / count_palindrome_num.py
Created May 9, 2023 21:52
Count palindrome numbers within a range
# ================ Palindrome ==================== #
def is_num_palindrome(num: int) -> bool:
num = abs(num)
while num > 9:
exponent = math.floor(math.log10(num))
msb, remainder = divmod(num, 10 ** exponent)
num, lsb = divmod(remainder, 10)
if msb != lsb:
return False
@codecakes
codecakes / int_to_roman_literal.py
Created May 9, 2023 21:51
Integer conversion to roman literal
import math
# ================ Int to Roman ==================== #
RomanMapping = {
1: 'I',
4: 'IV',
5: 'V',
9: 'IX',
10: 'X',
40: 'XL',
@codecakes
codecakes / minimum_window_substring.py
Last active September 27, 2022 20:51
minimum window substring
"""
Given two strings s and t of lengths m and n respectively,
return the minimum window substring of s such that every character in t (including duplicates)
is included in the window.
If there is no such substring, return the empty string "".
A substring is a contiguous sequence of characters within the string.
This is fucking hard.
@codecakes
codecakes / anagram_indices_string.py
Created September 27, 2022 13:37
Find All Anagrams in a String
"""Given two strings s and p, return an array of all the start indices of p's anagrams in s.
You may return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase,
typically using all the original letters exactly once.
"""
from collections import Counter