From bb5eb9323fcf2bbd5b2a4c38e9d60a37071165d3 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Sun, 9 Jun 2024 12:26:06 +0500 Subject: [PATCH] leetcode: solve two sum problem in elixir --- leetcode/0001-two-sum/elixir/.formatter.exs | 4 +++ leetcode/0001-two-sum/elixir/.gitignore | 26 +++++++++++++++++ leetcode/0001-two-sum/elixir/lib/two_sum.ex | 23 +++++++++++++++ leetcode/0001-two-sum/elixir/mix.exs | 28 +++++++++++++++++++ .../0001-two-sum/elixir/test/test_helper.exs | 1 + .../0001-two-sum/elixir/test/two_sum_test.exs | 16 +++++++++++ leetcode/0001-two-sum/{ => ruby}/main.rb | 0 leetcode/0001-two-sum/{ => ruby}/test.rb | 0 8 files changed, 98 insertions(+) create mode 100644 leetcode/0001-two-sum/elixir/.formatter.exs create mode 100644 leetcode/0001-two-sum/elixir/.gitignore create mode 100644 leetcode/0001-two-sum/elixir/lib/two_sum.ex create mode 100644 leetcode/0001-two-sum/elixir/mix.exs create mode 100644 leetcode/0001-two-sum/elixir/test/test_helper.exs create mode 100644 leetcode/0001-two-sum/elixir/test/two_sum_test.exs rename leetcode/0001-two-sum/{ => ruby}/main.rb (100%) rename leetcode/0001-two-sum/{ => ruby}/test.rb (100%) diff --git a/leetcode/0001-two-sum/elixir/.formatter.exs b/leetcode/0001-two-sum/elixir/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/leetcode/0001-two-sum/elixir/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/leetcode/0001-two-sum/elixir/.gitignore b/leetcode/0001-two-sum/elixir/.gitignore new file mode 100644 index 0000000..824ce05 --- /dev/null +++ b/leetcode/0001-two-sum/elixir/.gitignore @@ -0,0 +1,26 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +two_sum-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/leetcode/0001-two-sum/elixir/lib/two_sum.ex b/leetcode/0001-two-sum/elixir/lib/two_sum.ex new file mode 100644 index 0000000..6070d2e --- /dev/null +++ b/leetcode/0001-two-sum/elixir/lib/two_sum.ex @@ -0,0 +1,23 @@ +defmodule Solution do + @spec two_sum(nums :: [integer], target :: integer) :: [integer] + def two_sum(nums, target) do + process_num(nums, target) + end + + @spec process_num( + nums :: [integer], + target :: integer, + head_idx :: integer, + indexes :: %{integer => integer} + ) :: [integer] + defp process_num([head | tail], target, head_idx \\ 0, indexes \\ %{}) do + first_idx = indexes[target - head] + + if first_idx != nil do + [first_idx, head_idx] + else + indexes = Map.put(indexes, head, head_idx) + process_num(tail, target, head_idx + 1, indexes) + end + end +end diff --git a/leetcode/0001-two-sum/elixir/mix.exs b/leetcode/0001-two-sum/elixir/mix.exs new file mode 100644 index 0000000..49fb407 --- /dev/null +++ b/leetcode/0001-two-sum/elixir/mix.exs @@ -0,0 +1,28 @@ +defmodule TwoSum.MixProject do + use Mix.Project + + def project do + [ + app: :two_sum, + version: "0.1.0", + elixir: "~> 1.16", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/leetcode/0001-two-sum/elixir/test/test_helper.exs b/leetcode/0001-two-sum/elixir/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/leetcode/0001-two-sum/elixir/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() diff --git a/leetcode/0001-two-sum/elixir/test/two_sum_test.exs b/leetcode/0001-two-sum/elixir/test/two_sum_test.exs new file mode 100644 index 0000000..101d9ba --- /dev/null +++ b/leetcode/0001-two-sum/elixir/test/two_sum_test.exs @@ -0,0 +1,16 @@ +defmodule SolutionTest do + use ExUnit.Case + doctest Solution + + test "example 1" do + assert Solution.two_sum([2, 7, 11, 15], 9) == [0, 1] + end + + test "example 2" do + assert Solution.two_sum([3, 2, 4], 6) == [1, 2] + end + + test "example 3" do + assert Solution.two_sum([3, 3], 6) == [0, 1] + end +end diff --git a/leetcode/0001-two-sum/main.rb b/leetcode/0001-two-sum/ruby/main.rb similarity index 100% rename from leetcode/0001-two-sum/main.rb rename to leetcode/0001-two-sum/ruby/main.rb diff --git a/leetcode/0001-two-sum/test.rb b/leetcode/0001-two-sum/ruby/test.rb similarity index 100% rename from leetcode/0001-two-sum/test.rb rename to leetcode/0001-two-sum/ruby/test.rb