Powered by AppSignal & Oban Pro

Caesar Cypher

exercises/caesar_cypher.livemd

Caesar Cypher

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

Navigation

Return Home Report An Issue

Caesar Cypher

You’re going to create a Caesar Cypher. A cypher alters characters in order to make secret messages. The Caesar cypher is a substitution cypher named after the Roman emperor Julius Caesar.

A Caesar cypher shifts characters by some key. For example, if the key is 1 then each character shifts over by one, so "a" becomes "b", "b" becomes "c" and so on.

Create a CaesarCypher.encode/1 function which shifts each letter in a string by a key. The default key should be 1.

CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz")
"bcdefghijklmnopqrstuvwxyza"

You only need to handle lowercase letters.

Any Key

Create a CaesarCypher.encode/2 function which encodes the string by a provided key. For example, if the key is 2 then each letter shifts over by two, so "a" becomes "c", "b" becomes "d" and so on.

CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz", 2)
"cdefghijklmnopqrstuvwxyzab"

Example Solution

defmodule CaesarCypher do
 def encode(string) do
    encode(string, 1)
  end

  def encode(string, key) do
    String.to_charlist(string)
    |> Enum.map(fn char ->
      overflow = char + key > ?z
      if overflow do
        ?a + rem(char + key, ?z) - 1
      else
        char + key
      end
    end)
    |> List.to_string()
  end
end

Implement the CaesarCypher module as documented.

defmodule CaesarCypher do
  @moduledoc """
  Documentation for `CaesarCypher`
  """

  @doc """
  Encode a string using the caesar cypher.
  Shift every letter over by one.

  ## Examples

    iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz")
    "bcdefghijklmnopqrstuvwxyza"

  Encoding should work on any string

    iex> CaesarCypher.encode("hello")
    "ifmmp"
  """
  def encode(string) do
  end

  @doc """
  Encode a string using the caesar cypher and a provided key.
  Shift every letter over by the provided key.

  ## Examples

    iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz", 1)
    "bcdefghijklmnopqrstuvwxyza"

    iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz", 2)
    "cdefghijklmnopqrstuvwxyzab"

    iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz", 14)
    "opqrstuvwxyzabcdefghijklmn"

  Encoding should work on any string.

    iex> CaesarCypher.encode("hello", 1)
    "ifmmp"
  """
  def encode(string, key) do
  end
end

Mark As Completed

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

save_name =
  case Path.basename(__DIR__) do
    "reading" -> "caesar_cypher_reading"
    "exercises" -> "caesar_cypher_exercise"
  end

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

default = Map.get(existing_progress, save_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, save_name, completed), pretty: true)
    )
  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 -b caesar-cypher-exercise
$ git add .
$ git commit -m "finish caesar cypher exercise"
$ git push origin caesar-cypher-exercise

Create a pull request from your caesar-cypher-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 teacher by including @BrooklinJazz in your PR description to get feedback. You (or your teacher) 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.

Up Next

Previous Next
Email Validation Rollable Expressions