Skip to content

Instantly share code, notes, and snippets.

@hyunjun
hyunjun / latency.txt
Last active May 11, 2017 15:00 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
memory L1 cache reference 0.5 ns
memory Branch mispredict 5 ns
memory L2 cache reference 7 ns 14x L1 cache
memory Mutex lock/unlock 25 ns
memory Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
network Send 1K bytes over 1 Gbps network 10,000 ns 10 us
disk Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@hyunjun
hyunjun / lru_cache.py
Last active August 29, 2015 14:23
lru_cache
# http://www.quora.com/Isnt-dynamic-programming-much-simpler-coding-in-languages-like-Python-and-others-rather-than-Java-C-C++
# https://docs.python.org/3/library/functools.html
$ python3
Python 3.4.2 (default, Oct 19 2014, 17:52:17)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from functools import lru_cache
>>> @lru_cache(maxsize=None)
... def fib(n):
... if n < 2:
@hyunjun
hyunjun / reload.py
Created February 26, 2015 04:53
imp#reload
$ cat a.py
class A(object):
def __init__(self):
pass
$ python
Python 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Aug 21 2014, 18:22:21)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
@hyunjun
hyunjun / Solution.java
Created December 22, 2014 05:08
[hackerrank] Manasa and Stones
mport java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
public static String numOfStones(final int n, final int a, final int b) {
int small = a;
int big = b;
if ( b < a) {
small = b;
@hyunjun
hyunjun / FillingJar.java
Created December 18, 2014 00:56
[hacker rank] Filling Jars
mport java.math.BigInteger;
import java.util.Scanner;
public class Solution {
public static void main(final String[] args) {
Scanner sc = new Scanner(System.in);
String[] input = sc.nextLine().split(" ");
final int numOfJars = Integer.parseInt(input[0]);
final int numOfOps = Integer.parseInt(input[1]);
BigInteger totalNumOfCandies = new BigInteger("0");
4 5
10101
11100
11010
00101
@hyunjun
hyunjun / canBuildPalindrome.java
Created December 16, 2014 07:31
[hackerrank] game of throne - I
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Solution {
public static String canBuildPalindrome(final String s) {
Map<Character, Integer> m = new HashMap<Character, Integer>();
int odd_cnt = 0;
for ( int i = 0; i < s.length(); ++i ) {
@hyunjun
hyunjun / DistributionOfCandies.java
Created December 16, 2014 04:25
[hackerrank] angry children
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Solution {
public static int distributionOfCandies(final int K, final List<Integer> packets) {
int min = Integer.MAX_VALUE;
Collections.sort(packets);
@hyunjun
hyunjun / numOfReduction.java
Created December 12, 2014 01:26
number of reduction to make palindrome
import java.util.Scanner;
public class Solution {
public static int numOfReduction(final String s) {
int cnt = 0;
int l = 0;
int r = s.length() - 1;
while ( l < r ) {
int lNum = s.charAt(l);
@hyunjun
hyunjun / NumOfDeletion.java
Created December 11, 2014 02:32
[hackerrank] alternating characters
import java.util.Scanner;
public class Test {
public static int numOfDeletion(final String s) {
int cnt = 0;
char prev = s.charAt(0);
for ( int i = 1; i < s.length(); ++i ) {
char c = s.charAt(i);
if ( prev == c )