Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Drill: Replace `nil`s In A List With Elements From A 2nd List

deprecated_drill-enum2-replace-nils.livemd

Drill: Replace nils In A List With Elements From A 2nd List

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

Navigation

Return Home Report An Issue

Purpose

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.

To develop familiarity with comprehensions, 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
  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(input1, input2) do
    for index <- 0..length(input1)-1 do
      if Enum.at(input1, index) == nil do
        Enum.at(input2, index)
      else
        Enum.at(input1, index)
      end
    end
  end
end

Example Solution 2

defmodule ReplaceNils do
  def replace(input1, input2) do
    for {element, index} <- Enum.with_index(input1) do
      element || Enum.at(input2, index)
    end
  end
end

Example Solution 3

defmodule ReplaceNils do
  def replace(as, bs) do
    for {a, b} <- Enum.zip(as, bs) do
      a || b
    end
  end
end

Mark As Completed

file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")

progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()

default = Map.get(existing_progress, file_name, false)

form =
  Kino.Control.form(
    [
      completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
    ],
    report_changes: true
  )

Task.async(fn ->
  for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
    File.write!(progress_path, Jason.encode!(Map.put(existing_progress, file_name, completed)))
  end
end)

form

Commit Your Progress

Run the following in your command line from the curriculum folder to track and save your progress in a Git commit. Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.

$ git checkout solutions
$ git checkout -b drill-enum2-replace-nils-exercise
$ git add .
$ git commit -m "finish drill-enum2-replace-nils exercise"
$ git push origin drill-enum2-replace-nils-exercise

Create a pull request from your drill-enum2-replace-nils-exercise branch to your solutions branch. Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.

DockYard Academy Students Only:

Notify your instructor by including @BrooklinJazz in your PR description to get feedback. You (or your instructor) may merge your PR into your solutions branch after review.

If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.