Skip to content

Instantly share code, notes, and snippets.

View phamtm's full-sized avatar
🏠
Working from home

Minh Pham phamtm

🏠
Working from home
View GitHub Profile
@phamtm
phamtm / sterntree.cpp
Last active September 17, 2015 10:12
#include <iostream>
#include <algorithm>
using namespace std;
/* print the fractions of denominator <= n between n1/d1 and n2/d2 */
void genfrac(int n, int n1, int d1, int n2, int d2) {
if (d1 + d2 > n) return;
genfrac(n, n1, d1, n1 + n2, d1 + d2);
cout << n1 + n2 << "/" << d1 + d2 << "\n";
genfrac(n, n1 + n2, d1 + d2, n2, d2);
# Bash colors #
###############
txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
@phamtm
phamtm / SudokuSolver.java
Last active September 17, 2015 10:08
A solver for Sudoku
public class SudokuSolver {
int[] board = new int[81];
char[] in = null;
boolean[][] findLimits() {
boolean[][] ok = new boolean[81][9];
for (int i = 0; i < 81; i++) {
for (int j = 0; j < 9; j++) {
ok[i][j] = true;
}
# -*- coding: utf-8 -*-
import codecs
import cPickle as pickle
import os
import re
import string
PUNCTUATIONS = string.punctuation + u'“' + u'”' + u'…'
PATTERN_D_REPLACE = re.compile(r'Đ', flags=re.UNICODE)
@phamtm
phamtm / cassandra-3.3.sh
Created April 4, 2016 03:48
cassandra-docker-start.sh
!#/bin/bash
# 7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp
docker run --name c33 -p 7000:7000 -p 7001:7001 -p 7199:7199 -p 9042:9042 -p 9160:9160 -d cassandra:3.3
@phamtm
phamtm / heapperm.py
Last active May 30, 2016 06:49
Permutation generation methods
def swap(ar, i, j):
tmp = ar[i]
ar[i] = ar[j]
ar[j] = tmp
def genheapperm(ar, n):
if n == 1:
print ar
return
@phamtm
phamtm / implementation-notes.md
Last active January 2, 2017 09:14
Promise - Simple implementation

Promise implementation

  • Only the original promise (created with new) can control when it will resolve/reject (callee decide)

  • Each subsequent promise (subscriber) created with then will either resolve when the onFulfilled and onRejected return OR if no handler resolve with the same value as the parent

  • A promise should NEVER be resolved with another promise.

Who wait for whom?

@phamtm
phamtm / v0_no_free_no_realloc.c
Last active January 2, 2017 09:19
malloc implementation
#include <sys/type.h>
#include <unistd.h>
void* malloc(size_t size) {
void *p;
p = sbrk(0); // Initially, `break` is positioned at start of heap
if (sbrk(size) == (void*) -1)
return NULL;
@phamtm
phamtm / ToPostfix.java
Created January 23, 2017 02:00
Regular expression matcher
package mp;
import java.util.ArrayDeque;
import java.util.Deque;
public class ToPostfix {
/**
* Don't handle error
* missing: escape char