Last active
March 23, 2021 14:47
-
-
Save guigaoliveira/6e35b88f1e7e3a0ac40526dd4d6e13fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Solutions do | |
@moduledoc false | |
@doc """ | |
"turma de elixir formacao elixir de elixir" | |
%{"turma" => 1, "de" => 2, "elixir" => 3, "formacao" => 1} | |
""" | |
def get_words_occurrence(sentence) do | |
sentence | |
|> String.split() | |
|> Enum.frequencies | |
end | |
@doc """ | |
[1, 5, 0, 8, 10, 9], 19 | |
[4, 5] | |
""" | |
def get_two_sum_index(integers_list, target) do | |
Enum.with_index(integers_list) |> | |
Enum.reduce_while(%{}, fn {current_value, index}, acc -> | |
to_find = target - current_value | |
if Map.has_key?(acc, to_find) and Map.get(acc, to_find) !== index do | |
{:halt, [index, Map.get(acc, to_find)]} | |
else | |
{:cont, Map.put(acc, current_value, index)} | |
end | |
end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment