# artemkorsakov's solution

## to Scale Generator in the C# Track

Published at May 02 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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 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[] sharps = new string[]
{ "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#" };
private static readonly string[] isSharps = new string[]
{"C", "G", "D", "A", "a", "E", "B", "F#", "e", "b", "f#", "c#", "g#", "d#"};
private static readonly string[] flats = new string[]
{ "A", "Bb", "B", "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab" };
private static readonly string[] isFlats = new string[]
{ "F", "Bb", "Eb", "Ab", "Db", "Gb", "d", "g", "c", "f", "bb", "eb" };

public static string[] Chromatic(string tonic)
{
var scale = GetSharpsOrFlats(tonic);
var idx = GetIndex(scale, tonic);
var list = scale.Skip(idx).ToList();
return list.ToArray();
}

public static string[] Interval(string tonic, string pattern)
{
var scale = Chromatic(tonic);
var list = new List<string>
{
scale[0]
};

int idx = 0;
foreach (var ch in pattern.ToCharArray())
{
idx += GetInterval(ch);
if (scale[idx % scale.Length] == scale[0])
{
return list.ToArray();
}

}

return list.ToArray();
}

private static int GetIndex(string[] scale, string tonic)
{
for (int i = 0; i < scale.Length; i++)
{
if (scale[i].ToLower() == tonic.ToLower())
{
return i;
}
}

return -1;
}

private static string[] GetSharpsOrFlats(string tonic) => isSharps.Contains(tonic) ? sharps : flats;
private static int GetInterval(char ch) => ch == 'm' ? 1 : ch == 'M' ? 2 : 3;
}``````