This file contains 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
# 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 |
This file contains 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
# 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 |
This file contains 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
# 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 |
This file contains 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
# 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 |
This file contains 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
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 |
This file contains 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
# 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 |
This file contains 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
#!/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 |
This file contains 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
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}" |
This file contains 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
#!/usr/bin/env ruby | |
# Copyright (c) 2016 Pete Hanson | |
# frozen_string_literal: true | |
# :reek:UtilityFunction: { exclude: %w(largest_multiple) } | |
NUMBER = %( | |
73167176531330624919225119674426574742355349194934 | |
96983520312774506326239578318016984801869478851843 | |
85861560789112949495459501737958331952853208805511 | |
12540698747158523863050715693290963295227443043557 |
This file contains 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
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 |
OlderNewer