Skip to content

Instantly share code, notes, and snippets.

View jonelf's full-sized avatar

Jonas Elfström jonelf

View GitHub Profile
@jonelf
jonelf / gist:4071349
Created November 14, 2012 10:12
Sort by random vs. Fisher-Yates shuffle
calculateDistribution = (results) ->
dict = {}
for res in results
prop = res.join('')
if dict.hasOwnProperty(prop) then dict[prop]++ else dict[prop]=1
for key, value of dict
console.log key+":"+value
console.log "Sort by random"
@jonelf
jonelf / gist:4109899
Created November 19, 2012 09:54
Replace replace linq regex
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
@jonelf
jonelf / gist:4134839
Created November 23, 2012 09:57
Ethiopian Multiplication
m = 673
n = 7
product = 0
console.log "#{m} times #{n} by Ethiopian Multiplication\n"
while m >= 1
product += n unless m % 2 == 0
m = Math.floor(m / 2)
n = Math.floor(n * 2)
@jonelf
jonelf / gist:4340701
Created December 19, 2012 21:28
Longest running sequence from all permutations of array of strings
# http://stackoverflow.com/a/13961420/44620
class Sequence_tester
def self.max_running_sequence(arr_of_chararr)
reduced = []
all_same_chars = []
arr_of_chararr.each do |str|
arr = str.chars.to_a
if arr.all? {|c| c == arr.first}
@jonelf
jonelf / gist:4666945
Created January 29, 2013 19:30
Playing with Fibonacci in CoffeeScript and Ruby They are both kind of silly but I think I like the CoffeeScript version better.
#CoffeScript
fib = (n, i=0, j=1) -> i=j+j=i for _ in [1..n]
fib(7)
=> [1, 1, 2, 3, 5, 8, 13]
#Ruby
fib = ->(n, i=0, j=1){(1..n).map{i=j+j=i}}
fib[7]
=> [1, 1, 2, 3, 5, 8, 13]
@jonelf
jonelf / gist:5466993
Created April 26, 2013 12:10
Python list slice steps in Ruby.
class Array
alias_method :brackets, :[]
def [](*args)
return brackets(*args) if args.length != 3
start, stop, step = *args
self.values_at(*(start...stop).step(step))
end
end
@jonelf
jonelf / gist:5928868
Last active June 22, 2018 02:21
Math quiz
(0!+0!+0!)! = 6
(1+1+1)! = 6
2+2+2 = 6
3*3-3 = 6
√4+√4+√4 = 6
5/5+5 = 6
6-6+6 = 6
7-7/7 = 6
8-√√(8+8) = 6
√9*√9-√9 = 6
require 'prime'
primes = Prime.lazy
n = 4
loop do
n += 2
prime1 = primes.next
prime2 = n - 1
prime2 -= 2 until prime2.prime?
@jonelf
jonelf / runkeeper.rb
Created October 16, 2013 18:39
Gets your RunKeeper activities for the current month.
require 'json'
require 'httpclient'
domain = "runkeeper.com"
sslhost = "https://#{domain}/"
host = "http://#{domain}/"
email, user, password = "[email protected]", "jonelf", "password"
client = HTTPClient.new
body = { '_eventName' => 'submit', 'redirectUrl' => '/index', 'failUrl' => '', 'email' => email, 'password' => password }
Start_tags = {"*" => :emphasis, "{" => :footnote}
End_tags = {emphasis: "*", footnote: "}"}
Tags = Start_tags.keys.join
def split_by_paragraph(text, &block)
mode = Start_tags[text[0]] || :normal
if mode == :normal
index = text[1..-1].index /[#{Tags}]/
if index.nil?
yield ({:type => mode, :content => text})