ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

## to Scale Generator in the C# Track

Published at Jun 21 2020 · 1 comment
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.

### ScaleGeneratorTests.cs

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

using Xunit;

public class ScaleGeneratorTests
{
[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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this 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 this Skip property to run this test")]
public void Hexatonic()
{
var expected = new[] { "Db", "Eb", "F", "G", "A", "B" };
Assert.Equal(expected, ScaleGenerator.Interval("Db", "MMMMMM"));
}

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

[Fact(Skip = "Remove this Skip property to run this 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
{
public static string[] Interval(string tonic, string pattern)
{
tonic = Normalize(tonic);
var result = new string[pattern.Length];
var result_idx = 0;

var chromatic = Chromatic(tonic);
int index = Array.IndexOf(chromatic, tonic);

for (int i = 0; i < pattern.Length; ++i)
{
result[result_idx++] = chromatic[index];
index = NextIndex(index, chromatic.Length, pattern[i]);
}

return result;
}

private static string[] Sharps =
{ "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };

private static string[] Flats =
{ "F", "Gb", "G", "Ab", "A", "Bb", "B", "C", "Db", "D", "Eb", "E" };

public static string[] Chromatic(string tonic)
{
switch(tonic)
{
case "E":
case "F":
case "D":
return Flats;
case "A":
case "C":
case "G":
return Sharps;
default:
{
if (tonic.Length == 2 && tonic[1] == 'b')
return Flats;
if (tonic.Length == 2 && tonic[1] == '#')
return Sharps;

throw new NotImplementedException("");
}
}
}

private static int NextIndex(int index, int length, char pattern)
{
var step = pattern switch {
'm' => 1,
'M' => 2,
'A' => 3,
_ => throw new ArgumentException(),
};

return (index + step) % length;
}

private static string Normalize(string tonic)
{
return string.Concat(
tonic.Select((c, i) => (i == 0) ? char.ToUpper(c) : c));
}

}``````