Skip to content

Instantly share code, notes, and snippets.

main = do
print $ head1 ['x', 'y', 'z']
print $ max1 1 2
print $ fst1 (6, 5)
print $ zip1 [1, 2, 3, 4] ['a', 'b']
print $ filter1 (< 5) [1, 2, 8, 9]
print $ filter2 (\x -> x < 5) [1, 2, 8, 9]
print $ length1 [1, 2, 3]
print $ length2 [1, 2, 3]
print $ length3 [1, 2, 3]
quickCheck ((\n->(abs(n) == n) || (0-abs(n) == n)) :: Int -> Bool)
@evmorov
evmorov / Insert.hs
Last active October 12, 2016 22:02
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
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
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
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
require 'rspec'
require_relative 'mine_solver'
describe MineSolver do
describe '#safe' do
it do
board = '
1????
1????
111??
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 = []
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') }
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