This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gcd(a, b): | |
while b: a, b = b, a % b | |
return a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+) ' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import re | |
import pandas as pd | |
import psutil | |
import requests | |
def get_sessions(url, password_or_token): | |
sess = requests.Session() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
class CryptArithmeticSolver: | |
"""CryptArithmeticSolver solves cryptarithmetic problems. | |
(Also called alphametics or Verbal arithmetic) | |
To use: | |
>>> cas = CryptArithmeticSolver() | |
>>> cas.set_problem(''' | |
A |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
NewerOlder