From 2ff9912e77d348d3387e06f6849a58f7ec70d99c Mon Sep 17 00:00:00 2001 From: Evgeny Garlukovich Date: Wed, 14 Feb 2018 23:04:14 +0300 Subject: [PATCH] Add Elixir example for longest common subsequence --- .../elixir/01_longest_common_subsequence.exs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 09_dynamic_programming/elixir/01_longest_common_subsequence.exs diff --git a/09_dynamic_programming/elixir/01_longest_common_subsequence.exs b/09_dynamic_programming/elixir/01_longest_common_subsequence.exs new file mode 100644 index 0000000..c9bcb66 --- /dev/null +++ b/09_dynamic_programming/elixir/01_longest_common_subsequence.exs @@ -0,0 +1,12 @@ +# `cell` is a map of maps here, e.g. +# cell = %{ +# 0 => %{0 => 0, ...}, +# 1 => %{...}, +# ... +# } + +if String.at(word_a, i) == String.at(word_a, j) do + put_in(cell[i - 1][j - 1], cell[i - 1][j - 1] + 1) +else + put_in(cell[i - 1][j - 1], max(cell[i - 1][j], cell[i][j - 1])) +end