Skip to content

Instantly share code, notes, and snippets.

# frozen_string_literal: true
#
# Copyright (c) 2016 Pete Hanson
class Hex
HEX_TO_I = { 'a' => 10, 'b' => 11, 'c' => 12,
'd' => 13, 'e' => 14, 'f' => 15 }.freeze
def initialize(hex_number_string)
@hex_number = hex_number_string
# frozen_string_literal: true
#
# Copyright (c) 2016 Pete Hanson
def number_list_to_string(list)
list.map(&:to_s).join ''
end
def lowest_number(number_list)
number_list.permutation
@pdxwolfy
pdxwolfy / x3.rb
Last active March 19, 2016 18:31
# frozen_string_literal: true
#
# Copyright (c) 2016 Pete Hanson
def lowest_number(number_list)
number_list.map(&:to_s)
.permutation
.map(&:join)
.map(&:to_i)
.min
# frozen_string_literal: true
#
# Copyright (c) 2016 Pete Hanson
def smash(string)
string.each_char.select { |char| char =~ /\A[a-z]\Z/i }.join ' '
end
puts smash('Hello, world!') # -> H e l l o w o r l d
@pdxwolfy
pdxwolfy / x5.rb
Last active March 19, 2016 19:43
text = <<eof
Then Almitra spoke again and said, "And what of Marriage, master?" And he answered saying: You were born together, and together you shall be forevermore. You shall be together when white wings of death scatter your days. Aye, you shall be together even in the silent memory of God. But let there be spaces in your togetherness, And let the winds of the heavens dance between you. Love one another but make not a bond of love: Let it rather be a moving sea between the shores of your souls. Fill each other's cup but drink not from one cup. Give one another of your bread but eat not from the same loaf. Sing and dance together and be joyous, but let each one of you be alone, Even as the strings of a lute are alone though they quiver with the same music. Give your hearts, but not into each other's keeping. For only the hand of Life can contain your hearts. And stand together, yet not too near together: For the pillars of the temple stand apart, And the oak tree and the cypress grow not in each other's sha
# frozen_string_literal: true
#
# Copyright (c) 2016 Pete Hanson
require 'minitest/autorun'
def lowest_number(number_list)
number_list.map(&:to_s).sort do |a, b|
if a.start_with?(b)
a[b.size..-1] <=> b
#!/usr/bin/env ruby -w
# Copyright (c) 2016 Pete Hanson
# frozen_string_literal: true
class Fibonacci
def self.nth n_th
raise ArgumentError, 'n_th must be >= 1' unless n_th >= 1
@fib = [1, 1]
next_to_last = @fib.first
OMMAIZE = %r{
(\d) # need at least one digit before each group of 3
(\d\d\d) # 3 digits in a row
(\D) # decimal point or comma
}x
def to_currency amount_as_string
rounded_amount = round_float_as_string amount_as_string
amount_with_commas = insert_commas! rounded_amount
"$#{amount_with_commas}"
#!/usr/bin/env ruby
# Copyright (c) 2016 Pete Hanson
# frozen_string_literal: true
# :reek:UtilityFunction: { exclude: %w(largest_multiple) }
NUMBER = %(
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
class Triangle
attr_reader :rows
def initialize height
factorial = [1]
1.upto(height).each { |n| factorial << n * factorial.last }
@rows = 0.upto(height - 1).map do |row|
0.upto(row).map { |seq| factorial[row] / (factorial[seq] * factorial[row - seq]) }
end