Skip to content

Instantly share code, notes, and snippets.

@nishidy
nishidy / amazon_api_test.py
Last active June 18, 2017 13:48
Amazon Product API
import os
import time
from amazon.api import AmazonAPI
AMAZON_ACCESS_KEY = os.environ['AWSAccessKeyId']
AMAZON_SECRET_KEY = os.environ['AWSSecretKey']
AMAZON_ASSOC_TAG = os.environ['AWSAssocId']
print(AMAZON_ACCESS_KEY[-5:])
print(AMAZON_SECRET_KEY[-5:])
@nishidy
nishidy / find_common.py
Last active May 15, 2017 12:52
Find common list in three lists
from random import random
import time
import sys
def findCommon_NNN(A,B,C):
if 1000<len(A) or 1000<len(B) or 1000<len(C):
print("# This may be huge input for this function with O(N^3). Exit.")
return []
@nishidy
nishidy / consistent_hashing.cpp
Last active May 7, 2017 15:30
Consistent Hashing in C++
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <map>
#include <math.h>
//#include <list> # lower_bound does not work
#include <openssl/sha.h>
#include <assert.h>
@nishidy
nishidy / quick_sort.py
Last active April 8, 2022 01:24
Quick Sort in Python
class Sorter:
def __init__(self,data):
self.orig = data.copy()
self.data = data
def do_qsort(self):
print("Original data",self.orig)
self.run_qsort(0,len(self.data)-1)
print("Sorted data ",self.data)
@nishidy
nishidy / merge_sort.py
Last active May 2, 2017 23:26
Merge Sort in Python
class Sorter:
def __init__(self,data):
self.data = data
def do_msort(self):
print("Original data",self.data)
self.run_msort(0,len(self.data)-1)
print("Sorted data ",self.data)
def run_msort(self,i,j):
@nishidy
nishidy / lcs.py
Last active April 23, 2017 13:45
Longest Common Subsequence
class LCD:
def __init__(self,s1,s2):
self.s1 = s1
self.s2 = s2
print(s1,s2)
def solve(self):
a = [ [ 0 for y in range(len(self.s2)+1) ] for x in range(len(self.s1)+1) ]
for x in range(len(self.s1)):
for y in range(len(self.s2)):
@nishidy
nishidy / asta.py
Last active March 15, 2017 13:12
Variable length variable / Keyword variable length variable
def func(a,b=10,*c,**d):
print(a,b,c,d)
func(1,20,30,40)
func([1,20,30,40])
func({'a':1,'b':20,'c':30,'d':40})
func(*[1,20,30,40])
func(**{'a':1,'b':20,'c':30,'d':40})
a = {1:10,2:20}
@nishidy
nishidy / bucket.c
Last active January 14, 2017 03:06
Bucket sort
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <sys/time.h>
int cmp(const void* a, const void* b){
if(*(double*)a<*(double*)b) return -1;
if(*(double*)a>*(double*)b) return 1;
return 0;
@nishidy
nishidy / sais_orig.c
Last active January 9, 2017 14:31
Working implementation with main function of SA-IS algorithm for building Suffix Array in C from the paper.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
unsigned char mask[]={0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
#define tget(i) ( (t[(i)/8]&mask[(i)%8]) ? 1 : 0 )
#define tset(i, b) t[(i)/8]=(b)\
?(mask[(i)%8]|t[(i)/8])\
@nishidy
nishidy / multi_key_quick_sort.c
Last active January 1, 2017 07:29
Multi-key quick sort in place for strings
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <sys/time.h>
#define STRLEN 10
#define STRLENL 1000