This is an implementation of the quicksort algorithm written in Ruby. Basically, the same code you'll find on Wikibook's entry for quicksort implementations. A gem from the 'Book'.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_character(hexnum) | |
char = '' | |
char << hexnum.to_i(16) | |
end |
- Lukasz Wrobel demystifies
nil
in Ruby
Why the id of nil is 4 in Ruby
- An older post by Neeraj Singh on
nil
'sobject_id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
headers = DEFAULT_HEADERS
stub_request(:get, endpoint)
OlderNewer