This file contains hidden or 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
| const MAX = 1000000; | |
| const cache = new Array(MAX); | |
| function nextFor(n) { | |
| if (n == 1) { | |
| return 1; | |
| } | |
| else if (n <= MAX && cache[n - 1] !== undefined) { | |
| return cache[n - 1]; | |
| } |
This file contains hidden or 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
| ZERO = Int64.new(0) | |
| ONE = Int64.new(1) | |
| TWO = Int64.new(2) | |
| THREE = Int64.new(3) | |
| ONE_MILLION = Int64.new(1_000_000) | |
| class Collatz | |
| def initialize(min : Int64, max : Int64) | |
| @min = min | |
| @max = max |
This file contains hidden or 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
| defmodule Collatz do | |
| def call(n, cache), do: call(n, 0, cache) | |
| def call(1, length, _), do: length + 1 | |
| def call(n, length, cache), do: get(n, length, cache, cache[n]) | |
| defp get(n, length, cache, nil), do: call(next(n), length, cache) + 1 | |
| defp get(_, _, _, value), do: value | |
| defp next(n) when rem(n, 2) == 0, do: div(n, 2) | |
| defp next(n) when rem(n, 2) == 1, do: n * 3 + 1 |
This file contains hidden or 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
| defmodule NaiveBubbleSort do | |
| def call([]), do: [] | |
| def call(list), do: call(list, 0, length(list) - 1, true) | |
| def call(list, idx, last_idx, false) when last_idx == idx, do: list |> call | |
| def call(list, idx, last_idx, true) when last_idx == idx, do: list | |
| def call(list, idx, last_idx, done) do | |
| {a, b} = values_to_compare(list, idx) | |
| {list, done} = process(list, idx, a, b, done) |
This file contains hidden or 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 'minitest/autorun' | |
| class Item | |
| include Comparable | |
| def initialize(item, priority:) | |
| @item = item | |
| @priority = priority | |
| end |
This file contains hidden or 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
| defmodule FunctionLogger do | |
| defmacro __using__(_) do | |
| quote do | |
| import Kernel, except: [def: 2] | |
| import FunctionLogger | |
| end | |
| end | |
| defmacro def(fun_def, do: block) do | |
| quote do |
This file contains hidden or 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 HStruct | |
| def self.new(*members, defaults: {}, &block) | |
| klass = Class.new do | |
| @members = members | |
| @defaults = defaults | |
| class << self | |
| attr_reader :members, :defaults | |
| end |
This file contains hidden or 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
| sdfsdfsdfs |
This file contains hidden or 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
| sfsd | |
| sfds |
This file contains hidden or 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
| defmodule PrimeCalculator do | |
| @moduledoc """ | |
| Returns all prime numbers within a range | |
| """ | |
| import Enum, only: [sort: 1, to_list: 1, filter: 2] | |
| import Map, only: [keys: 1, put: 3] | |
| import Float, only: [ceil: 1] | |
| import :math, only: [sqrt: 1] |