Given two note names, you are to write a program that determines if the interval formed by these two notes is consonant or dissonant.
In Western music, there are only 12 "different" tones. Their names, sorted from lowest to highest, are these:
C, C#, D, D#, E, F, F#, G, G#, A, A#, B. The sequence is cyclical, i. e. it continues with another
C after the
The distance between two tones is called an interval. The interval between any two notes that are adjacent in the series above (e. g.
C — C# or
E — F) is called a semitone. The interval between more distant notes is defined as the number of semitone steps needed to get from the first to the second (while possibly wrapping around the sequence). Some examples:
D to E = 2 semitones,
C to G = 7 semitones,
B to D# = 4 semitones (this wraps around the sequence).1
Now, these intervals are divided into two categories: consonant (pleasantly sounding if you play the two notes at once) and dissonant (not so much).
Let's define the consonant intervals to be: 0, 3, 4, 5, 7, 8 and 9 semitones.
The rest of them is dissonant, namely: 1, 2, 6, 10 and 11 semitones.
Write a "program" (in the usual broad sense of the word: a function is perfectly OK) to do the following:
Take two note names (strings from the sequence above) as an input. You may take them however you like (from stdin, as arguments, separated by whatever you want, even feel free to take them as a list of characters (e. g.
["C","#"]). However, you may not assign any other names to the notes (especially you may not number them from 0 to 11 and use the numbers).
For you music geeks out there, the notes will be specified without the octave. In this case, it also does not matter in which order the notes come and which is lower and which is higher. Finally, you don't need to handle any names not in the list above. No other enharmonics like
E#, no flats, double-alterations and so on.
Pick any two different values. Your program must output one of them whenever the interval formed by the two notes in the input is consonant, and the other if they are not. (Could be
False, but even π and e if you want :))
This is a code-golf. The shortest program in bytes in each language wins. Have fun!
Examples and Test Cases
Note 1 Note 2 Output Interval [semitones] C D Dissonant 2 A# A# Consonant 0 G D Consonant 7 (wraparound) D# A Dissonant 6 F E Dissonant 11 A C Consonant 3
I don't add more of them since there aren't any particularly treacherous cases in this.
This is a first challenge of mine, so any constructive criticism is warmly welcome :—). If you find the theory explanation sloppy, feel free to ask questions. Finally, please do not tell me that this is a dupe of this or this. I made sure it's not. (The latter is quite similar but more complex. I thought that putting up a little simpler challenge will make it easier for people to join.)
1: I tried to simplify this explanation as far as I could. There's a lot more theory around intervals. Please don't bash me for leaving it out.