From 17e7e8f5fc818da35073fa69eed8a5dfda4aeb97 Mon Sep 17 00:00:00 2001 From: Gaurav Agarwal Date: Wed, 20 Mar 2024 15:27:57 +0530 Subject: [PATCH] Adding a solutions dir. --- solutions/exercism/bird_count.ex | 20 ++++++++++++ solutions/exercism/boutique_suggestions.ex | 24 +++++++++++++++ solutions/exercism/guessing_game.ex | 18 +++++++++++ solutions/exercism/log_level.ex | 36 ++++++++++++++++++++++ solutions/exercism/secrets.ex | 16 ++++++++++ 5 files changed, 114 insertions(+) create mode 100644 solutions/exercism/bird_count.ex create mode 100644 solutions/exercism/boutique_suggestions.ex create mode 100644 solutions/exercism/guessing_game.ex create mode 100644 solutions/exercism/log_level.ex create mode 100644 solutions/exercism/secrets.ex diff --git a/solutions/exercism/bird_count.ex b/solutions/exercism/bird_count.ex new file mode 100644 index 0000000..c183f40 --- /dev/null +++ b/solutions/exercism/bird_count.ex @@ -0,0 +1,20 @@ +defmodule BirdCount do + def today([]), do: nil + def today([count | _]), do: count + + def increment_day_count(list) do + # Please implement the increment_day_count/1 function + end + + def has_day_without_birds?(list) do + # Please implement the has_day_without_birds?/1 function + end + + def total(list) do + # Please implement the total/1 function + end + + def busy_days(list) do + # Please implement the busy_days/1 function + end +end diff --git a/solutions/exercism/boutique_suggestions.ex b/solutions/exercism/boutique_suggestions.ex new file mode 100644 index 0000000..008f0bb --- /dev/null +++ b/solutions/exercism/boutique_suggestions.ex @@ -0,0 +1,24 @@ +defmodule BoutiqueSuggestions do + @default_budget 100 + + def get_combinations(tops, bottoms, options \\ []) do + tops + |> Enum.map(fn (top) -> + bottoms + |> Stream.filter(fn(bottom) -> + is_contrasting?(top, bottom) and is_within_budget?(top, bottom, options[:maximum_price]) + end) + |> Enum.map(fn (bottom) -> {top, bottom} end) + end) + |> List.flatten + + # for top <- tops, + # bottom <- bottoms, + # is_contrasting?(top, bottom) and is_within_budget?(top, bottom, options[:maximum_price]) do + # {top, bottom} + # end + end + + defp is_contrasting?(top, bottom), do: top.base_color != bottom.base_color + defp is_within_budget?(top, bottom, budget), do: (top.price + bottom.price) <= (budget || @default_budget) +end diff --git a/solutions/exercism/guessing_game.ex b/solutions/exercism/guessing_game.ex new file mode 100644 index 0000000..a9ba6d2 --- /dev/null +++ b/solutions/exercism/guessing_game.ex @@ -0,0 +1,18 @@ +defmodule GuessingGame do + def compare(secret_number, guess \\ :no_guess) + def compare(_secret_number, :no_guess), do: "Make a guess" + def compare(secret_number, guess) when secret_number == guess, do: "Correct" + def compare(secret_number, guess) when (secret_number - 1 == guess) or (secret_number + 1 == guess), do: "So close" + def compare(secret_number, guess) when (guess > secret_number), do: "Too high" + def compare(secret_number, guess) when (guess < secret_number), do: "Too low" +end + +# Modify the `compare` function to respond to a lack of guess. + +# ```elixir +# GuessingGame.compare(5) +# # => "Make a guess" + +# GuessingGame.compare(5, :no_guess) +# # => "Make a guess" +# ``` diff --git a/solutions/exercism/log_level.ex b/solutions/exercism/log_level.ex new file mode 100644 index 0000000..63453c7 --- /dev/null +++ b/solutions/exercism/log_level.ex @@ -0,0 +1,36 @@ +defmodule LogLevel do + def to_label(level, legacy?) do + case {level, legacy?} do + {0, false} -> :trace + {1, _} -> :debug + {2, _} -> :info + {3, _} -> :warning + {4, _} -> :error + {5, false} -> :fatal + _ -> :unknown + end + end + + def alert_recipient(level, legacy?) do + case {to_label(level, legacy?), legacy?} do + {:error, _} -> :ops + {:fatal, _} -> :ops + {:unknown, true} -> :dev1 + {:unknown, false} -> :dev2 + _ -> :false + end + end +end + + +# Use the `LogLevel.to_label/2` function from the previous task. If the log label is _error_ or _fatal_, send the alert to the _ops_ team. If you receive a log with an _unknown_ label from a legacy system, send the alert to the _dev1_ team, other unknown labels should be sent to the _dev2_ team. All other log labels can be safely ignored by returning _false_. + +# | Log code | Log label | Supported in legacy apps? | +# |-----------------------| --------- | ------------------------- | +# | 0 | trace | no | +# | 1 | debug | yes | +# | 2 | info | yes | +# | 3 | warning | yes | +# | 4 | error | yes | +# | 5 | fatal | no | +# | other / not supported | unknown | - | diff --git a/solutions/exercism/secrets.ex b/solutions/exercism/secrets.ex new file mode 100644 index 0000000..1d0c3f6 --- /dev/null +++ b/solutions/exercism/secrets.ex @@ -0,0 +1,16 @@ +defmodule Secrets do + def secret_add(secret), do: fn (x) -> x + secret end + + def secret_subtract(secret), do: fn (x) -> x - secret end + + def secret_multiply(secret), do: fn (x) -> x * secret end + + def secret_divide(secret), do: fn (x) -> div(x, secret) end + + def secret_and(secret), do: fn (x) -> Bitwise.band(x, secret) end + + def secret_xor(secret), do: fn (x) -> Bitwise.bxor(x, secret) end + + # def secret_combine(secret_function1, secret_function2), do: fn (x) -> x |> secret_function1.() |> secret_function2.() end + def secret_combine(secret_function1, secret_function2), do: fn (x) -> secret_function2.(secret_function1.(x)) end +end