przemyxe0p

przemyxe0p

What do you do if you can not comprehend solution?

What do you do if you’ve done an exercise, it works, it’s not very inefficient and seems concise, then you go to the community solutions, sort by highest-rated user and see this?

defmodule Sublist do
  @doc """
  Returns whether the first list is a sublist or a superlist of the second list
  and if not whether it is equal or unequal to the second list.
  """
  def compare(a, b) when is_list(a) and is_list(b) do
    case {contains?(a, b), contains?(b,a)} do
      {true, true} -> :equal
      {true, false} -> :superlist
      {false, true} -> :sublist
      {false, false} -> :unequal
    end
  end

  # determines if list a contains list b. restore_a is needed to restore
  # already "eaten" members of a, when b couldn't be matched completely
  defp contains?(a, b, current_b \\ :initial, restore_a \\ nil)
  defp contains?(a, b, :initial, nil), do: contains?(a, b, b, nil)
  defp contains?(_, _, [], _), do: true
  defp contains?([], _, _, _), do: false
  defp contains?([x | a], b, [x | c], nil), do: contains?(a, b, c, a)
  defp contains?([x | a], b, [x | c], restore_a), do: contains?(a, b, c, restore_a)
  defp contains?([_ | a], b, _, nil), do: contains?(a, b, b, nil)
  defp contains?(_, b, _, restore_a), do: contains?(restore_a, b, b, nil)
end

To clarify I enjoy creating solutions, and having to think, and play, but there is border.

Marked As Solved

mudasobwa

mudasobwa

Creator of Cure

Create your own wrapper my_contains?/4 which would print the intermediate results and run it for several different inputs.

  def compare(a, b) when is_list(a) and is_list(b) do
    case {my_contains?(a, b), my_contains?(b,a)} do
      {true, true} -> :equal
      {true, false} -> :superlist
      {false, true} -> :sublist
      {false, false} -> :unequal
    end
  end

  defp my_contains?(a, b, current_b \\ :initial, restore_a \\ nil) do
    IO.inspect(a: a, b: b, current_b: current_b, restore_a: restore_a)
    contains?(a, b, current_b, restore_a)
  end

  defp contains?(a, b, current_b \\ :initial, restore_a \\ nil)
  defp contains?(a, b, :initial, nil), do: my_contains?(a, b, b, nil)
  defp contains?(_, _, [], _), do: true
  defp contains?([], _, _, _), do: false
  defp contains?([x | a], b, [x | c], nil), do: my_contains?(a, b, c, a)
  defp contains?([x | a], b, [x | c], restore_a), do: my_contains?(a, b, c, restore_a)
  defp contains?([_ | a], b, _, nil), do: my_contains?(a, b, b, nil)
  defp contains?(_, b, _, restore_a), do: my_contains?(restore_a, b, b, nil)```

Also Liked

derek-zhou

derek-zhou

The code you cited seems fine to me, pretty clever use of tail recursion. I am not sure what do you mean. do you mean:

  • You don’t like this code. Of course you can have a opinion and choose to use your own code. or:
  • You like this code but wish some one to explain to you in digestible pieces. Then you did not say which part you do not understand. Or:
  • You don’t care about this code at all, all you care is to win the competition to provide the best solution. Then it is a matter of skill issue, right?
derek-zhou

derek-zhou

The correct reading order is from the top to bottom. If you want to improve your skill on tail recursion, write with it. Can you implement the enumerating functions in Enum, starting with Enum.reverse/1, with tail recursion? If you can write comfortably in tail recursion, it will click for you.

przemyxe0p

przemyxe0p

  @spec flatten(list) :: list
  def flatten(list), do: fl(list) |> Enum.reverse()

  defp fl(list, acc \\ [])

  defp fl([], acc), do: acc

  defp fl([[_|_] = el | tail], acc), do: fl(el, acc) |> then(&fl(tail, &1))
  defp fl([nil | tail], acc), do: fl(tail, acc)
  defp fl([el | tail], acc),do: fl(tail, [el | acc])

I guess we should reverse at the end, not on intermediate states, and your suggestion would not work (I tested it).

How to tell if my above solution is proper tail-call recursion?

Where Next?

Popular in Challenges Top

sb8244
Note: This topic is to talk about Day 10 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
New
bjorng
This topic is about Day 17 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
sasajuric
Note: This topic is to talk about Day 12 of the Advent of Code. For general discussion about the Advent of Code 2018 and links to topics...
New
sneako
Note by the Moderators: This topic is to talk about the first day of the Advent of Code. For general discussion about the Advent of Code...
New
Aetherus
This topic is about the Advent of Code 2021 - Day 4. Thanks to @bjorng , we now have a new Private Leaderboard. The entry code is: 370...
New
stevensonmt
Trying to get more facility with dynamic programming concepts on Leetcode and having an issue I can’t find a way around. It’s a chutes an...
New
Aetherus
This topic is about Day 16 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
bjorng
This topic is about Day 15 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
bjorng
Note: This topic is to talk about Day 6 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
cblavier
Hi, there :wave: Today, I felt it was way more challenging! I went through part2 thanks to Agent based memoization (without memoization ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement