cornedor

FloppyPianno Key Sort

Oct 17th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (C) Corné Dorrestijn 2012
  2. Algorithm to sort out white and black keys on a piano.
  3.  
  4. Desired result:
  5.   02  04  NN  07  09  11  NN  14  16  NN  19
  6. 01  03  05  06  08  10  12  13  15  17  18  20
  7.  
  8. Pseudocode:
  9.  
  10. X = 0                   ; Total key count
  11. Y = 3                   ; Max keys in the current block of black keys
  12. Z = 0                   ; Black key count in current block
  13. S = FALSE               ; Black/White key switch
  14. A = []                  ; Black keys array
  15. B = []                  ; White keys array
  16.  
  17. X++                 ; Increase the current key
  18. IF S                    ; If a black key may be placed
  19.     Z++             ; Increase the black key count (of the current block)
  20.     IF Z == Y           ; If that black key count is equal to the max keys in this block
  21.         Y = Y == 3 ? 4 : 3  ; If maximum keys in this block is 3 make it 4 if not make it 3
  22.         Z = 0           ; Reset the black key count
  23.         B.PUSH(X)       ; Push the current key in the WHITE key array
  24.         S = TRUE        ; Set the switch so that a black key may be placed
  25.     ELSE                ; If the black key count is not equal to the max in this block
  26.         A.PUSH(X)       ; Push the current key in the BLACK key array
  27.         S = FALSE       ; Set the switch so that a white key may be placed
  28. ELSE                    ; If a white key may be placed
  29.     B.PUSH(X)           ; Push the current key in the white key array
  30.     S = TRUE            ; Set the switch so that a black key may be placed
  31.  
  32.  
  33. Memory after 8 loops:
  34.  
  35. i = 8
  36. Y = 4
  37. Z = 1
  38. S = FALSE
  39. A = [2, 4, 7]
  40. B = [1, 3, 5, 6, 8]
  41.  
  42. ECMAScript 3:
  43.  
  44. var maxBlock = 3;
  45. var currentInBlock = 0;
  46. var blockSwitch = false;
  47. var blackKeyArray = [];
  48. var whiteKeyArray = [];
  49. for(var i = 1; i <= 49; i++)
  50. {
  51.     if(blockSwitch)
  52.     {
  53.         currentInBlock++;
  54.         if(currentInBlock == maxBlock)
  55.         {
  56.             maxBlock = maxBlock == 3 ? 4 : 3;
  57.             currentInBlock = 0;
  58.             whiteKeyArray.push(i);
  59.             blockSwitch = true;
  60.         }
  61.         else
  62.         {
  63.             blackKeyArray.push(i);
  64.             blockSwitch = false;
  65.         }
  66.     }
  67.     else
  68.     {
  69.         whiteKeyArray.push(i);
  70.         blockSwitch = true;
  71.     }
  72. }
  73.  
  74. Result:
  75.  
  76. blackKeyArray = [2, 4, 7, 9, 11, 14, 16, 19, 21, 23, 26, 28, 31, 33, 35, 38, 40, 43, 45, 47];
  77. whiteKeyArray = [1, 3, 5, 6, 8, 10, 12, 13, 15, 17, 18, 20, 22, 24, 25, 27, 29, 30, 32, 34, 36, 37, 39, 41, 42, 44, 46, 48, 49];
Advertisement
Add Comment
Please, Sign In to add comment