7 kyu

Musical Pitch Classes

391 of 1,597craecke

Description:

In music, each note is named by its pitch class (e.g., C, E♭, F♯), and each pitch class can alternatively be expressed as an integer from 0 to 11. Your task will be to write a method called pitch_class (JS: pitchClass ) that, when given a letter-based pitch class, returns the corresponding integer.

Only seven letters are used to name the notes: "A" through "G." These letter names are cyclical, just like the days of the week. The notes corresponding to those letters are called the "natural notes." Here are the numbers corresponding to each of them:

  • C : 0
  • D : 2
  • E : 4
  • F : 5
  • G : 7
  • A : 9
  • B : 11

So pitch_class('D') (JS: pitchClass('D') ) should return 2, and pitch_class('B') (JS: pitchClass('B') ) should return 11.

The sharp sign ("♯") is essentially an increment operator, so "C♯" (pronounced "C sharp") refers to one note higher than C, which has a value of 1, whereas F♯ has a value of 6. Since Codewars doesn't allow the sharp sign, we'll use a number sign ("#") instead.

The flat sign ("♭") is the opposite of a sharp, meaning one note lower. F♭ has a value of 4, and C♭ has a value of 11 (the twelve-note system is cyclical). Since Codewars doesn't allow the flat sign, we'll use a lowercase "b" instead.

Return nil (JS: null ) for invalid input.

(Next in this series: http://www.codewars.com/kata/integer-to-musical-pitch-classes)

Algorithms

More By Author:

Check out these other kata created by craecke

Stats:

CreatedFeb 6, 2015
PublishedFeb 6, 2015
Warriors Trained2682
Total Skips155
Total Code Submissions14662
Total Times Completed1597
Ruby Completions391
JavaScript Completions1262
Total Stars33
% of votes with a positive feedback rating88% of 174
Total "Very Satisfied" Votes138
Total "Somewhat Satisfied" Votes31
Total "Not Satisfied" Votes5
Ad
Contributors
  • craecke Avatar
  • jhoffner Avatar
  • ZozoFouchtra Avatar
Ad