Last active
December 26, 2015 13:09
-
-
Save julius-datajunkie/7156041 to your computer and use it in GitHub Desktop.
Google Interview Question: http://www.careercup.com/question?id=4860021380743168
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
import unittest | |
def LongestZeroSequence(n): | |
''' | |
Input: n is a string of a binary sequence | |
Output: the length of the longest consecutive zero sequence | |
when encounter 1, stop counting and save the max sequence so far. | |
output the max sequence | |
''' | |
maxSequence = 0 | |
seq = 0 | |
for c in n: | |
if (c == '1'): | |
if (seq > maxSequence): | |
maxSequence = seq | |
seq = 0 | |
else: | |
seq += 1 | |
return maxSequence | |
class Test(unittest.TestCase): | |
def test_LongestZeroSequence(self): | |
self.assertEqual(LongestZeroSequence("1000010"),4) | |
self.assertEqual(LongestZeroSequence("00010000100000100"),5) | |
self.assertEqual(LongestZeroSequenceLogn("1000010"),4) | |
if __name__ == '__main__': | |
unittest.main(exit=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment