Problem 1686. Generate a melodic contour string matrix

Parsons code is a surprisingly effective way to identify music by its melodic motion. That is, with each successive note, does the pitch go up, down, or stay the same. No effort is made to capture the length of the interval between notes. The result is a lossy but useful "thumbprint" of a musical piece.

You will be given a string in Parsons code. By convention, the first note is denoted by *. Each note thereafter is either "u" for up, "d" for down, or "r" for repeat. The expected output is a string matrix showing the tune's contour. It can contain only space, star, dash, forward slash, and backward slash (ASCII values 32, 42, 45, 47, and 92 respectively). The output matrix should be the smallest possible bounding box that can contain all the nonspace characters. That is, there should be no empty rows or columns.


 str = '*rrr'
 melody = '*-*-*-*'
 str = '*du'
 melody = ['*   *'
           ' \ / '
           '  *  ']
 The "Happy Birthday" song!
 str = '*rududdrudud'
 melody = ['    *   *              '
           '   / \ / \             '
           '*-*   *   *     *   *  '
           '           \   / \ / \ '
           '            *-*   *   *']

Solution Stats

71.43% Correct | 28.57% Incorrect
Last solution submitted on Dec 05, 2014

Problem Comments


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!

Join the 15-year community celebration.

Play games and win prizes!

Learn more