def get_fourth_species(notes):
    result = ''
    # Ensure the notes are in range
    normalised = [note for note in notes if note > 0 and note < 18]
    if not normalised:
        return result

    # Fourth species starts with two beats rest.
    result = 'r2 '

    # Translate all the others except the final two.
    body = [NOTES[note] for note in normalised[:-2]]
    for pitch in body:
        result += '%s~ %s ' % (pitch, pitch)

    # Ensure the penultimate note is a semitone away IFF moving up to the final
    # note. (Kinda hacky - would be easier in Lisp)
    final_note = normalised.pop()
    penultimate_note = normalised.pop()
    next_note = NOTES[penultimate_note]
    if final_note == penultimate_note + 1:
        # Check if the note isn't a C or an F
        if final_note not in [4, 7, 11, 14]:
            # insert 'is' to sharpen the pitch of the note by a semitone.
            next_note = next_note[0] + 'is' + next_note[1:]
    result += ' ' + next_note

    # Ensure the final note is a semibreve.
    result += ' %s 1' % (NOTES[final_note])

    # Tidy up double spaces.
    result = result.replace('  ', ' ')
    return result
Given a representation of the contrapunctus part of fourth species
dataset/ETHPy150Open ntoll/foox/foox/lilypond.py/get_fourth_species