Avatar of josephfash11

josephfash11's solution

to Scale Generator in the C# Track

Published at Mar 19 2019 · 0 comments
Instructions
Test suite
Solution

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 Sharps or Flats: 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 other intervals. An "augmented first" interval, written "A", has two interceding notes (e.g., from A to C or Db to E). There are also smaller and larger intervals, but they will not figure into this exercise.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit ScaleGenerator.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

ScaleGeneratorTest.cs

// This file was auto-generated based on version 2.0.0 of the canonical data.

using Xunit;

public class ScaleGeneratorTest
{
    [Fact]
    public void Chromatic_scale_with_sharps()
    {
        var expected = new[] { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
        Assert.Equal(expected, ScaleGenerator.Chromatic("C"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Chromatic_scale_with_flats()
    {
        var expected = new[] { "F", "Gb", "G", "Ab", "A", "Bb", "B", "C", "Db", "D", "Eb", "E" };
        Assert.Equal(expected, ScaleGenerator.Chromatic("F"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Simple_major_scale()
    {
        var expected = new[] { "C", "D", "E", "F", "G", "A", "B" };
        Assert.Equal(expected, ScaleGenerator.Interval("C", "MMmMMMm"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Major_scale_with_sharps()
    {
        var expected = new[] { "G", "A", "B", "C", "D", "E", "F#" };
        Assert.Equal(expected, ScaleGenerator.Interval("G", "MMmMMMm"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Major_scale_with_flats()
    {
        var expected = new[] { "F", "G", "A", "Bb", "C", "D", "E" };
        Assert.Equal(expected, ScaleGenerator.Interval("F", "MMmMMMm"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Minor_scale_with_sharps()
    {
        var expected = new[] { "F#", "G#", "A", "B", "C#", "D", "E" };
        Assert.Equal(expected, ScaleGenerator.Interval("f#", "MmMMmMM"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Minor_scale_with_flats()
    {
        var expected = new[] { "Bb", "C", "Db", "Eb", "F", "Gb", "Ab" };
        Assert.Equal(expected, ScaleGenerator.Interval("bb", "MmMMmMM"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Dorian_mode()
    {
        var expected = new[] { "D", "E", "F", "G", "A", "B", "C" };
        Assert.Equal(expected, ScaleGenerator.Interval("d", "MmMMMmM"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Mixolydian_mode()
    {
        var expected = new[] { "Eb", "F", "G", "Ab", "Bb", "C", "Db" };
        Assert.Equal(expected, ScaleGenerator.Interval("Eb", "MMmMMmM"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Lydian_mode()
    {
        var expected = new[] { "A", "B", "C#", "D#", "E", "F#", "G#" };
        Assert.Equal(expected, ScaleGenerator.Interval("a", "MMMmMMm"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Phrygian_mode()
    {
        var expected = new[] { "E", "F", "G", "A", "B", "C", "D" };
        Assert.Equal(expected, ScaleGenerator.Interval("e", "mMMMmMM"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Locrian_mode()
    {
        var expected = new[] { "G", "Ab", "Bb", "C", "Db", "Eb", "F" };
        Assert.Equal(expected, ScaleGenerator.Interval("g", "mMMmMMM"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Harmonic_minor()
    {
        var expected = new[] { "D", "E", "F", "G", "A", "Bb", "Db" };
        Assert.Equal(expected, ScaleGenerator.Interval("d", "MmMMmAm"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Octatonic()
    {
        var expected = new[] { "C", "D", "D#", "F", "F#", "G#", "A", "B" };
        Assert.Equal(expected, ScaleGenerator.Interval("C", "MmMmMmMm"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Hexatonic()
    {
        var expected = new[] { "Db", "Eb", "F", "G", "A", "B" };
        Assert.Equal(expected, ScaleGenerator.Interval("Db", "MMMMMM"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Pentatonic()
    {
        var expected = new[] { "A", "B", "C#", "E", "F#" };
        Assert.Equal(expected, ScaleGenerator.Interval("A", "MMAMA"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Enigmatic()
    {
        var expected = new[] { "G", "G#", "B", "C#", "D#", "F", "F#" };
        Assert.Equal(expected, ScaleGenerator.Interval("G", "mAMMMmm"));
    }
}
using System;
using System.Collections.Generic;
using System.Linq;

public static class ScaleGenerator
{
    private static readonly string[] SharpNotes = { "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#" };
    private static readonly string[] FlatNotes = { "A", "Bb", "B", "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab" };
    private static readonly string[] FlatKeys = { "F", "Bb", "Eb", "Ab", "Db", "Gb", "d", "g", "c", "f", "bb", "eb" };
    private enum Intervals { m = 1, M, A };

    public static string[] Chromatic(string tonic)
    {
        return Interval(tonic, "mmmmmmmmmmmm");
    }

    public static string[] Interval(string tonic, string pattern)
    {
        List<string> result = new List<string>();

        List<string> correctScale = new List<string>();
        if (FlatKeys.Contains(tonic)) correctScale = FlatNotes.ToList();
        else correctScale = SharpNotes.ToList();

        int baseNote;

        if (tonic.Length == 1)
        {
            baseNote = correctScale.IndexOf(tonic.ToUpper());
        }
        else
        {
            char[] temp = new char[2];
            temp[0] = Convert.ToChar(tonic.Substring(0, 1).ToUpper());
            temp[1] = tonic[1];
            baseNote = correctScale.IndexOf(new String(temp));
        }

        result.Add(correctScale[baseNote]);
        for (int i = 0; i < pattern.Length - 1; i++)
        {
            baseNote = baseNote + (int)Enum.Parse(typeof(Intervals), Convert.ToString(pattern[i]));
            if (baseNote > 11) baseNote -= 12;
            result.Add(correctScale[baseNote]);
        }
        return result.ToArray();
    }
}

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?