Skip to content

Instantly share code, notes, and snippets.

@hsinewu
hsinewu / stack.cpp
Created November 26, 2018 07:19
Simple stack implementation in c++
#include <iostream>
// #include <stdexcept>
using namespace std;
template<typename T>
class Stack {
typedef struct Node {
T data;
struct Node* next;
} Node;
@hsinewu
hsinewu / eight-queen.cpp
Created November 22, 2018 02:37
eight queen backtracking c++
// #include <bits/stdc++>
#include <cstdio>
#include <cstdlib>
// using namespace std;
#define QUEEN 8
int cnt;
void printGrid(int grid[][QUEEN]) {
for(int i=0; i<QUEEN; i++) {
@hsinewu
hsinewu / docker-compose.yml
Created October 5, 2018 06:40
Nginx load balance with docker
version: "2"
services:
gate:
image: nginx:latest
ports:
- 80:80
volumes:
- ./load-balance.conf:/etc/nginx/conf.d/default.conf
web1:
@hsinewu
hsinewu / bst.hs
Last active April 21, 2018 06:19
Naive (unbalanced) binary search tree in haskell
data BinarySearchTree = Null | Node Int (BinarySearchTree) (BinarySearchTree)
deriving Show
insert Null val = Node val Null Null
insert (Node x left right) val
| x > val = Node x left' right
| otherwise = Node x left right'
where
left' = insert left val
right' = insert right val
@hsinewu
hsinewu / Query
Last active December 19, 2017 08:59
Solve 4x4 sudoku in prolog stupidly
?- sudoku44(A1,2,3,A4,B1,B2,B3,2,1,C2,C3,C4,D1,4,1,D4).
1 2 3 4
-------------
A | . 2 | 3 . |
B | . . | . 2 |
-------------
C | 1 . | . . |
D | . 4 | 1 . |
-------------
@hsinewu
hsinewu / kmp.cpp
Last active May 17, 2021 11:34
Knuth–Morris–Pratt algorithm in c++
#include <iostream>
#include <algorithm>
using namespace std;
int find_substring(string str, string pattern) {
// Step 0. Should not be empty string
if( str.size() == 0 || pattern.size() == 0)
return -1;
@hsinewu
hsinewu / Ints.go
Last active June 19, 2017 13:07
read integers from stdin to array
package main
import (
"fmt"
)
func ints(n int) ([]int) {
xs := make([]int, n)
for i := range xs {
fmt.Scan(&xs[i])
}
@hsinewu
hsinewu / lps.rb
Created June 15, 2017 07:17
Longest Palindromic Subsequence
def lps(i, j)
return $dp[i][j] if $dp[i][j]
return 1 if i==j
return 0 if i>j
if $a[i] == $a[j]
z = lps(i+1, j-1) + 2
else
z = [ lps(i+1, j), lps(i, j-1) ].max
end
$dp[i][j] = z
@hsinewu
hsinewu / manifest.json
Created February 6, 2017 08:38
webextension tab url
{
"manifest_version": 2,
"name": "Tab url",
"description": "Shows url of the current page",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
@hsinewu
hsinewu / unionFind.cpp
Created January 20, 2017 17:16
Data structure
int uf[SIZE];
void init() {
std::iota(uf, uf+SIZE, 0);
}
int root(int x) {
int _x = x;
while(x!=uf[x]) x = uf[x] /* = uf[uf[x]] */ ;
return uf[_x] = x;