Skip to content

Instantly share code, notes, and snippets.

View honghaoz's full-sized avatar
:shipit:
shipit

Honghao honghaoz

:shipit:
shipit
View GitHub Profile
@honghaoz
honghaoz / Continuation.swift
Last active November 25, 2022 15:30
Aggregated continuation, uses checked continuation version in DEBUG build and use unsafe version in RELEASE.
/// Aggregated continuation, uses `CheckedContinuation` for DEBUG build, uses `UnsafeContinuation` for RELEASE build.
public struct Continuation<T, E>: Sendable where E: Error {
#if DEBUG
public let continuation: CheckedContinuation<T, E>
public init(continuation: CheckedContinuation<T, E>) {
self.continuation = continuation
}
#else
@honghaoz
honghaoz / NSObject+Observation.swift
Created April 21, 2016 17:15
Swift KVO handy observation
//
// NSObject+Observation.swift
// Pods
//
// Created by Honghao Zhang on 2016-02-11.
//
//
import Foundation
@honghaoz
honghaoz / 0_reuse_code.js
Last active August 29, 2015 14:22
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@honghaoz
honghaoz / collection view perform batch update
Last active August 29, 2015 14:11
collection view perform batch update
// Compare old data model with new data model and perform batch update for collection view
/**
* Perform batch update on collection view, use old stories and new stories to make comparison
* groupId property of story is used for comparison
*
* PRE: make sure collection view's data model is update with newArray
*
* After animation completed, visible cells' view are updated
*
@honghaoz
honghaoz / isPrime.py
Last active August 29, 2015 14:06
isPrime
def isPrime(n):
'''check if integer n is a prime'''
# make sure n is a positive integer
n = abs(int(n))
# 0 and 1 are not primes
if n < 2:
return False
# 2 is the only even prime number
if n == 2:
return True
#!/usr/bin/env python
from random import randint
from copy import deepcopy
def find_mincut(n, e):
mincut = len(e)
N = 2000
for x in range(N):
nodes = deepcopy(n)
@honghaoz
honghaoz / LinkedList-RemoveDuplicates.cpp
Created June 6, 2014 01:57
Remove duplicates from an unsorted linked list
#include<iostream>
#include<set>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int dat) {
data = dat;
@honghaoz
honghaoz / LinkedList-Basics.cpp
Last active August 29, 2015 14:02
LinkedList Basic definitions and operations
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int dat) {
data = dat;
next = NULL;
@honghaoz
honghaoz / Sort-CountSort.cpp
Last active August 29, 2015 14:02
CountSort
#include<iostream>
using namespace std;
void countSort(int a[], int size, int k) {
int *b = (int *)malloc(sizeof(int) * k);
memset(b, 0, sizeof(int) * k);
for (int i = 0; i < size; i++) {
b[a[i]]++;
}
for (int i = 1; i < k; i++) {
@honghaoz
honghaoz / String-SamePermutation.cpp
Last active August 29, 2015 14:02
Check whether one string is a permutation of the other
include<set>
include<cstring>
using namespace std;
bool checkSame(string a, string b) {
if (a.length() != b.length()) {
return false;
}
map<char, int> testMap;
int length = (int)a.length();