Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created February 2, 2021 02:26
Show Gist options
  • Save ZechCodes/91b785fcbc8b33068986c9447ccb8b05 to your computer and use it in GitHub Desktop.
Save ZechCodes/91b785fcbc8b33068986c9447ccb8b05 to your computer and use it in GitHub Desktop.
Challenge 175 - Syncopated Rhythm

Challenge 175 - Syncopated Rhythm

Syncopation means an emphasis on a weak beat of a bar of music; most commonly, beats 2 and 4 (and all other even-numbered beats if applicable).

s is a line of music, represented as a string, where hashtags # represent emphasized beats. Create a function that returns if the line of music contains any syncopation.

Examples

has_syncopation(".#.#.#.#") ➞ True
# There are Hash signs in the second, fourth, sixth and
# eighth positions of the string.

has_syncopation("#.#...#.") ➞ False
# There are no Hash signs in the second, fourth, sixth or
# eighth positions of the string.

has_syncopation("#.#.###.") ➞ True
# There are Hash signs in the sixth positions of the string.

Notes

  • All other unemphasized beats will be represented as a dot.
import unittest
def has_syncopation(music: str) -> bool:
return False # Put your code here!!!
class Test(unittest.TestCase):
def test_1(self):
self.assertEqual(has_syncopation(".#.#.#.#"), True)
def test_2(self):
self.assertEqual(has_syncopation("#.#...#."), False)
def test_3(self):
self.assertEqual(has_syncopation("#.#.###."), True)
def test_4(self):
self.assertEqual(has_syncopation(".."), False)
def test_5(self):
self.assertEqual(has_syncopation(""), False)
def test_6(self):
self.assertEqual(has_syncopation("##"), True)
def test_7(self):
self.assertEqual(has_syncopation("####...."), True)
def test_8(self):
self.assertEqual(has_syncopation("#"), False)
def test_9(self):
self.assertEqual(has_syncopation(".#.#...."), True)
def test_10(self):
self.assertEqual(has_syncopation(".#.#"), True)
def test_11(self):
self.assertEqual(has_syncopation(".#."), True)
def test_12(self):
self.assertEqual(has_syncopation("#."), False)
def test_13(self):
self.assertEqual(has_syncopation(".#"), True)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment