Remove binary_test because it's not actually needed
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
22
lib/util.ex
22
lib/util.ex
@@ -14,26 +14,4 @@ defmodule Omnibot.Util do
|
|||||||
def denotify_nick(nick) do
|
def denotify_nick(nick) do
|
||||||
String.graphemes(nick) |> Enum.join("\u200b")
|
String.graphemes(nick) |> Enum.join("\u200b")
|
||||||
end
|
end
|
||||||
|
|
||||||
def binary_search([], _key) do
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def binary_search([{key, value} | _], key) do
|
|
||||||
{0, value}
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc "Attempts to find to find the given key in a sorted associative array."
|
|
||||||
def binary_search(list, key) do
|
|
||||||
{head, tail} = Enum.split(list, trunc(length(list) / 2))
|
|
||||||
[{mid, _} | _] = tail
|
|
||||||
if key < mid do
|
|
||||||
binary_search(head, key)
|
|
||||||
else
|
|
||||||
case binary_search(tail, key) do
|
|
||||||
nil -> nil
|
|
||||||
{index, item} -> {index + length(head), item}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,21 +12,4 @@ defmodule Omnibot.UtilTest do
|
|||||||
assert Util.string_or_nil("") == nil
|
assert Util.string_or_nil("") == nil
|
||||||
assert Util.string_or_nil("asdf") == "asdf"
|
assert Util.string_or_nil("asdf") == "asdf"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "binary_search" do
|
|
||||||
indexes = 0..10 |> Enum.to_list()
|
|
||||||
values = indexes |> Enum.map(&({&1, &1 * 2}))
|
|
||||||
assert Enum.map(indexes, &(Util.binary_search(values, &1))) == values
|
|
||||||
|
|
||||||
indexes = 0..101 |> Enum.to_list()
|
|
||||||
values = indexes |> Enum.map(&({&1, &1 * 2}))
|
|
||||||
assert Enum.map(indexes, &(Util.binary_search(values, &1))) == values
|
|
||||||
|
|
||||||
values = [a: 15, b: 22, c: -1, d: 0]
|
|
||||||
|
|
||||||
assert Util.binary_search(values, :a) == {0, 15}
|
|
||||||
assert Util.binary_search(values, :b) == {1, 22}
|
|
||||||
assert Util.binary_search(values, :c) == {2, -1}
|
|
||||||
assert Util.binary_search(values, :d) == {3, 0}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user