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 '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 |
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
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: |
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 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 |
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 '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 |
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 '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 |
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
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 |
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
# 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 |
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
# 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 |
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
# 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? |
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
# 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) |
NewerOlder