{{ message }}

Instantly share code, notes, and snippets.

# O-I

Last active Nov 28, 2018
Ruby Character to Unicode Converter
View get_character.rb
 def get_character(hexnum) char = '' char << hexnum.to_i(16) end
Last active Dec 25, 2015
Ruby Quicksort
Last active Aug 29, 2015
Master of nil
Last active Jul 31, 2019
Ruby univariate polynomial root finder
View univariate_polynomial_roots.rb
 require 'matrix' # Input: an array of the n coefficients [a_n, a_n-1,..., a_1] of # a univariate polynomial p of degree n ([a_n]x^n + [a_n-1]x^n-1 + ... + a_1) # # Output: an array of all n roots of p # # Exploits the fact that the eigenvalues of the companion matrix of the # monic equivalent of p are the roots of p #
Created Jul 11, 2014
View fizzbuzz.hs
 fizzbuzz :: Int -> [String] fizzbuzz n = take n \$ addNums \$ zipWith (++) fizz buzz where fizz = cycle ["", "", "Fizz"] buzz = cycle ["", "", "", "", "Buzz"] addNums = zipWith numOrNot \$ map show [1..] numOrNot = \ x y -> if null y then x else y
Created Jul 11, 2014
Generate n unique random natural numbers whose sum is m
View random_sum.rb
 # Question source: http://codegolf.stackexchange.com/q/8574/12268 # Write an algorithm in any programming language you desire # that generates n unique randomly-distributed random natural # numbers (i.e. positive integers, no zero), sum of which is # equal to t, where t is bigger than or equal to n*(n+1)/2. # Example: Generate 10 unique random natural numbers, sum of which is equal to 500. def rand_sum(size, sum)
Last active Aug 29, 2015
Experimenting with the Guardian Content API
View guardian_api_test.rb
 require 'open-uri' require 'dotenv' require 'json' require 'pry' Dotenv.load # Basic structure of a Guardian API request: # BASE_URI + endpoint + queries + api-key + params
Created Sep 24, 2014
Nsect an array in Ruby
View nsect.rb
 # nsect takes an array and optional positive integer n (default is 3) # and returns the array partitioned into n arrays, (n-1) of which are # of size i, the nth partition being of size (i-1), i, or (i+1). # Examples below: def nsect(arr, n = 3) i = (arr.size + 1) / n ans = [] (n-1).times do |j| ans << arr[(0+j)*i...(1+j)*i]
Created Oct 2, 2014
Ulam spiral
View ulam_spiral.rb
 require 'prime' def ulam_spiral(n) matrix = Array.new(n) { Array.new(n) } path = [*1..n*n].reverse padding = (n*n).to_s.size layer = 0 until path.empty? matrix[layer].map! { |l| l || path.shift } matrix = matrix.transpose.reverse
Last active Aug 29, 2015
[TIx 1] Module#define_method behavior across Ruby versions
View define_method.md

Here's today's scenario. I'm working on mocking requests to an API to test a Ruby wrapper I've built for it. I have a `spec_helper` file with several convenience methods that look something like this:

```# spec_helper.rb
# snip

def stub_get(path, options = {})
endpoint = DEFAULT_API_URL + path