Skip to content

Instantly share code, notes, and snippets.

View DarrenN's full-sized avatar
🌵
(on-a vision-quest)

Darren DarrenN

🌵
(on-a vision-quest)
View GitHub Profile
@DarrenN
DarrenN / array.js
Created November 16, 2012 13:58
Get even numbers from array using recursion instead of a loop, and without mutating the original array
Array.prototype.evens = function() {
var arr = this;
var out = [];
var count = 0;
var even = function(arr, out) {
if (count <= arr.length - 1) {
if ((arr[count] % 2) === 0) {
out.push(arr[count]);
}
count++;
@DarrenN
DarrenN / quicksilver_score.php
Created July 16, 2012 20:13 — forked from xeoncross/quicksilver_score.php
Quicksilver string ranking algorithm in PHP
<?
// Quicksilver Score
//
// A port of the Quicksilver string ranking algorithm
// (re-ported from Javascript to PHP by Kenzie Campbell)
// http://route19.com/logbook/view/quicksilver-score-in-php
//
// score("hello world","axl") //=> 0.0
// score("hello world","ow") //=> 0.6
@DarrenN
DarrenN / shortid.rb
Created November 20, 2011 18:02
Short unique ID (Ruby)
t = DateTime
id = t.now.strftime("%Y%m%d%k%M%S%L") # Get current date to the milliseconds
id = id.to_i.to_s(36) # will generate somthing like "5i0sp1h4tkc"
# Reverse it
id.to_i(36)
@DarrenN
DarrenN / stopwords.php
Created October 30, 2011 00:01
Naively pluck keywords from text
<?php
function stopWords($text, $stopwords) {
// Remove line breaks and spaces from stopwords
$stopwords = array_map(function($x){return trim(strtolower($x));}, $stopwords);
// Replace all non-word chars with comma
$pattern = '/[0-9\W]/';
$text = preg_replace($pattern, ',', $text);
@DarrenN
DarrenN / errors.sh
Created February 5, 2011 13:33
Vagrant file from Getting started
[default] Running chef-solo...
[default] stdin: is not a tty
: stderr
[default] [Sat, 05 Feb 2011 05:32:39 -0800] INFO: Setting the run_list to ["recipe[vagrant_main]"] from JSON
[Sat, 05 Feb 2011 05:32:39 -0800] INFO: Starting Chef Run (Version 0.9.12)
: stdout
[default] [Sat, 05 Feb 2011 05:32:47 -0800] INFO: Ran execute[apt-get update] successfully
[Sat, 05 Feb 2011 05:32:48 -0800] FATAL: No cookbook found in ["/tmp/vagrant-chef/cookbooks-0", "/tmp/vagrant-chef/cookbooks"], make sure cookbook_path is set correctly.
@DarrenN
DarrenN / stop_words.txt
Created January 29, 2011 22:02
Naively parse a text removing stopwords
'tis
'twas
a
aah
aaron
abandon
abandoned
abbott
abby
abe
@DarrenN
DarrenN / commas.html
Created January 4, 2011 18:00
makes commas in number on fly
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Index</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<form action="index_submit" method="get" accept-charset="utf-8">
@DarrenN
DarrenN / Euler3.coffee
Created December 21, 2010 18:17
Project Euler #3 in pure CoffeeScript
# Project Euler #3 -> http://projecteuler.net/index.php?section=problems&id=3
# Q: What is the largest prime factor of the number 600851475143 ?
# Generate a Sieve of Eratosthenes (an array loaded with prime numbers)
primes = []
sieve = (set, pos) ->
return false if pos >= set.length
if set[pos] * set[pos] < set[set.length-1]
primes = (x for x in set when x == set[pos] or x % set[pos] != 0)
return sieve(primes,pos+1)
@DarrenN
DarrenN / Euler9.rb
Created December 20, 2010 04:31
Project Euler #9 in Ruby
for n in 0..100
for m in 1..100
if (m>n)
a = 2*m*n
b = (m**2 - n**2)
c = (m**2 + n**2)
end
if (a + b + c == 1000)
puts "Triplet = #{a} + #{b} = #{c} of which the product is #{a*b*c}"
@DarrenN
DarrenN / CoffeeScript
Created December 19, 2010 19:21
Uses CoffeeScript and Underscore.js to solve Project Euler #3 http://projecteuler.net/index.php?section=problems&id=3
# Sieve of Eratosthenes
# (an array loaded with prime numbers)
base = [2..10000]
primes = []
sieve = (set, pos) ->
return false if pos >= set.length
if set[pos] * set[pos] < set[set.length-1]
primes = _.select set, ((num) ->
return num if num == set[pos]
return num % set[pos] != 0