Skip to content

Instantly share code, notes, and snippets.

@ChunMinChang
ChunMinChang / device_switch.scpt
Last active July 17, 2018 22:23
Switch output devices
(*
Run this script:
1. By terminal:
- Enable Terminal for Accessibility in Privacy section of
Security & Provacy in Setting App.
- Run `osascript <path_to_file>/<script_name>` in terminal
2. By Script Editor
- Enable Script Editor for Accessibility in Privacy section of
Security & Provacy in Setting App.
- Copy the following code in Script Editor and click Run
@ChunMinChang
ChunMinChang / main.rs
Created July 16, 2018 23:41
Borrowed pointers learning notes.
// Summary:
//
// immutable : read-only privilege
// mutable : read-write privilege
//
// 1. A read-only(immutable) vairable cannot be casted to a read-write(mutable) variable.
// 2. One variable can be read by multiple instances, no matter what its type is.
// 3. If one mutable variable is under writting, no other can read or write it.
// 4. If one variable is under reading, no other can write it.
//
@ChunMinChang
ChunMinChang / roadmap.md
Last active May 17, 2018 03:31
How to Become a Software Engineer
@ChunMinChang
ChunMinChang / binary_search_in_rotated_array.cpp
Last active February 7, 2018 01:44
Find an element in a increasing-order sorted array of n integers that has been rotated an unknown number of times. #searching #CtCI
#include <cassert>
#include <iostream>
// Utilities
// ============================================================================
#define SIZE_OF_ARRAY(x) (sizeof(x)/sizeof(x[0]))
void printArray(int a[], int length) {
for (int i = 0 ; i < length ; ++i) {
std::cout << a[i] << " ";
@ChunMinChang
ChunMinChang / avg.cpp
Last active January 31, 2018 07:13
Calculating average without sum #Math
// $ g++ avg.cpp --std=c++11
#include <iostream>
// double average = 0;
// uint64_t count = 0;
// void Add(uint64_t data)
// {
// average += (data - average) / ++count;
// }
@ChunMinChang
ChunMinChang / SortStack.cpp
Last active January 31, 2018 07:20
Sort the stack #stack #sorting #data_structure #leetcode
#include <cassert>
#include <iostream>
#include <stack>
// Goal:
// Sort `s` by decreasing order, from the bottom to the top.
// The minimal element will be at the top after sorting.
// Limit:
// You cannot use other data structures to implement it
// except an additional stack.
@ChunMinChang
ChunMinChang / HashTable.h
Last active January 31, 2018 07:21
Hash Table Implementation #data_structure #hashtable
#include <cassert> // for assert.
#include <cstdlib> // for calloc, free.
#include <functional> // for std::hash.
#include <memory> // for std::unique_ptr.
#define DEBUG 1 // Set 1 to log the debugging messages.
#define LOG(...) DEBUG && fprintf(stderr, __VA_ARGS__)
#if DEBUG
#include <iostream> // for std::cout, std::endl.
@ChunMinChang
ChunMinChang / AutoArray.h
Last active January 31, 2018 07:18
Resizable array implementation #data_structure #array
#include <cassert> // for assert.
#include <cstdlib> // for calloc, free.
#include <cstring> // for memcpy.
template<class T>
class AutoArray
{
public:
AutoArray(unsigned int aCapacity = 0)
: mCapacity(aCapacity)
@ChunMinChang
ChunMinChang / mincost.cpp
Last active February 5, 2018 05:29
Calculate the minimal cost path from the top-left cell to the bottom-right cell on a grid #recursion #dynamic_programming #DPfCI
// $ g++ mincost.cpp --std=c++11
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
#undef RECURSION
#undef MEMOIZATION
#undef DYNAMIC_PROGRAMMING
#undef RUN
@ChunMinChang
ChunMinChang / combination.cpp
Last active February 5, 2018 05:28
Calculate the number of combination of n things taken k at a time without repetition. #recursion #dynamic_programming #DPfCI
// Combination(n, k):
// Returns the number of combination of n things taken k
// at a time without repetition.
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
#undef RECURSION
#undef MEMOIZATION