Skip to content

Instantly share code, notes, and snippets.

@starwarsgeek1138
Forked from skyler/stef.py
Last active June 16, 2016 19:20
Show Gist options
  • Save starwarsgeek1138/c2f5b744fd5dd07aa99f048a2757af44 to your computer and use it in GitHub Desktop.
Save starwarsgeek1138/c2f5b744fd5dd07aa99f048a2757af44 to your computer and use it in GitHub Desktop.
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