Skip to content

Instantly share code, notes, and snippets.

View sanchojaf's full-sized avatar

Miguel Sancho Fernandez sanchojaf

View GitHub Profile
@sanchojaf
sanchojaf / gist:1b6b51cc6932fdcf9f6eb1f8f104d31f
Last active July 20, 2017 13:27
rails-stripe-membership-saas

ensure that you have rails 4.2 like envioroment

git clone git://github.com/RailsApps/rails-stripe-membership-saas.git

cd rails-stripe-membership-saas

bundle

rake db:migrate

json_payload =
"{
\"orders\": [
{
\"id\": \"R123456789\",
\"status\": \"complete\",
\"channel\": \"spree\",
\"email\": \"spree@example.com\",
\"currency\": \"USD\",
\"placed_on\": \"2017-03-10T15: 20: 43Z\",
@sanchojaf
sanchojaf / traversal_binary_tree.rb
Last active November 6, 2016 16:42
Breadth-First Insertion and Traversal in a Binary Tree
# The traverse iterator will iterate over the tree in the same
# breadth-first order.
class Tree
attr_accessor :left
attr_accessor :right
attr_accessor :data
def initialize(x=nil)
@left = @right = nil
@sanchojaf
sanchojaf / towers_of_hanoi_with_stack.rb
Last active November 6, 2016 15:42
Towers of Hanoi using Stack (stack, recursion)
# We have a pole with a certain number of disks staked on it;
# call this the source pole. We want move all of these to the
# destination pole, using a third (auxiliary) pole as an
# intermediate resting place. The catch is that you can move
# only one disk at the time, and you cannot ever place a larger
# disk onto a smaller one.
def towers(list)
while !list.empty?
n, src, dst, aux = list.pop
@sanchojaf
sanchojaf / matching_ip_v4_address.rb
Created November 6, 2016 14:53
Matching IP V4 Address (ruby, regular expressions)
# IP V4 Address are four decimal numbers separated by periods,
# each number ranging from 0 to 255.
number = /(\d|[01]?\d\d|2[0-4]\d|25[0-5])/
expression = /^(#{number}\.){3}#{number}$/
sample_text = "10.123.80.202"
sample_text =~ expression
@sanchojaf
sanchojaf / matching_keyword_value_pair.rb
Last active November 6, 2016 14:56
Matching Keyword-Value Pair (ruby, regular expressions)
# Extracts the keyword and the value. The assumptions are that
# the keyword oor attribute is a single word, the value extends
# to the end of the line, and the equal sign may be surrounded
# by whitespace.
expression = /(\w+)\s*=\s*(.*?)$/
sample_text = "color = blue"
sample_text =~ expression
@sanchojaf
sanchojaf / quick_select.rb
Last active April 8, 2018 01:18
Ruby QuickSort and QuickSelect
def quicksort(arr, first, last)
return arr unless first < last
p_index = partition(arr, first, last)
quicksort(arr, first, p_index - 1)
quicksort(arr, p_index + 1, last)
arr
end
def quickselect(arr, first, last, i)
return arr[first] if first == last
@sanchojaf
sanchojaf / monotone_chain_convex_hull.rb
Created October 29, 2016 23:28
Andrew's monotone chain convex hull algorithm
# Andrew's monotone chain convex hull algorithm constructs the convex hull
# of a set of 2-dimensional points in O(n\log n) time.
# It does so by first sorting the points lexicographically (first by x-coordinate,
# and in case of a tie, by y-coordinate), and then constructing upper and lower
# hulls of the points inO(n) time.
# An upper hull is the part of the convex hull, which is visible from the above.
# It runs from its rightmost point to the leftmost point in counterclockwise order.
# Lower hull is the remaining part of the convex hull.