Skip to content

Instantly share code, notes, and snippets.

@dongho-jung
dongho-jung / sp_simulator.py
Created April 18, 2024 11:02
aws sp simulator
def get_df_sp(sp_uncovered_map, sp_committed):
sp_result = []
for hour, rows in sp_uncovered_map.items():
remaining_sp_committed = sp_committed
for row in rows:
# 현재 usage_type에서 최대로 커버할 수 있는 amount -> ? * sp_rate = remaining_sp_committed 가 되는 ? 또는 해당 usage_type amount의 최댓값
# 즉 min(remaining_sp_committed/sp_rate, usage_amount)가 현재 usage_type이 쓸수있는 최대 amount
covered_amount = min(remaining_sp_committed / row['sp_rate'], row['usage_amount'])
uncovered_amount = max(0, row['usage_amount'] - covered_amount)
covered_cost = covered_amount * row['sp_rate']
@dongho-jung
dongho-jung / gcd.py
Created April 27, 2020 15:22
GCD algorithm using Euclid Algorithm
def gcd(a, b):
while b: a, b = b, a % b
return a
import itertools
def get_primes(n):
for i in itertools.chain([2], itertools.count(3, 2)):
if n <= 1: return
while n%i == 0:
n /= i
yield i
@dongho-jung
dongho-jung / partition.py
Created April 22, 2020 03:01
python partition a list to k groups
def partition(list_, k):
"""
partition([1, 2, 3, 4], 2) -> [[1], [2, 3, 4]], [[1, 2], [3, 4]], ..., [[1, 3], [2, 4]], [[3], [1, 2, 4]]
"""
if k == 1: # base case 1: k == 1, just yield itself as a list
yield [list_]
elif k == len(list_): # base case 2: k == len(list_), yield each item in the list wrapped in a list
yield [[s] for s in list_]
else:
head, *tail = list_ # head = the first element, tail = the rest
@dongho-jung
dongho-jung / aws_elb_log_parser.py
Last active March 24, 2020 03:52
parse aws elb log
import gzip
import re
import pandas as pd
pd.options.display.max_columns = None
def gen_parse_elb_log(file):
prog = re.compile('(?P<type>\S+) (?P<timestamp>\S+) (?P<elb>\S+) (?P<client_port>\S+) '
@dongho-jung
dongho-jung / bash_snippets
Last active February 5, 2020 03:04
bash snippets
# find the ifindex values and each veth names
find /sys/class/net/veth* | xargs -I % echo 'cut -z -d"/" -f5 <<< % | tr "\n" " "; cat %/ifindex' | bash
# find the iflink of the container. it should be run in the container
cat /sys/class/net/eth0/iflink
# or if you want to traverse all the containers use this
for container in `docker ps -q`; do docker inspect --format='{{.Name}}' $container | tr '\n' ' '; docker exec -it $container cat /sys/class/net/eth0/iflink; done
# you can find which veth interface is mapped to which container
@dongho-jung
dongho-jung / list_memory_usage_notebook.py
Last active October 31, 2019 13:10
list memory usages of each notebook
import json
import re
import pandas as pd
import psutil
import requests
def get_sessions(url, password_or_token):
sess = requests.Session()
@dongho-jung
dongho-jung / crypt_arithmetic_solver.py
Last active August 22, 2019 12:58
this solver finds solutions for a cryptarithmetic problem that also called alphametics or verbal arithmetic
import itertools
class CryptArithmeticSolver:
"""CryptArithmeticSolver solves cryptarithmetic problems.
(Also called alphametics or Verbal arithmetic)
To use:
>>> cas = CryptArithmeticSolver()
>>> cas.set_problem('''
A
@dongho-jung
dongho-jung / l_system_get.py
Created August 6, 2019 08:31
this function spew out next L-system result
def l_system_gen(init, rule):
x = init
table = str.maketrans(rule)
while True:
yield x
x = x.translate(table)
l_system = l_system_gen('AB', {'A':'AB', 'B': 'A'})
print(next(l_system))
@dongho-jung
dongho-jung / BOJ_1114_통나무자르기.py
Last active July 29, 2019 03:47
Kyle thank you good
# https://www.topcoder.com/community/competitive-programming/tutorials/binary-search
# to get least x for which predicate(x) is true
def binary_search(low, high, predicate):
while low < high:
mid = low + (high - low) // 2
if predicate(mid): high = mid
else: low = mid+1
return low
# get a predicate for verifying the max length