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 Base | |
def smallest(num) | |
"base #{smallest_base(num)} => #{to_dec(num, smallest_base(num))}" | |
end | |
def all_bases(num) | |
(smallest_base(num).to_i..16).map { |base| "base #{base} => #{to_dec(num, base)}" }.join("\n") | |
end | |
def to_dec(num, base) |
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
require 'rspec' | |
require_relative 'base' | |
describe Base do | |
describe '#smallest' do | |
it do | |
expect(subject.smallest('1')).to eq('base 2 => 1') | |
end | |
it do |
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 Nickname | |
def initialize | |
@dict = File.readlines('enable1.txt').map(&:strip) | |
end | |
def generate(name) | |
name = name.downcase.gsub(/\W+/, '') | |
substrings(name).reduce([]) do |names, substring| | |
return names.map(&:capitalize).join(', ') if names.any? && substring.size < names.last.size | |
(@dict.include? substring) ? names.push(substring) : names |
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
require 'rspec' | |
require_relative 'nickname' | |
describe Nickname do | |
describe '#generate' do | |
it { expect(subject.generate('Donald Knuth')).to eq('Donut') } | |
it { expect(subject.generate('Alan Turing')).to eq('Alanin, Anting') } | |
it { expect(subject.generate('Claude Shannon')).to eq('Clades, Cannon') } | |
it { expect(subject.generate('Ada Lovelace')).to eq('Aloe, Alec, Alee, Alae') } | |
it { expect(subject.generate('Haskell Curry')).to eq('Harry, Hurry, Herry') } |
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 MineSolver | |
def safe(board) | |
@board = board.split("\n").reject(&:empty?).map do |row| | |
row.chars.map { |field| field == ' ' || field == '?' ? field : field.to_i } | |
end | |
set_risks | |
find_mine while is_there_mine? | |
safe = [] |
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
require 'rspec' | |
require_relative 'mine_solver' | |
describe MineSolver do | |
describe '#safe' do | |
it do | |
board = ' | |
1???? | |
1???? | |
111?? |
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
def dec_to_fib(dec) | |
fib_list = [0, 1] | |
fib_list.push fib_list[-2..-1].reduce(:+) while fib_list.last < dec | |
nums = [] | |
fib_list[1...-1].reverse.map do |n| | |
nums.any? && nums.reduce(:+) + n > dec ? 0 : (nums.push n; 1) | |
end.join.to_i | |
end |
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
require 'rspec' | |
require_relative 'unusual_bases' | |
describe '#dec_to_fib' do | |
it { expect(dec_to_fib(16)).to eq(1001000) } | |
it { expect(dec_to_fib(32)).to eq(10101000) } | |
it { expect(dec_to_fib(9024720)).to eq(1010100101010100000010001000010010) } | |
end | |
describe '#fib_to_dec' do |
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
import System.Random | |
import System.IO | |
check :: String -> String -> Char -> (Bool, String) | |
check word display c = | |
( c `elem` word | |
, [ if x == c | |
then c | |
else y | |
| (x, y) <- zip word display |
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
data Tree | |
= Leaf | |
| Node Int | |
Tree | |
Tree | |
deriving (Show) | |
insertOrder :: Tree -> Int -> Tree | |
insertOrder Leaf new = Node new Leaf Leaf | |
insertOrder (Node num left right) new |