Skip to content

Instantly share code, notes, and snippets.

@nhatbui
nhatbui / count_unique.py
Last active November 20, 2015 05:55
Count Unique from Pandas DataFrame
import pandas as pd
d = {
'a': ['1','1','2','2','2','3'],
'b': ['green', 'blue','yellow','yellow','blue','green']
}
df = pd.DataFrame.from_dict(d)
df_count = pd.crosstab(df['a'], df['b'])
@nhatbui
nhatbui / hierarchy.py
Created November 24, 2015 09:55
Pointer-Based n-ary Tree to Graph
class Hierarchy:
def __init__(self):
self.hierarchy = {}
def add_node(self, id):
self.hierarchy[id] = { "children": [], "parent": None }
def add_edge(self, from_id, to_id):
if from_id not in self.hierarchy:
self.add_node(from_id)
@nhatbui
nhatbui / nba-teams.txt
Last active January 9, 2016 09:33 — forked from haldun/nba-teams.txt
NBA team list in plain text
Atlanta Hawks
Boston Celtics
Brooklyn Nets
Charlotte Hornets
Chicago Bulls
Cleveland Cavaliers
Dallas Mavericks
Denver Nuggets
Detroit Pistons
Golden State Warriors
@nhatbui
nhatbui / filter_punc.py
Last active June 2, 2016 10:34
Filters rows based on exactly one punctuation occurance or none.
#!/usr/bin/python
import sys
import csv
import re
# The data in at least one of the fields (the body field) can include newline
# characters, and all the fields are enclosed in double quotes. Therefore, we
# will need to process the data file in a way other than using split(","). To do this,
# we have provided sample code for using the csv module of Python. Each 'line'
# will be a list that contains each field in sequential order.
@nhatbui
nhatbui / top10.py
Last active June 2, 2016 10:41
finds top 10 longest line[4]'s using quickselect
#!/usr/bin/python
import sys
import csv
import random
# Finds top 10 longest line[4]'s using quickselect sorted in ascending order
def partition(a, l, r, p, key=None):
pivot_value = key(a[p])
temp = a[p]
@nhatbui
nhatbui / get_all_tweets.py
Last active September 25, 2016 20:25
Get all historical Tweets from user(s) starting from now.
import argparse
import datetime
from TwitterAPI import TwitterAPI, TwitterOAuth, TwitterRequestError
twitter_handles = ['Twitter']
# The seed_set is a queue containing API requests to be performed.
seed_set = []
api_endpoint = 'statuses/user_timeline'
for handle in twitter_handles:

Keybase proof

I hereby claim:

  • I am nhatbui on github.
  • I am nbui (https://keybase.io/nbui) on keybase.
  • I have a public key ASA_K8kZ1CUaGNhVEsHepSJ5_P7HW3GYD_tSkgJjPAw5gQo

To claim this, I am signing this object:

COMPILER=clang++
CPP_FLAGS=-D ASIO_STANDALONE -std=c++11 -stdlib=libc++
INCLUDES=-I/path/to/asio-1.10.6/include
all: client server
client: chat_client.cpp
$(COMPILER) $(CPP_FLAGS) $(INCLUDES) -o $@ $<
server: chat_server.cpp
@nhatbui
nhatbui / list.c
Created September 25, 2017 06:31
Linked List implementation in C. Iteration in only one direction. Optimized for appending. (nothing special, just for copy pasta)
// One element of the list
// You should replace the "data" field
// with a type of your choice.
struct Node
{
struct Node * next;
/* Replace this with the data of your choice */
char * data;
};
@nhatbui
nhatbui / heartbeat.go
Last active October 21, 2017 17:04
A toy heartbeat server in Go. Each client, upon loading, registers itself and thereafter, sends heartbeats every X seconds. The server checks every Y seconds if a heartbeat has been received.
package main
import (
"fmt"
"log"
"net/http"
"sync"
"time"
)