Skip to content

Instantly share code, notes, and snippets.

View AaronFlower's full-sized avatar
💭
I may be slow to respond.

AaronFlower

💭
I may be slow to respond.
View GitHub Profile
@AaronFlower
AaronFlower / insertion-sort.js
Last active October 27, 2017 02:26
Insertion sort javascript version
function insertionSort(data)
{
for(let i = 1; i < data.length; ++i) {
let j = i
while(j > 0 && data[j] < data[j - 1]) {
let tmp = data[j]
data[j] = data[j - 1]
data[j - 1] = tmp
j --
}
@AaronFlower
AaronFlower / binary-search.js
Last active October 26, 2017 02:23
Binary Search
a = Array.from({length: 15}).map((_,i) => i)
function binarySearch(arr, needle)
{
let begin = 0, end = arr.length - 1
while (begin <= end) {
let mid = Math.floor((end - begin) / 2) + begin
if (arr[mid] === needle) {
return mid
} else if (arr[mid] > needle) {
@AaronFlower
AaronFlower / vector-rotation.py
Last active October 26, 2017 02:34
Programming Pearls: Column 2, vector-rotation
def vector_rotation(str, pos):
'''Transpose concept: (A.T B.T).T = (BA) '''
l_half = str[:pos][::-1]
r_half = str[pos:][::-1]
new_str = l_half + r_half
return new_str[::-1]
s = 'abcdefgh'
print(vector_rotation(s, 2))
print(vector_rotation(s, 3))
@AaronFlower
AaronFlower / insertion-sort.py
Created October 27, 2017 02:25
insertion-sort python.
def insertion_sort(data):
length = len(data)
for i in range(1, length):
j = i
while j > 0 and data[j] < data[j-1]:
tmp = data[j-1]
data[j - 1] = data[j]
data[j] = tmp
j -= 1
@AaronFlower
AaronFlower / quick-sort.hs
Created October 31, 2017 03:18
quick sort haskell version
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
@AaronFlower
AaronFlower / if-statement.sh
Last active November 9, 2017 05:17
bash shell if statement
if [ -f .bashrc ] ; then
echo "bashrc exists"
else
echo "bashrc does not exist"
fi
if [[ -r .bashrc ]]
then
echo ".bashrc exists and have permission granted."
fi
@AaronFlower
AaronFlower / mustache.js
Created November 13, 2017 07:38
a simple mustache js
str = 'hello {name}, say {msg}. {name} is a handsome boy, {age}!'
reg = /{([^{}]+)}/g
data = {
name: 'eason',
msg: 'hi'
}
str.replace(reg, function (_, key) {
return data[key] || `{${key}}`
})
// "hello eason, say hi. eason is a handsome boy, {age}!"
@AaronFlower
AaronFlower / escapeHtml.js
Created November 13, 2017 13:27
js escape html
/**
* 参考 https://github.com/janl/mustache.js/blob/b283da5c8c26dbf78c357a1f67bfed26d18f5e32/mustache.js#L60
*/
var entityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'/': '&#x2F;',
@AaronFlower
AaronFlower / c_cpp_properties.json
Created November 18, 2017 08:12
vs code mac c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": [
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
"/usr/local/include",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0/include",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include",
@AaronFlower
AaronFlower / adder-function.js
Created November 21, 2017 05:07
Javascript Function Object
const adder = new Function('a', 'b', 'return a + b')
adder(2, 3)