Avatar of davearonson

davearonson's solution

to Scale Generator in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a tonic, or starting note, and a set of intervals, generate the musical scale starting with the tonic and following the specified interval pattern.

Scales in Western music are based on the chromatic (12-note) scale.This scale can be expressed as the following group of pitches:

A, A#, B, C, C#, D, D#, E, F, F#, G, G#

A given sharp note (indicated by a #), can also be expressed as the flat of the note above it (indicated by a b), so the chromatic scale can also be written like this:

A, Bb, B, C, Db, D, Eb, E, F, Gb, G, Ab

The major and minor scale and modes are subsets of this twelve-pitch collection. They have seven pitches, and are called diatonic scales. The collection of notes in these scales is written with either sharps or flats, depending on the tonic. Here is a list of which are which:

No Accidentals: C major A minor

Use Sharps: G, D, A, E, B, F# major e, b, f#, c#, g#, d# minor

Use Flats: F, Bb, Eb, Ab, Db, Gb major d, g, c, f, bb, eb minor

The diatonic scales, and all other scales that derive from the chromatic scale, are built upon intervals. An interval is the space between two pitches.

The simplest interval is between two adjacent notes, and is called a "half step", or "minor second" (sometimes written as a lower-case "m"). The interval between two notes that have an interceding note is called a "whole step" or "major second" (written as an upper-case "M"). The diatonic scales are built using only these two intervals between adjacent notes.

Non-diatonic scales can contain the same letter twice, and can contain other intervals. Sometimes they may be smaller than usual (diminished, written "D"), or larger (augmented, written "A"). Intervals larger than an augmented second have other names.

Here is a table of pitches with the names of their interval distance from the tonic (A).

A A# B C C# D D# E F F# G G# A
Unison Min 2nd Maj 2nd Min 3rd Maj 3rd Per 4th Tritone Per 5th Min 6th Maj 6th Min 7th Maj 7th Octave
Dim 3rd Aug 2nd Dim 4th Aug 4th Dim 5th Aug 5th Dim 7th Aug 6th Dim 8ve
Dim 5th

Running tests

Execute the tests with:

$ elixir scale_generator_test.exs

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

For more detailed information about the Elixir track, please see the help page.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

scale_generator_test.exs

if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("scale_generator.exs", __DIR__)
end

ExUnit.start()
ExUnit.configure(trace: true, exclude: :pending)

defmodule ScaleGeneratorTest do
  use ExUnit.Case

  @major_scale_pattern "MMmMMMm"
  @minor_scale_pattern "MmMMmMM"
  @dorian_scale_pattern "MmMMMmM"
  @mixolydian_scale_pattern "MMmMMmM"
  @lydian_scale_pattern "MMMmMMm"
  @phrygian_scale_pattern "mMMMmMM"
  @locrian_scale_pattern "mMMmMMM"
  @harmonic_minor_scale_pattern "MmMMmAm"
  @melodic_minor_scale_pattern "MmMMMMm"
  @octatonic_scale_pattern "MmMmMmMm"
  @hexatonic_scale_pattern "MMMMMM"
  @pentatonic_scale_pattern "MMAMA"
  @enigmatic_scale_pattern "mAMMMmm"

  describe "step to next note" do
    # @tag :pending
    test "with half-tone interval" do
      assert ScaleGenerator.step(~w(C C# D D# E F F# G G# A A# B), "C", "m") == "C#"
    end

    @tag :pending
    test "with full tone interval" do
      assert ScaleGenerator.step(~w(C C# D D# E F F# G G# A A# B), "C", "M") == "D"
    end

    @tag :pending
    test "with accidental interval" do
      assert ScaleGenerator.step(~w(C C# D D# E F F# G G# A A# B), "C", "A") == "D#"
    end
  end

  describe "generate chromatic scale" do
    @tag :pending
    test "starting with A" do
      assert ScaleGenerator.chromatic_scale("A") == ~w(A A# B C C# D D# E F F# G G# A)
    end

    @tag :pending
    test "starting with C" do
      assert ScaleGenerator.chromatic_scale("C") == ~w(C C# D D# E F F# G G# A A# B C)
    end

    @tag :pending
    test "starting with G" do
      assert ScaleGenerator.chromatic_scale("G") == ~w(G G# A A# B C C# D D# E F F# G)
    end

    @tag :pending
    test "works with with lowercase notes" do
      assert ScaleGenerator.chromatic_scale("f#") == ~w(F# G G# A A# B C C# D D# E F F#)
    end
  end

  describe "generate flat chromatic scale" do
    @tag :pending
    test "starting with A" do
      assert ScaleGenerator.flat_chromatic_scale("A") == ~w(A Bb B C Db D Eb E F Gb G Ab A)
    end

    @tag :pending
    test "starting with C" do
      assert ScaleGenerator.flat_chromatic_scale("C") == ~w(C Db D Eb E F Gb G Ab A Bb B C)
    end

    @tag :pending
    test "starting with G" do
      assert ScaleGenerator.flat_chromatic_scale("G") == ~w(G Ab A Bb B C Db D Eb E F Gb G)
    end

    @tag :pending
    test "works with with lowercase notes" do
      assert ScaleGenerator.flat_chromatic_scale("Gb") == ~w(Gb G Ab A Bb B C Db D Eb E F Gb)
    end
  end

  describe "find chromatic scale for flat tonics" do
    @tag :pending
    test "using F" do
      assert ScaleGenerator.find_chromatic_scale("F") == ~w(F Gb G Ab A Bb B C Db D Eb E F)
    end

    @tag :pending
    test "using Bb" do
      assert ScaleGenerator.find_chromatic_scale("Bb") == ~w(Bb B C Db D Eb E F Gb G Ab A Bb)
    end

    @tag :pending
    test "using Eb" do
      assert ScaleGenerator.find_chromatic_scale("Eb") == ~w(Eb E F Gb G Ab A Bb B C Db D Eb)
    end

    @tag :pending
    test "using Ab" do
      assert ScaleGenerator.find_chromatic_scale("Ab") == ~w(Ab A Bb B C Db D Eb E F Gb G Ab)
    end

    @tag :pending
    test "using Db" do
      assert ScaleGenerator.find_chromatic_scale("Db") == ~w(Db D Eb E F Gb G Ab A Bb B C Db)
    end

    @tag :pending
    test "using Gb" do
      assert ScaleGenerator.find_chromatic_scale("Gb") == ~w(Gb G Ab A Bb B C Db D Eb E F Gb)
    end

    @tag :pending
    test "using d" do
      assert ScaleGenerator.find_chromatic_scale("d") == ~w(D Eb E F Gb G Ab A Bb B C Db D)
    end

    @tag :pending
    test "using g" do
      assert ScaleGenerator.find_chromatic_scale("g") == ~w(G Ab A Bb B C Db D Eb E F Gb  G)
    end

    @tag :pending
    test "using c" do
      assert ScaleGenerator.find_chromatic_scale("c") == ~w(C Db D Eb E F Gb G Ab A Bb B  C)
    end

    @tag :pending
    test "using f" do
      assert ScaleGenerator.find_chromatic_scale("f") == ~w(F Gb G Ab A Bb B C Db D Eb E F)
    end

    @tag :pending
    test "using bb" do
      assert ScaleGenerator.find_chromatic_scale("bb") == ~w(Bb B C Db D Eb E F Gb G Ab A Bb)
    end

    @tag :pending
    test "using eb" do
      assert ScaleGenerator.find_chromatic_scale("eb") == ~w(Eb E F Gb G Ab A Bb B C Db D Eb)
    end
  end

  describe "find chromatic scale for non-flat tonics" do
    @tag :pending
    test "using A" do
      assert ScaleGenerator.find_chromatic_scale("A") == ~w(A A# B C C# D D# E F F# G G# A)
    end

    @tag :pending
    test "using A#" do
      assert ScaleGenerator.find_chromatic_scale("A#") == ~w(A# B C C# D D# E F F# G G# A A#)
    end

    @tag :pending
    test "using B" do
      assert ScaleGenerator.find_chromatic_scale("B") == ~w(B C C# D D# E F F# G G# A A# B)
    end

    @tag :pending
    test "using C" do
      assert ScaleGenerator.find_chromatic_scale("C") == ~w(C C# D D# E F F# G G# A A# B C)
    end

    @tag :pending
    test "using C#" do
      assert ScaleGenerator.find_chromatic_scale("C#") == ~w(C# D D# E F F# G G# A A# B C C#)
    end

    @tag :pending
    test "using D" do
      assert ScaleGenerator.find_chromatic_scale("D") == ~w(D D# E F F# G G# A A# B C C# D)
    end

    @tag :pending
    test "using D#" do
      assert ScaleGenerator.find_chromatic_scale("D#") == ~w(D# E F F# G G# A A# B C C# D D#)
    end

    @tag :pending
    test "using E" do
      assert ScaleGenerator.find_chromatic_scale("E") == ~w(E F F# G G# A A# B C C# D D# E)
    end

    @tag :pending
    test "using F#" do
      assert ScaleGenerator.find_chromatic_scale("F#") == ~w(F# G G# A A# B C C# D D# E F F#)
    end

    @tag :pending
    test "using G" do
      assert ScaleGenerator.find_chromatic_scale("G") == ~w(G G# A A# B C C# D D# E F F# G)
    end

    @tag :pending
    test "using G#" do
      assert ScaleGenerator.find_chromatic_scale("G#") == ~w(G# A A# B C C# D D# E F F# G G#)
    end
  end

  describe "generate scale from tonic and pattern" do
    @tag :pending
    test "C Major scale" do
      assert ScaleGenerator.scale("C", @major_scale_pattern) == ~w(C D E F G A B C)
    end

    @tag :pending
    test "G Major scale" do
      assert ScaleGenerator.scale("G", @major_scale_pattern) == ~w(G A B C D E F# G)
    end

    @tag :pending
    test "f# minor scale" do
      assert ScaleGenerator.scale("f#", @minor_scale_pattern) == ~w(F# G# A B C# D E F#)
    end

    @tag :pending
    test "b flat minor scale" do
      assert ScaleGenerator.scale("bb", @minor_scale_pattern) == ~w(Bb C Db Eb F Gb Ab Bb)
    end

    @tag :pending
    test "D Dorian scale" do
      assert ScaleGenerator.scale("d", @dorian_scale_pattern) == ~w(D E F G A B C D)
    end

    @tag :pending
    test "E flat Mixolydian scale" do
      assert ScaleGenerator.scale("Eb", @mixolydian_scale_pattern) == ~w(Eb F G Ab Bb C Db Eb)
    end

    @tag :pending
    test "a Lydian scale" do
      assert ScaleGenerator.scale("a", @lydian_scale_pattern) == ~w(A B C# D# E F# G# A)
    end

    @tag :pending
    test "e Phrygian scale" do
      assert ScaleGenerator.scale("e", @phrygian_scale_pattern) == ~w(E F G A B C D E)
    end

    @tag :pending
    test "g Locrian scale" do
      assert ScaleGenerator.scale("g", @locrian_scale_pattern) == ~w(G Ab Bb C Db Eb F G)
    end

    @tag :pending
    test "d Harmonic minor scale" do
      assert ScaleGenerator.scale("d", @harmonic_minor_scale_pattern) == ~w(D E F G A Bb Db D)
    end

    @tag :pending
    test "C Melodic minor scale" do
      assert ScaleGenerator.scale("C", @melodic_minor_scale_pattern) == ~w(C D D# F G A B C)
    end

    @tag :pending
    test "C Octatonic scale" do
      assert ScaleGenerator.scale("C", @octatonic_scale_pattern) == ~w(C D D# F F# G# A B C)
    end

    @tag :pending
    test "D flat Hexatonic scale" do
      assert ScaleGenerator.scale("Db", @hexatonic_scale_pattern) == ~w(Db Eb F G A B Db)
    end

    @tag :pending
    test "A Pentatonic scale" do
      assert ScaleGenerator.scale("A", @pentatonic_scale_pattern) == ~w(A B C# E F# A)
    end

    @tag :pending
    test "G Enigmatic scale" do
      assert ScaleGenerator.scale("G", @enigmatic_scale_pattern) == ~w(G G# B C# D# F F# G)
    end
  end
end
defmodule ScaleGenerator do
  @doc """
  Find the note for a given interval (`step`) in a `scale` after the `tonic`.

  "m": one semitone
  "M": two semitones (full tone)
  "A": augmented second (three semitones)

  Given the `tonic` "D" in the `scale` (C C# D D# E F F# G G# A A# B C), you
  should return the following notes for the given `step`:

  "m": D#
  "M": E
  "A": F
  """
  @spec step(scale :: list(String.t()), tonic :: String.t(), step :: String.t()) :: list(String.t())
  def step(scale, tonic, step) do
    scale
    |> Enum.at(rem(find_value(scale, tonic) + step_size(step), 12))
  end

  @doc """
  The chromatic scale is a musical scale with thirteen pitches, each a semitone
  (half-tone) above or below another.

  Notes with a sharp (#) are a semitone higher than the note below them, where
  the next letter note is a full tone except in the case of B and E, which have
  no sharps.

  Generate these notes, starting with the given `tonic` and wrapping back
  around to the note before it, ending with the tonic an octave higher than the
  original. If the `tonic` is lowercase, capitalize it.

  "C" should generate: ~w(C C# D D# E F F# G G# A A# B C)
  """
  @spec chromatic_scale(tonic :: String.t()) :: list(String.t())
  def chromatic_scale(tonic \\ "C") do
    generate_scale(~w(C C# D D# E F F# G G# A A# B),
                   tonic |> String.upcase)
  end

  @doc """
  Sharp notes can also be considered the flat (b) note of the tone above them,
  so the notes can also be represented as:

  A Bb B C Db D Eb E F Gb G Ab

  Generate these notes, starting with the given `tonic` and wrapping back
  around to the note before it, ending with the tonic an octave higher than the
  original. If the `tonic` is lowercase, capitalize it.

  "C" should generate: ~w(C Db D Eb E F Gb G Ab A Bb B C)
  """
  @spec flat_chromatic_scale(tonic :: String.t()) :: list(String.t())
  def flat_chromatic_scale(tonic \\ "C") do
    generate_scale(~w(C Db D Eb E F Gb G Ab A Bb B),
                   tonic |> upcase_only_first_letter)
  end

  @doc """
  Certain scales will require the use of the flat version, depending on the
  `tonic` (key) that begins them, which is C in the above examples.

  For any of the following tonics, use the flat chromatic scale:

  F Bb Eb Ab Db Gb d g c f bb eb

  For all others, use the regular chromatic scale.
  """
  @spec find_chromatic_scale(tonic :: String.t()) :: list(String.t())
  def find_chromatic_scale(tonic) do
    if ~w(F Bb Eb Ab Db Gb d g c f bb eb) |> find_value(tonic) do
      flat_chromatic_scale(tonic)
    else
      chromatic_scale(tonic)
    end
  end

  @doc """
  The `pattern` string will let you know how many steps to make for the next
  note in the scale.

  For example, a C Major scale will receive the pattern "MMmMMMm", which
  indicates you will start with C, make a full step over C# to D, another over
  D# to E, then a semitone, stepping from E to F (again, E has no sharp). You
  can follow the rest of the pattern to get:

  C D E F G A B C
  """
  @spec scale(tonic :: String.t(), pattern :: String.t()) :: list(String.t())
  def scale(tonic, pattern) do
    [(tonic |> upcase_only_first_letter)|
     apply_pattern(String.graphemes(pattern), find_chromatic_scale(tonic), [])]
  end


  defp apply_pattern([head|tail], scale, acc) do
    idx = step_size(head)
    apply_pattern(tail, scale |> Enum.drop(idx), [scale |> Enum.at(idx)|acc])
  end
  defp apply_pattern([], _scale, acc), do: acc |> Enum.reverse

  defp find_value(list, value) do
    Enum.find_index(list , fn(element) -> element == value end)
  end

  defp generate_scale(notes, tonic) do
    tonic_index = notes |> find_value(tonic)
    (notes |> Enum.drop(tonic_index)) ++
      (notes |> Enum.take(tonic_index)) ++
      [tonic]
  end

  defp step_size(step), do: find_value(~w(m M A), step) + 1

  defp upcase_only_first_letter(str) do
    str
    |> String.graphemes
    |> (fn([h|t]) -> [String.upcase(h) | t] end).()
    |> Enum.join
  end

end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?