Skip to content

Commit

Permalink
Adding a solutions dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
algogrit committed Mar 20, 2024
1 parent 7512a94 commit 17e7e8f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
20 changes: 20 additions & 0 deletions solutions/exercism/bird_count.ex
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions solutions/exercism/boutique_suggestions.ex
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions solutions/exercism/guessing_game.ex
Original file line number Diff line number Diff line change
@@ -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"
# ```
36 changes: 36 additions & 0 deletions solutions/exercism/log_level.ex
Original file line number Diff line number Diff line change
@@ -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 | - |
16 changes: 16 additions & 0 deletions solutions/exercism/secrets.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 17e7e8f

Please sign in to comment.