Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (C) Corné Dorrestijn 2012
- Algorithm to sort out white and black keys on a piano.
- Desired result:
- 02 04 NN 07 09 11 NN 14 16 NN 19
- 01 03 05 06 08 10 12 13 15 17 18 20
- Pseudocode:
- X = 0 ; Total key count
- Y = 3 ; Max keys in the current block of black keys
- Z = 0 ; Black key count in current block
- S = FALSE ; Black/White key switch
- A = [] ; Black keys array
- B = [] ; White keys array
- X++ ; Increase the current key
- IF S ; If a black key may be placed
- Z++ ; Increase the black key count (of the current block)
- IF Z == Y ; If that black key count is equal to the max keys in this block
- Y = Y == 3 ? 4 : 3 ; If maximum keys in this block is 3 make it 4 if not make it 3
- Z = 0 ; Reset the black key count
- B.PUSH(X) ; Push the current key in the WHITE key array
- S = TRUE ; Set the switch so that a black key may be placed
- ELSE ; If the black key count is not equal to the max in this block
- A.PUSH(X) ; Push the current key in the BLACK key array
- S = FALSE ; Set the switch so that a white key may be placed
- ELSE ; If a white key may be placed
- B.PUSH(X) ; Push the current key in the white key array
- S = TRUE ; Set the switch so that a black key may be placed
- Memory after 8 loops:
- i = 8
- Y = 4
- Z = 1
- S = FALSE
- A = [2, 4, 7]
- B = [1, 3, 5, 6, 8]
- ECMAScript 3:
- var maxBlock = 3;
- var currentInBlock = 0;
- var blockSwitch = false;
- var blackKeyArray = [];
- var whiteKeyArray = [];
- for(var i = 1; i <= 49; i++)
- {
- if(blockSwitch)
- {
- currentInBlock++;
- if(currentInBlock == maxBlock)
- {
- maxBlock = maxBlock == 3 ? 4 : 3;
- currentInBlock = 0;
- whiteKeyArray.push(i);
- blockSwitch = true;
- }
- else
- {
- blackKeyArray.push(i);
- blockSwitch = false;
- }
- }
- else
- {
- whiteKeyArray.push(i);
- blockSwitch = true;
- }
- }
- Result:
- blackKeyArray = [2, 4, 7, 9, 11, 14, 16, 19, 21, 23, 26, 28, 31, 33, 35, 38, 40, 43, 45, 47];
- 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