Download examples/contestants/bob/hasher.ex from Snapkitty/forge-code: direct link, hf CLI and curl.
- Browser
- Download file 993 Bytes
-
https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/examples/contestants/bob/hasher.ex
- Command line
-
hf download hf://datasets/Snapkitty/forge-code/examples/contestants/bob/hasher.ex
-
curl -L -o hasher.ex https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/examples/contestants/bob/hasher.ex
993 Bytes
| defmodule Bob.Hasher do | |
| @moduledoc """ | |
| Bob's implementation of the integer hashing challenge. | |
| Uses iterative approaches for better performance. | |
| """ | |
| @doc """ | |
| Computes SHA-256 hash of a string. | |
| """ | |
| def sha256(input) do | |
| :crypto.hash(:sha256, input) |> Base.encode16(case: :lower) | |
| end | |
| @doc """ | |
| Sums a list of integers iteratively. | |
| """ | |
| def sum_list(list) do | |
| do_sum(list, 0) | |
| end | |
| defp do_sum([], acc), do: acc | |
| defp do_sum([head | tail], acc), do: do_sum(tail, acc + head) | |
| @doc """ | |
| Computes factorial iteratively. | |
| """ | |
| def factorial(n) do | |
| do_factorial(n, 1) | |
| end | |
| defp do_factorial(0, acc), do: acc | |
| defp do_factorial(n, acc), do: do_factorial(n - 1, acc * n) | |
| @doc """ | |
| Main entry point that routes to the appropriate function. | |
| """ | |
| def solve(:sha256_forge, input), do: sha256(input) | |
| def solve(:sum_list, input), do: sum_list(input) | |
| def solve(:factorial_5, input), do: factorial(input) | |
| def solve(_challenge, _input), do: nil | |
| end | |