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 / reverse_sentence.py
Created September 4, 2022 21:38
Frame words in a phrase in reverse order the non-idiomatic unpythonic way
# Online Python - IDE, Editor, Compiler, Interpreter
import string
EXCLUDE_CHARS = {each_invalid_char: "" for each_invalid_char in string.punctuation}
SAME_CHARS = {**{w:w for w in string.ascii_letters}, **EXCLUDE_CHARS}
EXCLUDE_CHAR_MAP = str.maketrans(SAME_CHARS)
def reverse_string(sentence: str):
@codecakes
codecakes / largest_array_sum.py
Created September 4, 2022 19:48
Largest Contiguous Sum of Array with positive and negative integers
def largest_array_sum(arr):
n = len(arr)
if n < 1:
return 0
if n < 2:
return arr[0]
max_sum = float("-inf")
current_sum = arr[0]
for num in arr[1:]:
current_sum = max(current_sum + num, num)
@codecakes
codecakes / PigLatin.kt
Created August 24, 2022 11:16
Translate English to Pig Latin speak
import java.util.*
import kotlin.reflect.KFunction0
/**
* Rule 1:
* If a word begins with a vowel sound, add an "ay" sound to the end of the word.
* Please note that "xr" and "yt" at the beginning of a word make vowel sounds
* (e.g. "xray" -> "xrayay", "yttria" -> "yttriaay").
*
* Rule 2:
* If a word begins with a consonant sound, move it to the end of the word and then add an "ay" sound
@codecakes
codecakes / context_swtiching.py
Created November 17, 2021 21:31
Context switching from one blocking statement to other and switching back to one until the other resumes.
import concurrent.futures as cf
import asyncio
from time import sleep
def two():
print('two')
sleep(4) # blocking
print('two again')
@codecakes
codecakes / min_count_dp.py
Last active November 5, 2021 14:00
Returns minimum count to reach target given necessary unlimited coins of that denomation.
import functools as ft
# See: https://github.com/python/cpython/blob/e2d65630f36712dbdbf7711520c985c526a5cc25/Lib/functools.py#L525
@ft.lru_cache
def min_count(L: list, target: int, sum_count = None) -> int:
"""Returns minimum count to reach target given necessary unlimited coins of that denomation."""
if not L: return -1
sum_count = sum_count or 0
if target < 0:
return float('inf')
@codecakes
codecakes / consumer_producers.py
Created October 26, 2021 22:36
anatomy of consumer producer
import itertools
def p1():
res = None
for x in itertools.cycle(range(5)):
yield x
res = yield
print(f"x={res}")
if res == -1:
print(f"ending term for x={x} res={res}")
@codecakes
codecakes / next_greater_element_circular.py
Created October 21, 2021 11:40
Next Greater Element Circular
def nextGreaterElement(array):
if not array: return []
n = len(array)
if n == 1:
return [-1]
stack = []
result = [-1 for _ in range(n)]
# fill next greater values - non circular, from stack
for long_idx in range(2*n):
idx = long_idx%n
@codecakes
codecakes / stack_sort.py
Created October 12, 2021 01:25
stack sort
def sort(stack, element):
if not stack or element > stack[-1]:
stack += [element]
else:
top_el = stack.pop()
sort(stack, element)
stack += [top_el]
return stack
def sortStack(stack):
@codecakes
codecakes / max_previous_number_array.py
Created October 11, 2021 16:12
Stack. Return an array in descending order with previous integer greater than current
"""https://www.algoexpert.io/questions/Sunset%20Views
Return an array in descending order given an array of positive non-zero integers either starting from left or right
"""
import functools
def sunsetViews(buildings, direction):
n = len(buildings)
def get_index(direction, n, idx):
@codecakes
codecakes / max_profit_diff_stock.py
Created October 7, 2021 01:22
Given a log of stock prices compute the maximum possible earning.
def solution(A):
# write your code in Python 3.6
max_diff = float('-inf')
cur_sum = 0
n = len(A)
for idx, num in zip(range(1, n), A[1:]):
diff = num - A[idx-1]
if cur_sum > 0:
cur_sum += diff
else: