Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Minh Pham phamtm

🏠
Working from home
View GitHub Profile
@phamtm
phamtm / ToPostfix.java
Created Jan 23, 2017
Regular expression matcher
View ToPostfix.java
package mp;
import java.util.ArrayDeque;
import java.util.Deque;
public class ToPostfix {
/**
* Don't handle error
* missing: escape char
@phamtm
phamtm / v0_no_free_no_realloc.c
Last active Jan 2, 2017
malloc implementation
View v0_no_free_no_realloc.c
#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 / implementation-notes.md
Last active Jan 2, 2017
Promise - Simple implementation
View implementation-notes.md

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 / heapperm.py
Last active May 30, 2016
Permutation generation methods
View heapperm.py
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 / cassandra-3.3.sh
Created Apr 4, 2016
cassandra-docker-start.sh
View cassandra-3.3.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
View preprocess.py
# -*- 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 / SudokuSolver.java
Last active Sep 17, 2015
A solver for Sudoku
View SudokuSolver.java
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;
}
View .bashrc
# 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
View sterntree.cpp
#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);
You can’t perform that action at this time.