Skip to content

Instantly share code, notes, and snippets.

View pwxcoo's full-sized avatar
🐟

Xiance Wu pwxcoo

🐟
View GitHub Profile
@pwxcoo
pwxcoo / quick_sort.hs
Created December 2, 2018 04:04
quick sort in Haskell
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let smallerSorted = quicksort [a | a <- xs, a <= x]
biggerSorted = quicksort [a | a <- xs, a > x]
in smallerSorted ++ [x] ++ biggerSorted
@pwxcoo
pwxcoo / fetch.js
Created October 6, 2018 06:08
Simple fetch() sample
fetch(`/api/following/`, {
method: "POST",
headers:{
'Content-Type': 'application/json'
},
body: JSON.stringify({
followerUsername: loginName,
followingUsername: nowname
})
})
@pwxcoo
pwxcoo / json.js
Created October 5, 2018 05:54
[jQuery]Convert form to json
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
@pwxcoo
pwxcoo / navbar.html
Last active October 2, 2018 09:22
A universal navbar template
<nav class="navbar navbar-expand-lg navbar-dark bg-dark ">
<a class="navbar-brand" href="#">Popular Quotes Collection</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
<ul class="navbar-nav">
<li class="nav-item ative">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
@pwxcoo
pwxcoo / NewPasswordUtil.java
Last active July 14, 2022 13:10
Generate a SALT in Java for Salted-Hash.
package com.pwxcoo.github.utils;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Random;
@pwxcoo
pwxcoo / double.cpp
Last active September 21, 2018 09:57
1. print double type value to binary/hexadecimal expression in c++ 2. double parser for converting binary to double in Python
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
@pwxcoo
pwxcoo / AtomicIntegerExample.java
Last active June 18, 2019 01:23
Five approaches to print "ABC" 10 times in sequence using 3 threads in Java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerExample {
private AtomicInteger sycValue = new AtomicInteger(0);
private static final int MAX_SYC_VALUE = 3 * 10;
@pwxcoo
pwxcoo / inorder.cpp
Last active September 12, 2018 10:02
Binary Tree Traversal of Iteration (non-recursion)
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> res;
auto now = root;
while(!st.empty() || now)
{
while(now)
{
st.push(now);
@pwxcoo
pwxcoo / cycle.cpp
Last active June 18, 2019 01:31
Find Cycle List Beginning Node
/**
* Time: O(n)
* Space: O(1)
* 1. find list if exists cycle.
* 2. 0 -> a(cycle s) -> b(cycle met) -> c(cycle s(e))
* slow = a + b + (b + c)n1, fast = a + b + (b + c)n2.
* slow * 2 = fast
* 2a + 2b + 2*n1*(b + c) = a + b + n2*(b + c) => a = (n2 - 2*n1) * (b + c) - b
* After meeting, so one start going from head and meanwhile slow start going, they will meet at cycle begin.
**/
@pwxcoo
pwxcoo / sqrt.py
Created September 12, 2018 03:57
Newton Iteration Sqrt
def newtonSqrt(n, eps=1e-8):
"""
- ASSUME
f(x) = x^2 - n
when f(x)=0, x will be the root of n.
- DEDUCE BY Taylor Series Formula
f(x) = f(x0) + (x - x0)f'(x0)
- WHEN f(x)=0
x = x0 - f(x0)/f'(x0)
x will be closer to the root than x0