Created
June 16, 2016 03:35
-
-
Save skyler/5853ae5f0dc3149cfcda4ff0d0af1125 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_chords(chords, music): | |
"""Search for chords within music (O(n) time). | |
:param list chords: chords to look for, in order | |
:param list music: music to search within | |
""" | |
if len(music) < len(chords): | |
return 0 | |
my_chord_i = -1 | |
occurrences = 0 | |
for my_chord in music: | |
if my_chord in chords: | |
j = chords.index(my_chord) | |
if j > my_chord_i: | |
my_chord_i = j | |
if my_chord_i == len(chords) -1: | |
occurrences += 1 | |
my_chord_i = -1 | |
return occurrences | |
if __name__ == '__main__': | |
chords = ['C', 'F', 'G'] | |
music0 = ['C', 'F'] | |
times0 = find_chords(chords, music0) | |
assert times0 == 0 | |
music1 = ['C', 'F', 'G'] | |
times1 = find_chords(chords, music1) | |
assert times1 == 1 | |
music2 = ['C', 'F', 'G', 'A', 'C', 'F', 'G', 'E'] | |
times2 = find_chords(chords, music2) | |
assert times2 == 2 | |
music3 = ['C', 'F', 'G', 'C', 'F', 'C', 'F', 'G', 'B', 'C', 'F', 'G'] | |
times3 = find_chords(chords, music3) | |
assert times3 == 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this - it may actually come in good use later if I want to search specific chord progressions in the corpus. 👍