Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

6ku_2

elixir/6ku/6ku_2.livemd

6ku_2

PI approximation

https://www.codewars.com/kata/550527b108b86f700000073f

defmodule Piapprox do
  def iter_pi(epsilon) do
    Stream.iterate(1, fn x -> x + 1 end)
    |> Enum.reduce_while(4, fn x, acc ->
      if abs(:math.pi() - acc) > epsilon do
        if rem(x, 2) == 0 do
          {:cont, acc + 1 / (x * 2 + 1) * 4}
        else
          {:cont, acc - 1 / (x * 2 + 1) * 4}
        end
      else
        {:halt, [x, trunc(acc * 1.0e10) / 1.0e10]}
      end
    end)
  end
end
Piapprox.iter_pi(0.01)
# 1315929035
# :math.pi()

Calculate the area of a regular n sides polygon inside a circle of radius r

https://www.codewars.com/kata/5a58ca28e626c55ae000018a

defmodule Kata do
  def area_of_polygon_inside_circle(circle_radius, number_of_sides) do
    Float.round(
      circle_radius * circle_radius * number_of_sides *
        :math.sin(:math.pi() / number_of_sides * 2) /
        2,
      3
    )
  end
end
Kata.area_of_polygon_inside_circle(3, 3)

Array.diff

https://www.codewars.com/kata/523f5d21c841566fde000009

defmodule ArrayDiff do
  def array_diff(a, b), do: Enum.reject(a, &(&1 in b))
end
# [2,2]
ArrayDiff.array_diff([1, 2, 2], [1])

Roman Numerals Decoder

https://www.codewars.com/kata/51b6249c4612257ac0000005

defmodule Solution do
  @digit %{"I" => 1, "V" => 5, "X" => 10, "L" => 50, "C" => 100, "D" => 500, "M" => 1000}
  def decode(roman) do
    roman = roman |> String.graphemes()

    roman
    |> Enum.map(&@digit[&1])
    |> Enum.with_index()
    |> Enum.reduce(0, fn {x, y}, acc ->
      if Enum.at(roman, y + 1) != nil and @digit[Enum.at(roman, y + 1)] > x,
        do: acc - x,
        else: acc + x
    end)
  end
end
Solution.decode("XLVI")

Esolang Interpreters #1 - Introduction to Esolangs and My First Interpreter (MiniStringFuck)

https://www.codewars.com/kata/586dd26a69b6fd46dd0000c0

defmodule MiniStringFuck do
  @moduledoc false
  def execute(command) do
    command
    |> String.codepoints()
    |> Enum.reduce({0, []}, fn
      "+", {255, out} -> {0, out}
      "+", {x, out} -> {x + 1, out}
      ".", {x, out} -> {x, [out | [x]]}
      _cp, acc -> acc
    end)
    |> elem(1)
    |> List.to_string()
  end
end
# function myFirstInterpreter(code) {
#   let out = "",
#     byte = 0;
#   [...code].forEach(cmd => {
#     switch (cmd) {
#       case "+":
#         byte = (byte + 1) % 256;
#         break;
#       case ".":
#         out += String.fromCharCode(byte);
#         break;
#     }
#   });
#   return out;
# }
alphabet_program = String.duplicate("+", 65) <> String.duplicate(".+", 25) <> "."

# == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MiniStringFuck.execute(alphabet_program)

Statistics for an Athletic Association

https://www.codewars.com/kata/55b3425df71c1201a800009c

defmodule AthleticAssociation do
  def stat(""), do: ""

  def stat(string) do
    ts =
      string
      |> String.split(", ")
      |> Enum.map(&amp;String.split(&amp;1, "|"))
      |> Enum.map(&amp;Enum.map(&amp;1, fn x -> String.to_integer(x) end))
      |> Enum.map(fn [h, m, s] -> h * 60 * 60 + m * 60 + s end)
      |> Enum.sort()

    "Range: #{format(range(ts))} Average: #{format(avg(ts))} Median: #{format(median(ts))}"
  end

  defp range(ts), do: Enum.min_max(ts) |> (fn {min, max} -> max - min end).()

  defp avg(ts), do: Enum.sum(ts) |> div(Enum.count(ts))

  defp median(ts), do: ts |> length() |> div_rem(2) |> median(ts)
  defp median({half, 0}, ts), do: avg(Enum.slice(ts, half - 1, 2))
  defp median({half, 1}, ts), do: Enum.at(ts, half)

  defp div_rem(dividend, divisor), do: {div(dividend, divisor), rem(dividend, divisor)}

  defp format(t) do
    {h, x} = div_rem(t, 60 * 60)
    {m, s} = div_rem(x, 60)

    [h, m, s]
    |> Enum.map(&amp;Integer.to_string/1)
    |> Enum.map_join("|", &amp;String.pad_leading(&amp;1, 2, "0"))
  end
end
AthleticAssociation.stat("01|15|59, 1|47|16, 01|17|20, 1|32|34, 2|17|17")

Are they the “same”?

https://www.codewars.com/kata/550498447451fbbd7600041c

defmodule Aretheythesame do
  @spec comp([number], [number]) :: boolean
  def comp(a, b) do
    a |> Enum.map(&amp;(&amp;1 * &amp;1)) |> Enum.sort() == b |> Enum.sort()
  end
end
a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [11 * 11, 121 * 121, 144 * 144, 19 * 19, 161 * 161, 19 * 19, 144 * 144, 19 * 19]
Aretheythesame.comp(a, b)

Disguised sequences (II)

https://www.codewars.com/kata/56fe17fcc25bf3e19a000292

defmodule Disguised2 do
  def v1(n, p) do
    Stream.iterate(0, fn x -> x + 1 end)
    |> Enum.reduce_while(0, fn x, result ->
      if x <= n do
        # IO.inspect(pow(-1, x))
        # IO.inspect(:math.pow(-1, x))
        # IO.inspect(pow(4, n - x))
        # IO.inspect(n-x)
        # IO.inspect(:math.pow(4, n - x))
        part = :math.pow(-1, x) * p * :math.pow(4, n - x)

        IO.inspect(part)
        abc = n * 2 - x
        {:cont, result + part * div(factorial(abc), factorial(abc - x) * factorial(x))}
      else
        {:halt, result}
      end
    end)
  end

  def u1(n, p) do
    Stream.iterate(0, fn x -> x + 1 end)
    |> Enum.reduce_while(0, fn x, result ->
      if x <= n do
        part = :math.pow(-1, x) * p * :math.pow(4, n - x)
        abc = n * 2 - x + 1
        {:cont, result + part * div(factorial(abc), factorial(abc - x) * factorial(x))}
      else
        {:halt, result}
      end
    end)
  end

  def v_eff(n, p) do
    v1(n, p)
  end

  def u_eff(n, p) do
    u1(n, p)
  end

  def pow(n, x) do
    Enum.reduce(1..x, 1, fn _, acc -> n * acc end)
  end

  def factorial(n) when n <= 1, do: 1
  def factorial(n) when n > 1, do: n * factorial(n - 1)
end
Disguised2.v1(1, 14)