🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of jeffreyfultonca

jeffreyfultonca's solution

to Scale Generator in the Swift Track

Published at Dec 27 2020 · 0 comments
Test suite

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


Go through the project setup instructions for Xcode using Swift:


Submitting Incomplete Solutions

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


import XCTest
@testable import ScaleGeneratorTests



import XCTest
@testable import ScaleGenerator

class ScaleGeneratorTests: XCTestCase {

    func testNamingScale() {
        let chromatic = ScaleGenerator(tonic: "c", scaleName: "chromatic")
        XCTAssertEqual(chromatic.name, "C chromatic")

    func testChromaticScale() {
        let chromatic = ScaleGenerator(tonic: "C", scaleName: "chromatic")
        XCTAssertEqual(chromatic.pitches(), ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"])

    func testAnotherChromaticScale() {
        let chromatic = ScaleGenerator(tonic: "F", scaleName: "chromatic")
        XCTAssertEqual(chromatic.pitches(), ["F", "Gb", "G", "Ab", "A", "Bb", "B", "C", "Db", "D", "Eb", "E"])

    func testNamingMajorScale() {
        let major = ScaleGenerator(tonic: "G", scaleName: "major", pattern: "MMmMMMm")
        XCTAssertEqual(major.name, "G major")

    func testMajorScale() {
        let major = ScaleGenerator(tonic: "C", scaleName: "major", pattern: "MMmMMMm")
        XCTAssertEqual(major.pitches(), ["C", "D", "E", "F", "G", "A", "B"])

    func testAnotherMajorScale() {
        let major = ScaleGenerator(tonic: "G", scaleName: "major", pattern: "MMmMMMm")
        XCTAssertEqual(major.pitches(), ["G", "A", "B", "C", "D", "E", "F#"])

    func testMinorScale() {
        let minor = ScaleGenerator(tonic: "f#", scaleName: "minor", pattern: "MmMMmMM")
        XCTAssertEqual(minor.pitches(), ["F#", "G#", "A", "B", "C#", "D", "E"])

    func testAnotherMinorScale() {
        let minor = ScaleGenerator(tonic: "bb", scaleName: "minor", pattern: "MmMMmMM")
        XCTAssertEqual(minor.pitches(), ["Bb", "C", "Db", "Eb", "F", "Gb", "Ab"])

    func testDorianMode() {
        let dorian = ScaleGenerator(tonic: "d", scaleName: "dorian", pattern: "MmMMMmM")
        XCTAssertEqual(dorian.pitches(), ["D", "E", "F", "G", "A", "B", "C"])

    func testMixolydianMode() {
        let mixolydian = ScaleGenerator(tonic: "Eb", scaleName: "mixolydian", pattern: "MMmMMmM")
        XCTAssertEqual(mixolydian.pitches(), ["Eb", "F", "G", "Ab", "Bb", "C", "Db"])

    func testLydianMode() {
        let lydian = ScaleGenerator(tonic: "a", scaleName: "lydian", pattern: "MMMmMMm")
        XCTAssertEqual(lydian.pitches(), ["A", "B", "C#", "D#", "E", "F#", "G#"])

    func testPhrygianMode() {
        let phrygian = ScaleGenerator(tonic: "e", scaleName: "phrygian", pattern: "mMMMmMM")
        XCTAssertEqual(phrygian.pitches(), ["E", "F", "G", "A", "B", "C", "D"])

    func testLocrianMode() {
        let locrian = ScaleGenerator(tonic: "g", scaleName: "locrian", pattern: "mMMmMMM")
        XCTAssertEqual(locrian.pitches(), ["G", "Ab", "Bb", "C", "Db", "Eb", "F"])

    func testHarmonicMinor() {
        let harmonicMinor = ScaleGenerator(tonic: "d", scaleName: "harmonic minor", pattern: "MmMMmAm")
        XCTAssertEqual(harmonicMinor.pitches(), ["D", "E", "F", "G", "A", "Bb", "Db"])

    func testOctatonic() {
        let octatonic = ScaleGenerator(tonic: "C", scaleName: "octatonic", pattern: "MmMmMmMm")
        XCTAssertEqual(octatonic.pitches(), ["C", "D", "D#", "F", "F#", "G#", "A", "B"])

    func testHexatonic() {
        let hexatonic = ScaleGenerator(tonic: "Db", scaleName: "hexatonic", pattern: "MMMMMM")
        XCTAssertEqual(hexatonic.pitches(), ["Db", "Eb", "F", "G", "A", "B"])

    func testPentatonic() {
        let pentatonic = ScaleGenerator(tonic: "A", scaleName: "pentatonic", pattern: "MMAMA")
        XCTAssertEqual(pentatonic.pitches(), ["A", "B", "C#", "E", "F#"])

    func testEnigmatic() {
        let enigmatic = ScaleGenerator(tonic: "G", scaleName: "enigma", pattern: "mAMMMmm")
        XCTAssertEqual(enigmatic.pitches(), ["G", "G#", "B", "C#", "D#", "F", "F#"])

    static var allTests: [(String, (ScaleGeneratorTests) -> () throws -> Void)] {
        return [
            ("testNamingScale", testNamingScale),
            ("testChromaticScale", testChromaticScale),
            ("testAnotherChromaticScale", testAnotherChromaticScale),
            ("testNamingMajorScale", testNamingMajorScale),
            ("testMajorScale", testMajorScale),
            ("testAnotherMajorScale", testAnotherMajorScale),
            ("testDorianMode", testDorianMode),
            ("testMixolydianMode", testMixolydianMode),
            ("testLydianMode", testLydianMode),
            ("testPhrygianMode", testPhrygianMode),
            ("testLocrianMode", testLocrianMode),
            ("testHarmonicMinor", testHarmonicMinor),
            ("testOctatonic", testOctatonic),
            ("testHexatonic", testHexatonic),
            ("testPentatonic", testPentatonic),
            ("testEnigmatic", testEnigmatic)
// MARK: - ScaleGenerator

struct ScaleGenerator {
    // MARK: - Stored properties
    let name: String
    let _pitches: [String]
    func pitches() -> [String] { return _pitches }
    // MARK: - Lifecycle
        tonic: String,
        scaleName: String,
        pattern: String? = nil)
        self.name = "\(tonic.uppercased()) \(scaleName)"
        self._pitches = Self.makeDiatonicScale(
            tonic: tonic,
            pattern: pattern
    // MARK: - Logic
    private static func makeDiatonicScale(
        tonic: String,
        pattern: String?) -> [String]
        let pitches = makeChromaticPattern(
            tonic: tonic
        guard let pattern = pattern,
              pattern.hasElements else
            return pitches
        var index = pitches.startIndex
        var scale: [String] = []

        let steps = pattern
            .map({ Key(rawValue: $0)! })
            .map({ $0.step })

        for step in steps {
            let pitch = pitches[index]
            index = index.advanced(by: step)

        return scale
    private static func makeChromaticPattern(
        tonic: String) -> [String]
        let notes: [String] = {
            if tonic.isFlatKey {
                return Notes.flat
            else {
                return Notes.sharp
        return makeChromaticPattern(
            tonic: tonic,
            notes: notes
    private static func makeChromaticPattern(
        tonic: String,
        notes: [String]) -> [String]
        let tonic = tonic.withFirstCharacterUppercased
        guard let tonicIndex = notes.firstIndex(of: tonic) else { return notes }
        let lastNotes = notes.suffix(from: tonicIndex)
        let firstNotes = notes.prefix(upTo: tonicIndex)
        return Array(lastNotes + firstNotes)

// MARK: - Key

enum Key: Character {
    case minor = "m"
    case major = "M"
    case augmented = "A"
    var step: Int {
        switch self {
        case .minor: return 1
        case .major: return 2
        case .augmented: return 3

// MARK: - Notes

enum Notes {
    static let sharp = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]
    static let flat = ["A", "Bb", "B", "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab"]

// MARK: - String extension

extension String {
    var isFlatKey: Bool {
        let flatKeys = ["F", "Bb", "Eb", "Ab", "Db", "Gb", "d", "g", "c", "f", "bb", "eb"]
        return flatKeys.contains(self)
    var withFirstCharacterUppercased: String {
        return self
            .map({ (index) -> String in
                let character = self[index]
                guard index == self.startIndex else { return String(character) }
                return character.uppercased()

// MARK: - Collection extension

extension Collection {
    var hasElements: Bool {
        return isEmpty == false

Community comments

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

jeffreyfultonca's Reflection

This exercise completely stumped me. I just couldn't wrap my head around the instructions and music theory for some reason. In order to solve it I basically copied bolzani's solution, then re-implemented parts for fun. Thanks to bolzani for the help.