SHARE
TWEET

/r/dailyprogrammer challenge 343 major scales

a guest Nov 8th, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. # Background
  4.  
  5. For the purpose of this challenge, the 12
  6. musical notes in the chromatic scale are named:
  7.  
  8. ```
  9. C  C#  D  D#  E  F  F#  G  G#  A  A#  B
  10. ```
  11.  
  12. The interval between each pair of notes is called a
  13. semitone, and the sequence wraps around. So for
  14. instance, E is 1 semitone above D#, C is 1 semitone
  15. above B, F# is 4 semitones above D, and C# is 10
  16. semitones above D#. (This also means that every note is
  17. 12 semitones above itself.)
  18.  
  19. A major scale comprises 7
  20. out of the 12 notes in the chromatic scale. There are 12
  21. different major scales, one for each note. For instance,
  22. the D major scale comprises these 7 notes:
  23.  
  24. ```
  25. D  E  F#  G  A  B  C#
  26. ```
  27.  
  28. The
  29. notes in a major scale are the notes that are 0, 2, 4,
  30. 5, 7, 9, and 11 semitones above the note that the scale
  31. is named after. In the movable do solfège system, these
  32. are referred to by the names Do, Re, Mi, Fa, So, La, and
  33. Ti, respectively. So for instance, Mi in the D major
  34. scale is F#, because F# is 4 semitones above D.
  35.  
  36. (In
  37. general, a note can have more than one name. For
  38. instance A# is also known as Bb. Depending on the
  39. context, one or the other name is more appropriate.
  40. You'd never hear it referred to as the A# major scale in
  41. real music. Instead it would be called Bb major. Don't
  42. worry about that for this challenge. Just always use the
  43. names of the notes given above.)
  44.  
  45. # Challenge
  46.  
  47. Write a
  48. function that takes the name of a major scale and the
  49. solfège name of a note, and returns the corresponding
  50. note in that scale.
  51.  
  52. # Examples
  53.  
  54. ```
  55. note("C", "Do") -> "C"
  56. note("C", "Re") -> "D"
  57. note("C", "Mi") -> "E"
  58. note("D", "Mi") -> "F#"
  59. note("A#", "Fa") -> "D#"
  60. ```
  61.  
  62. */
  63.  
  64. import java.util.Arrays;
  65. import java.util.List;
  66.  
  67. public class main{
  68.     public static void main(String... args) {
  69.         note("C", "Do");
  70.         note("C", "Re");
  71.         note("C", "Mi");
  72.         note("D", "Mi");
  73.         note("A#", "Fa");
  74.         /*
  75.        
  76.         note("C", "Do") -> "C"
  77.         note("C", "Re") -> "D"
  78.         note("C", "Mi") -> "E"
  79.         note("D", "Mi") -> "F#"
  80.         note("A#", "Fa") -> "D#"
  81.  
  82.         C
  83.         D
  84.         E
  85.         F#
  86.         D#
  87.  
  88.         */
  89.     }
  90.  
  91.     private static final List<String> chromaticScale = Arrays.asList("C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B");
  92.     private static final List<String> noteNames = Arrays.asList("Do", "Re", "Mi", "Fa", "So", "La", "Ti");
  93.     private static final List<Integer> noteOffsets = Arrays.asList(0, 2, 4, 5, 7, 9, 11);
  94.  
  95.     public static void note(String scale, String name) {
  96.         int base = chromaticScale.indexOf(scale);
  97.         int offset = nameToOffset(name);
  98.         String note = chromaticScale.get((base + offset) % chromaticScale.size());
  99.         System.out.println(note);
  100.     }
  101.  
  102.     private static int nameToOffset(String name) {
  103.         return noteOffsets.get(noteNames.indexOf(name));
  104.     }
  105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top