Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View jackhftang's full-sized avatar

Jack Tang jackhftang

View GitHub Profile
@jackhftang
jackhftang / permutations.py
Created April 6, 2022 06:56
single yield, fast inner loop version
def permutations(lis):
N = len(lis)
i = 0
c = [*range(0, max(2,N+1))]
while True:
yield lis
i = 1
while c[i] == 0:
c[i] = i
i += 1
@jackhftang
jackhftang / http_client_pool.nim
Last active May 12, 2023 17:58
Nim HttpClientPool
import asyncdispatch
import deques
import options
import httpclient
import strformat
export httpclient # for AsyncResponse
#[
Wrapping AsyncHttpClient for pooling and timeout, also see
you are a zoo keeper, You wanna keep animals to be healthy (not overweight) so you apply a diet plan.
Problem:
An animal has intial weight S at the start, For every day, you can choose to lose weight (a[i]) or have food (b[i]) or , but you have to make sure every day animal doesnt exceed T (target weight)
for N days plan, how many possbile combinations can have
condition
・1 ≦ N ≦ 35
・1 ≦ Ai, Bi ≦ 1,000,000,000 (1 ≦ i ≦ N)
・(sum of A_i) < S ≦ T ≦ 1,000,000,000
sample in
2 10 20
@jackhftang
jackhftang / fletcher.nim
Created October 25, 2019 13:35
fletcher
proc fletcher16*(arr: openArray[uint8]): uint16 =
var
c1: int = 0
c2: int = 0
for d in arr:
c1 += d.int
c2 += c1
result = (((c2 mod 255) shl 8) + (c1 mod 255)).uint16
@jackhftang
jackhftang / index.php
Last active April 26, 2021 07:59
LDAP PHP Change Password Page (modified). Original can be found at https://gist.github.com/mattrude/657334
<?php
/**
* LDAP PHP Change Password Webpage
*
* Copyright (C) 2010 Matt Rude <http://mattrude.com>
* Copyright (C) 2019 Jack Tang <me@jackhftang.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@jackhftang
jackhftang / generateStringMap.php
Last active April 9, 2019 04:27
Generate a string map in goalng. [WTFPL](http://www.wtfpl.net/txt/copying/)
package util
import (
"time"
)
<?php
$className = "StringMap";
$arr = [
"Bool" => "bool",
There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbour). Each day, for each cell, if its neighbours are both active or both inactive, the cell becomes inactive the next day,. otherwise itbecomes active the next day.
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumsed to be always inactive. Even after updating the cell state. consider its pervious state for updating the state of other cells. Update the cell informationof allcells simultaneously.
Write a fuction cellCompete which takes takes one 8 element array of integers cells representing the current state of 8 cells and one integer days representing te number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell.
@jackhftang
jackhftang / tweetnacl.c
Last active February 8, 2019 02:21
A memory-reduced version of scalarmult(), marginally be able to run on arduino uno (2K bytes memroy). Original [tweetnacl](https://tweetnacl.cr.yp.to/20140427/tweetnacl.c)
#define FOR(i, n) for (i = 0;i < n;++i)
#define sv static void
typedef unsigned char u8;
typedef unsigned long u32;
typedef unsigned long long u64;
typedef long long i64;
typedef i64 gf[16]; // 1024-bit = 128 bytes
static const gf _121665 = {0xDB41, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
@jackhftang
jackhftang / lamport_timestamp.js
Last active January 20, 2019 04:37
Verification on causuality provided by Lamport timestamp
const cluster = require('cluster')
const N = 8
if(cluster.isMaster){
let workers = {}
let globals = []
for(let i=0; i<N; i++) workers[i] = cluster.fork({id: i});
cluster.on('message', (worker, {message, timestamp, to}) => {
globals.push({timestamp, message})
globals.sort(function(x,y){
const cluster = require('cluster');
const minions = ['alpha', 'beta'];
const tHeartBeat = 1000
const tCheck = 1000
const tTimeout = 3000
if (cluster.isMaster) {
for (let id of minions) cluster.fork({id});
let lastHeartbeat = {}