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 / is_palindrome_integer.py
Created September 7, 2021 14:16
is it a palindrome integer
import math
class Solution:
def isPalindrome(self, x):
'''
:type x: int
:rtype: bool
'''
if x < 0:
@codecakes
codecakes / optimal_wait_time.py
Last active September 5, 2021 22:17
Optimal Queue Arrangement - less waiting time
# you can sort one time queue like below
# you can sort dynamic queue using priority queue - for larger n
def waiting_time(**n: dict):
"""Optimal queue wait time.
>>> waiting_time(t1=15, t2=20, t3=10)
35
"""
waiting_time = 0
wait_list = {}
@codecakes
codecakes / threadbounded_executor.py
Last active May 28, 2021 11:31
ThreadPoolBatchedExecutor
# See: https://stackoverflow.com/a/55235583
import concurrent.futures as futures
import queue
from threading import BoundedSemaphore
class ThreadPoolBatchedExecutor(futures.ThreadPoolExecutor):
def __init__(self, limit, *args, **kwargs):
super().__init__(*args, **kwargs)
@codecakes
codecakes / card_kotpees.py
Created March 11, 2021 21:11
Domain driven functional implementation of a card game
"""Kotpees Card Game."""
import enum
import random
from typing import NamedTuple, Tuple, Union, Optional
class Person(str):
...
@codecakes
codecakes / docker-mongo-repair
Created March 8, 2021 05:31 — forked from mtrunkat/docker-mongo-repair
Run "mongo --repair" in Docker container that cannot start because of MongoDB error
#!/bin/bash
# See https://github.com/docker-library/mongo/pull/63
docker run --rm --volumes-from my-mongo-server mongo unlink "/data/db/mongod.lock"
docker run --rm --volumes-from my-mongo-server mongo --repair
@codecakes
codecakes / non_repeat_substring.py
Created December 25, 2020 06:02
Longest substring without any repeating characters
def non_repeat_substring(string):
from collections import Counter
max_ctr = Counter()
left_idx = 0
distinct = 0
max_distinct = float('-inf')
for right_idx, r_char in enumerate(string):
if not max_ctr[r_char]:
distinct += 1
max_ctr[r_char] += 1
@codecakes
codecakes / k_distinct_occurences.py
Created December 25, 2020 05:44
Find K maximum occurrences in unique K baskets until more than K distinct occurrences
from collections import Counter
def fruits_into_baskets(fruits):
max_ctr = Counter()
left_idx = 0
distinct = 0
max_k = 2
ct = float('-inf')
for right_idx, r_char in enumerate(fruits):
if not max_ctr[r_char]:
@codecakes
codecakes / longest_substring_with_k_distinct.py
Created December 19, 2020 07:29
longest substring with no more than K distinct characters
from collections import defaultdict
def longest_substring_with_k_distinct(chars, k):
left_idx = 0
distinct_ct = 0
max_ln = -1
counter = defaultdict(int)
for right_idx, char in enumerate(chars):
# print("max_ln={}".format(max_ln))
# print(
@codecakes
codecakes / smallest_subarray_sum.py
Last active December 18, 2020 10:21
find the length of the smallest contiguous subarray whose sum is greater than or equal to S
def smallest_subarray_with_given_sum(s, arr):
min_ct = float('inf')
t_head = head_idx = 0
running_sum = 0
t_sum = 0
for tail_idx, num in enumerate(arr):
running_sum += num
while running_sum >= s:
min_ct = min(min_ct, tail_idx - head_idx+1)
running_sum -= arr[head_idx]
@codecakes
codecakes / max_sub_array_of_size_k.py
Created December 17, 2020 08:38
max sub array sum
def max_sub_array_of_size_k(k, arr):
max_sum = -1
running_sum = start_idx = 0
for tail_idx, num in enumerate(arr):
running_sum += num
if tail_idx - start_idx == k:
running_sum -= arr[start_idx]
start_idx += 1
max_sum = (running_sum and max(max_sum, running_sum)) or max_sum
return max_sum