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
require "rubygems"
require 'mongo'
db = Mongo::Connection.new.db("mydb")
## Errors:
./mongo.rb:6: uninitialized constant Mongo (NameError)
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
1 10.0.1.1 (10.0.1.1) 0.954 ms 0.775 ms 2.437 ms
2 cpe-98-14-80-1.nyc.res.rr.com (98.14.80.1) 9.815 ms 9.027 ms 9.670 ms
3 gig-2-14-nycmnyr-rtr1.nyc.rr.com (24.29.157.25) 12.519 ms 8.980 ms 10.487 ms
4 gig10-0-0-nycmnya-rtr1.nyc.rr.com (24.29.157.98) 8.909 ms 8.598 ms 9.500 ms
5 tenge-0-3-0-nwrknjmd-rtr.nyc.rr.com (24.29.97.6) 12.971 ms 10.117 ms 9.746 ms
6 ae-4-0.cr0.nyc30.tbone.rr.com (66.109.6.78) 9.278 ms 9.223 ms 43.665 ms
7 ae-1-0.pr0.nyc30.tbone.rr.com (66.109.6.161) 43.319 ms 8.634 ms 9.928 ms
8 xe-4-3-0.edge2.newark1.level3.net (4.59.20.161) 9.459 ms 10.364 ms 9.663 ms
9 ae-32-52.ebr2.newark1.level3.net (4.68.99.62) 14.356 ms 36.022 ms 17.790 ms
10 ae-4-4.ebr2.washington1.level3.net (4.69.132.101) 16.215 ms 15.545 ms 15.891 ms
@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
@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 / 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 / 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 / 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 / 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 / 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 / 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)