From eb747848b8cea57006d882af439cbd83482f7788 Mon Sep 17 00:00:00 2001 From: Angelika Tyborska Date: Fri, 29 Sep 2023 18:15:49 +0200 Subject: [PATCH 1/2] Fix handling blocks in dna-encoding recursion check --- .../test_suite/dna_encoding.ex | 2 +- .../test_suite/dna_encoding_test.exs | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/elixir_analyzer/test_suite/dna_encoding.ex b/lib/elixir_analyzer/test_suite/dna_encoding.ex index b517257b..1a9b3786 100644 --- a/lib/elixir_analyzer/test_suite/dna_encoding.ex +++ b/lib/elixir_analyzer/test_suite/dna_encoding.ex @@ -104,7 +104,7 @@ defmodule ElixirAnalyzer.TestSuite.DNAEncoding do last_call_in_function_def = case opts[:do] do - {:__block, _, calls} when is_list(calls) -> + {:__block__, _, calls} when is_list(calls) -> List.last(calls) calls when is_list(calls) -> diff --git a/test/elixir_analyzer/test_suite/dna_encoding_test.exs b/test/elixir_analyzer/test_suite/dna_encoding_test.exs index e48eb426..8eea3db3 100644 --- a/test/elixir_analyzer/test_suite/dna_encoding_test.exs +++ b/test/elixir_analyzer/test_suite/dna_encoding_test.exs @@ -44,6 +44,64 @@ defmodule ElixirAnalyzer.ExerciseTest.DNAEncodingTest do end end + test_exercise_analysis "a different reasonable solution", + comments: [] do + defmodule DNA do + def encode_nucleotide(code_point) do + case code_point do + ?\s -> 0b0000 + ?A -> 0b0001 + ?C -> 0b0010 + ?G -> 0b0100 + ?T -> 0b1000 + end + end + + def decode_nucleotide(encoded_code) do + case encoded_code do + 0b0000 -> ?\s + 0b0001 -> ?A + 0b0010 -> ?C + 0b0100 -> ?G + 0b1000 -> ?T + end + end + + def encode(dna) do + do_encode(dna, <<>>) + end + + defp do_encode([], encoded), do: encoded + + defp do_encode([head | tail], encoded) do + encoded_head = encode_nucleotide(head) + do_encode(tail, <>) + end + + def decode(dna) do + do_decode(dna, []) + |> reverse() + end + + defp do_decode(<<>>, list), do: list + + defp do_decode(<>, list) do + decoded_head = decode_nucleotide(head) + do_decode(rest, [decoded_head | list]) + end + + defp reverse(list) do + do_reverse(list, []) + end + + defp do_reverse([], reversed_list), do: reversed_list + + defp do_reverse([head | tail], reversed_list) do + do_reverse(tail, [head | reversed_list]) + end + end + end + test_exercise_analysis "detects the usage of the Enum module", comments: [Constants.dna_encoding_use_recursion()] do [ From 704996da45d1de78c3288056b46aa6ea80008d4d Mon Sep 17 00:00:00 2001 From: Angelika Tyborska Date: Fri, 29 Sep 2023 18:16:21 +0200 Subject: [PATCH 2/2] Upgrade elixir submodule --- elixir | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elixir b/elixir index f9ab2759..f448435b 160000 --- a/elixir +++ b/elixir @@ -1 +1 @@ -Subproject commit f9ab27596f3da28826193f7ebd052777c0159f51 +Subproject commit f448435b1ebfee6f7bcdd330f4cb36fbeb7431b1