Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

étude 6-3 and appending to a list #42

Open
miguelcobain opened this issue May 10, 2016 · 0 comments
Open

étude 6-3 and appending to a list #42

miguelcobain opened this issue May 10, 2016 · 0 comments

Comments

@miguelcobain
Copy link

In Chapter 6 there is an interlude referring to the problem of adding to the tail of a list.
To work around it the solution presented is to add to that tail and reverse afterwards.

I think a better solution would be to use the ++ operator ti add to the tail.
Solution to étude 6-3:

defmodule Teeth do

  def alert(depths) do
    alert(depths, 1, [])
  end

  def alert([], _, result) do
    result
  end

  def alert([tooth | teeth], number, result) do
    cond do
      Stats.maximum(tooth) >= 4 -> alert(teeth, number + 1, result ++ [number])
      true -> alert(teeth, number + 1, result)
    end
  end

  def pocket_depths do
    [[0], [2,2,1,2,2,1], [3,1,2,3,2,3],
    [3,1,3,2,1,2], [3,2,3,2,2,1], [2,3,1,2,1,1],
    [3,1,3,2,3,2], [3,3,2,1,3,1], [4,3,3,2,3,3],
    [3,1,1,3,2,2], [4,3,4,3,2,3], [2,3,1,3,2,2],
    [1,2,1,1,3,2], [1,2,2,3,2,3], [1,3,2,1,3,3], [0],
    [3,2,3,1,1,2], [2,2,1,1,3,2], [2,1,1,1,1,2],
    [3,3,2,1,1,3], [3,1,3,2,3,2], [3,3,1,2,3,3],
    [1,2,2,3,3,3], [2,2,3,2,3,3], [2,2,2,4,3,4],
    [3,4,3,3,3,4], [1,1,2,3,1,2], [2,2,3,2,1,3],
    [3,4,2,4,4,3], [3,3,2,1,2,3], [2,2,2,2,3,3],
    [3,2,3,2,3,2]]
  end

end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant