Skip to content

Latest commit

 

History

History
204 lines (162 loc) · 6.26 KB

drill-patternmatching-replace-nils.livemd

File metadata and controls

204 lines (162 loc) · 6.26 KB

Replacing Nils

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Purpose

This is a guest exercise written by Quentin Crain.

This is a drill exercise:

Drill exercises are meant to provide practise of Elixir's syntax and important language modules so developers can type them out as fast as possible. The problem should not be conceptually difficult to facilitate this goal.

Developers need to commit both Elixir's syntax and core modules/functions to (working) memory so that when writing their code the developer is not slowed down having to remember the syntax or which module function to use. This is analogous to knowing how to spell and the grammar of your (human) language.

In this particular iteration of this problem, the goal is to practise writing/using functions with pattern matching in the functions' heads.

To develop familiarity with functions and pattern matching, you will replace nil values in a list with values from a second list at the same index.

For example:

list1 = [0, nil, 2, 3, nil]
list2 = [:a, :b, :c, :d, :e]

ReplaceNils.replace(list1, list2)
[0, :b, 2, 3, :e]

You can assume the lists are of the same length.

Implement the ReplaceNils module as below:

defmodule ReplaceNils do
  @moduledoc """
  Replace Nils
  """

  @doc """
  replace nil values in the first list with values from the second list in the same position.
  """
  def replace(input1, input2) do
    nil
  end
end

Here are some additional test data to think about:

Testcase 1:
  Input1:   [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  Input2:   [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
  Expected: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Testcase 2:
  Input1:   [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
  Input2:   [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j]
  Expected: [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j]

Testcase 3:
  Input1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  Input2: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Testcase 4:
  Input1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  Input2: [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j]

Testcase 5:
  [1, 2, 3, nil, nil, 6, 7, nil, 9, 10]
  [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j]

Here are some example solutions:

Example Solution 1
defmodule ReplaceNils do
  def replace([i1|input1], [i2|input2]) do
    if input1 == [] do
      if i1 == nil, do: [i2], else: [i1]
    else
      [if i1 == nil, do: [i2], else: [i1]] ++ replace(input1, input2)
    end
  end
end
Example Solution 2
defmodule ReplaceNils do
  def replace([a|as], [b|bs]) do
    case as do
      [] -> [a || b]
      _  -> [a || b] ++ replace(as, bs)
    end
  end
end
Example Solution 3
defmodule ReplaceNils do
  def replace(as, bs, acc \\ [])

  def replace([], [], acc), do: Enum.reverse(acc)
  def replace([nil|as], [b|bs], acc), do: replace(as, bs, [b|acc])
  def replace([a|as], [_b|bs], acc), do: replace(as, bs, [a|acc])
end

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Replacing Nils exercise"
$ git push

We're proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation