 # hamberge's solution

## to Scale Generator in the C# Track

Published at Aug 02 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.Linq;
using System.Collections.Generic;

public static class ScaleGenerator
{
private enum SharpOrFlat { sharp, flat, none };

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

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

private enum MajorOrMinor { major, minor };

private static class Note
{
public static MajorOrMinor GetMajorOrMinor(string note)
{
return Char.IsUpper(note) ? MajorOrMinor.major : MajorOrMinor.minor;
}
public static string ToString(int noteValue, SharpOrFlat asSharpOrFlat)
{
return (asSharpOrFlat == SharpOrFlat.sharp) ? notesAsSharp[noteValue] : notesAsFlat[noteValue];
}
public static bool IsSharp(string note)
{
note = NoteFormat(note);
if (notesAsSharp.Contains(note))
{
return true;
}
return false;
}
public static bool IsFlat(string note)
{
note = NoteFormat(note);
if (notesAsFlat.Contains(note))
{
return true;
}
return false;
}
public static SharpOrFlat GetSharpOrFlat(string note)
{
note = NoteFormat(note);
if (IsFlat(note)) return SharpOrFlat.flat;
if (IsSharp(note)) return SharpOrFlat.sharp;
}
public static int GetNoteValue(string note)
{
note = NoteFormat(note);
if(IsFlat(note))
{
return Array.IndexOf(notesAsFlat, note);
}
if(IsSharp(note))
{
return Array.IndexOf(notesAsSharp, note);
}
}
public static string NoteFormat(string note)
{
char[] noteChars = note.ToCharArray();
noteChars = char.ToUpper(noteChars);
return new string(noteChars);
}
}

private static SharpOrFlat[] majorScaleSharpOrFlat =
{
SharpOrFlat.sharp, //A
SharpOrFlat.flat, //Bb
SharpOrFlat.sharp, //B
SharpOrFlat.sharp, //C
SharpOrFlat.flat, //Db
SharpOrFlat.sharp, //D
SharpOrFlat.flat, //Eb
SharpOrFlat.sharp, //E
SharpOrFlat.flat, //F
SharpOrFlat.flat, //Gb
SharpOrFlat.sharp, //G
SharpOrFlat.flat, //Ab
};

private static SharpOrFlat[] minorScaleSharpOrFlat =
{
SharpOrFlat.sharp, //A
SharpOrFlat.flat, //Bb
SharpOrFlat.sharp, //B
SharpOrFlat.sharp, //C
SharpOrFlat.sharp, //C#
SharpOrFlat.flat, //D
SharpOrFlat.flat, //Eb
SharpOrFlat.sharp, //E
SharpOrFlat.flat, //F
SharpOrFlat.sharp, //F#
SharpOrFlat.flat, //G
SharpOrFlat.sharp //G#
};

private static Dictionary<string, int> intervalValues = new Dictionary<string, int>
{
{"M", 2 },
{"m", 1 },
{"A", 3 }
};

//case of the first note defines whether major or minor
public static string[] Chromatic(string tonic)
{
int noteValue = Note.GetNoteValue(tonic);
SharpOrFlat sharpOrFlat = majorScaleSharpOrFlat[noteValue];
string[] retVal = new string;
for(int i = 0; i < 12; i++)
{
retVal[i] = Note.ToString((i + noteValue) % 12, sharpOrFlat);
}
return retVal;
}

public static string[] Interval(string tonic, string pattern)
{
MajorOrMinor majorOrMinor = Note.GetMajorOrMinor(tonic);
int baseNoteValue = Note.GetNoteValue(tonic);
SharpOrFlat scaleSharpOrFlat =
(majorOrMinor == MajorOrMinor.major) ? majorScaleSharpOrFlat[baseNoteValue] : minorScaleSharpOrFlat[baseNoteValue];
string[] retVal = new string[pattern.Length];
retVal = Note.NoteFormat(tonic);
for(int i = 0; i < pattern.Length-1; i++)
{
baseNoteValue = (baseNoteValue + intervalValues[pattern[i].ToString()]) % 12;
retVal[i + 1] = Note.ToString(baseNoteValue, scaleSharpOrFlat);
}
return retVal;
}
}``````