23
1
Your task is to take a sequence of characters (the music) as input (in a function or program), and print (or return) the music as it would look like in a music box.
You will only receive the characters ABCDEFG.()
as input, and the input will never be empty. You may also receive the letters in lowercase, if you wish for it.
This is an empty music box, of length 3:
.......
.......
.......
As you can see, the lines are 7 characters long, and since the length of the music box is 3, we have 3 lines. There are only .
s here, since the music box is empty. Let's put some music in it!
First, we create the music box. In this example, the input will be CDAG.DAG
.
The length of CDAG.DAG
is 8, so we need a music box of length 8:
.......
.......
.......
.......
.......
.......
.......
.......
Then, we read the input, one character at a time, and place an O
at its respective position.
The first character is C
, and the location of each note is equivalent to this (I added spaces for clarity):
A B C D E F G
. . . . . . .
. . . . . . .
(and so on)
If the input character is a .
, then we just print an empty line .......
So, the C
would be the 3rd character along. Let's put it in our music box at the top:
..O....
.......
.......
.......
.......
.......
.......
.......
We will repeat this process for all the other characters (the text in brackets is just to show you the note, you shouldn't output that):
..O.... (C)
...O... (D)
O...... (A)
......O (G)
....... (.)
...O... (D)
O...... (A)
......O (G)
Because of how music boxes work, if we use a character other than O
, .
and <insert newline here>
, such as a space, in our output, then it won't play the correct music!
This is a chord:
(ACE)
This chord is instructing us to play the notes A
, C
and E
at the same time. There will never be a pause (ie a .
) in a chord.
This is how it would be written:
O.O.O...
And this is how it might appear in music: B(ACE)D
You will never receive a chord in a chord, ie this won't be valid: (AB(CD)EF)
or this: A(B())
, and chord will not be empty, ie this won't be valid: A()B
You will never receive an invalid input.
Examples:
B(ACE)D
.O.....
O.O.O..
...O...
B
.O.....
GGABC
......O
......O
O......
.O.....
..O....
...
.......
.......
.......
A..F.C(DA).
O......
.......
.......
.....O.
.......
..O....
O..O...
.......
.(ABCDEF)
.......
OOOOOO.
Trailing/leading whitespace on the output is permitted.
As this is code-golf, the shortest code wins!
can a music string contain
()
twice (e.g:AB(CD)E(FG)
) ?? – Mr. Xcoder – 2017-03-10T09:01:52.740@Mr.Xcoder Yes, it can. – Okx – 2017-03-10T09:05:51.013
Can the output be a list/array of characters? – Rod – 2017-03-10T12:00:50.960
@Rod sure, as per PPCG standards – Okx – 2017-03-10T12:05:32.293
Are we guaranteed to not get two of the same note in the same chord? – Business Cat – 2017-03-10T16:59:39.487
@BasicSunset Yes. – Okx – 2017-03-10T17:19:50.143
In a language where a list of characters counts as a string, can we take input as a nested list, such as
{B, {A, C, E}, D}
for your first example? – Greg Martin – 2017-03-10T18:42:30.800@GregMartin No, I don't that would be vaid. – Okx – 2017-03-10T22:38:18.463
Can input be
(B)
instead ofB
? – l4m2 – 2018-07-23T10:51:46.470@l4m2 No, it cannot. – Okx – 2018-07-23T13:39:49.373