Skip to content

Instantly share code, notes, and snippets.

Avatar

Rewon Child rewonc

  • San Francisco, CA
View GitHub Profile
View gist:fee8174208d8d53e7028
// 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 Dec 9, 2014
IIFE in loop vs pure functional version
View gist:fee5ebfb2f83e3ba8dac
// 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 Dec 21, 2018
Counting array inversions in Javascript
View inversion.js
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 Apr 3, 2018
AngularJS/PhoneGap/Ionic: filter to convert links for opening in separate mobile window
View app.js
//*
//* 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 Sep 22, 2014
Checking whether parentheses balance each other in Scala
View balance.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 Sep 22, 2014
Pascal's triangle in scala
View pascal.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 Jan 23, 2017
Authenticate a user with angularfire, save to firebase, and make available to controllers with promise
View app.js
/*
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 Sep 18, 2014
Play Google Translate's text to speech audio directly on your rails webpage
View gist:0024ec71bb589be18bd7
#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))