Skip to content

Instantly share code, notes, and snippets.

View rewonc's full-sized avatar

Rewon Child rewonc

  • San Francisco, CA
View GitHub Profile
@rewonc
rewonc / gist:fee8174208d8d53e7028
Created June 18, 2015 22:53
Snail -- reverse array
// Return an array sorted in a "snail" format -- that is, around the edge and then the middle.
// var arr = [[1,2,3],
// [4,5,6],
// [7,8,9]];
// var arr1 = [[1]];
// var arr2 = [[1, 2], [3, 4]];
// Test.assertSimilar(snail(arr1), [1]);
@rewonc
rewonc / gist:fee5ebfb2f83e3ba8dac
Created December 9, 2014 23:14
IIFE in loop vs pure functional version
// imperative version
var i,
callLater = {};
for (i = 0; i < 3; i++) {
(function (val) {
callLater[val] = function () {
return "hello " + val;
};
}(i));
@rewonc
rewonc / inversion.js
Last active December 21, 2018 12:37
Counting array inversions in Javascript
function countInversions(array){
// Note: this uses a variant of merge sort
//input handlers
if (array === undefined) throw new Error("Array must be defined to count inversions");
if (array.length === 0 || array.length === 1) return 0;
var tally = 0; // count for inversions
sort(array); // merge sort the array and increment tally when there are crossovers
return tally;
@rewonc
rewonc / app.js
Last active October 26, 2021 00:16
AngularJS/PhoneGap/Ionic: filter to convert links for opening in separate mobile window
//*
//* See the JS Fiddle: http://jsfiddle.net/sxcjmoj5/3/
//*
//*
// make sure ngSanitize module is installed:
// https://docs.angularjs.org/api/ngSanitize
//
// To convert links from plaintext, you must use the linky filter which is included with ngSanitize
// https://docs.angularjs.org/api/ngSanitize/filter/linky
//
@rewonc
rewonc / balance.scala
Created September 22, 2014 15:59
Checking whether parentheses balance each other in Scala
def balance(chars: List[Char]): Boolean = {
def balIter(chars: List[Char], count: Int): Boolean = {
if (chars.isEmpty){
if (count == 0) true else false
} else{
if (chars.head.toString == ")") {
if (count < 1) false else balIter(chars.tail, count - 1)
} else if (chars.head.toString == "("){
balIter(chars.tail, count + 1)
} else {
@rewonc
rewonc / pascal.scala
Created September 22, 2014 15:58
Pascal's triangle in scala
def pascal(c: Int, r: Int): Int = {
if (c < 0 || r < 0 || c > r) throw new IllegalArgumentException("Column and row numbers must be 0 or greater. Column length must be lower than row length") else{
if (c == 0 || c == r) 1 else {
pascal(c - 1, r - 1) + pascal(c, r - 1)
}
}
}
@rewonc
rewonc / app.js
Last active January 23, 2017 19:59
Authenticate a user with angularfire, save to firebase, and make available to controllers with promise
/*
This file is a brief example of how to use angularfire to authenticate a user, save that user's public profile to firebase, then
ensure that both the authenticated and public user are available in your controllers.
The route configuration uses angular-ui-router: https://github.com/angular-ui/ui-router
*/
@rewonc
rewonc / gist:0024ec71bb589be18bd7
Created September 18, 2014 00:19
Play Google Translate's text to speech audio directly on your rails webpage
#This will allow you to serve google TTS audio from your own domain, allowing you to use it in the source for HTML5 audio tags.
#tts_controller.rb
class TtsController < ApplicationController
def pipe
queryString = params[:query_string] #.gsub(. . ., '')
lang = params[:lang] #Language code; see https://sites.google.com/site/tomihasa/google-language-codes
require 'net/http'
url = URI.parse('http://translate.google.com/translate_tts?ie=UTF-8&tl=' + lang + '&q=' + URI::encode(queryString))