Skip to content

Instantly share code, notes, and snippets.

@Kache
Kache / fib.rb
Last active January 20, 2022 18:53
require 'matrix'
require 'bigdecimal'
def fib_linear(n)
return 0 if n.zero?
arr = [0, 1]
(n - 1).times { arr << arr.last + arr.shift }
arr.last
end
from __future__ import annotations
import hashlib as hl
import inspect
import io
from queue import Queue
from threading import Thread
from typing import Any, AnyStr, Iterable, TypeVar, TYPE_CHECKING, Iterator
import zlib
if TYPE_CHECKING:
@Kache
Kache / bin_heap.rb
Created January 24, 2021 06:06
Binary Heap in Ruby
class BinHeap
def initialize(&block)
@btree = []
@key_func = block
end
def enq(val)
@btree << val
heapify(start: size - 1) { |i| parent_that_is_larger(i) }
self
require 'pry-byebug'
require 'matrix'
class MatrixSum
def self.matrix
@matrix ||= (<<~MATRIX_STR).split("\n").map { |row| row.split(' ').map(&:to_i) }
7 53 183 439 863 497 383 563 79 973 287 63 343 169 583
627 343 773 959 943 767 473 103 699 303 957 703 583 639 913
447 283 463 29 23 487 463 993 119 883 327 493 423 159 743
217 623 3 399 853 407 103 983 89 463 290 516 212 462 350
@Kache
Kache / pe203.rb
Created July 28, 2019 19:34
Project Euler 203
require 'prime'
require 'pry-byebug'
module SquarefreeBinomialCoefficients
def self.choose(n, k)
top = ((n - k + 1)..n).reduce(1, :*)
bottom = (2..k).reduce(1, :*)
top / bottom
end
@Kache
Kache / acker.rb
Last active February 5, 2019 22:46
module Acker
def self.orig(m, n)
if m.zero?
n + 1
elsif n.zero?
orig(m - 1, 1)
else
a = orig(m, n - 1)
orig(m - 1, a)
end
# simulate rolling a fair M-sided die with a N-sided die
def roll(m, with:)
lower, subinterval_len = 0, m.to_r # m segments of reals between 0 and m
# until subinterval completely in one segment
until (lower + subinterval_len).ceil - lower.floor <= 1
subinterval_len /= with
lower += subinterval_len * rand(with) # random nth partition of subinterval
end
lower.floor + 1
@Kache
Kache / flek_test_reporter_plugin.rb
Created September 12, 2017 06:37
Minitest reporter for slowest tests and outputs a rerun copy/paste
# Uses Minitest's built-in "plugin system"
module Minitest
def self.plugin_flek_test_reporter_init(options)
self.reporter.reporters << FlekTestReporter.new
end
# TODO: bring back some old features, e.g. fast-fail
class FlekTestReporter < Reporter
def initialize(io = $stdout, options = {})
super
@Kache
Kache / extended_euclidean.rb
Last active January 30, 2017 19:07
Extended Euclidean Algorithm in Ruby for GCD, Bézout coefficients, and Modular Multiplicative Inverse
# Extended Euclidean algorithm
#
# given integers a and m, iteratively computes:
# * coefficients s and t that satisfies Bézout's Identity: s*a + t*m == gcd(a, m)
# * greatest common denominator: gcd(a, m)
# * modular multiplicative inverse of a with respect to the modulus m, if it exists
def extended_euclidean(a, m)
r0, r1 = a, m
s, t = 1, 0
until r1.zero?
# Activate the gem you are reporting the issue against.
#gem 'activerecord', '4.2.1' # works in this version, for me
gem 'activerecord', '4.0.13'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)