Last active
October 8, 2016 08:03
-
-
Save jonasbn/abd5d4d5269fee749c0d813fad26f1b6 to your computer and use it in GitHub Desktop.
Nordea task 1, GOTOCopenhagen 2016
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
#!/usr/bin/env python | |
import re | |
import unittest | |
import string | |
def isSubstring (s1, s2): | |
# We do the matching | |
result = re.search(s2, s1) | |
# We have a result | |
if result: | |
return 'YES' | |
# duplicate the original string | |
s1 = s1 + s1 | |
# handling bad strings | |
if len(s2) * 2 != len(s1): | |
return 'NO' | |
return 'NO' | |
# StackOverflow: | |
# http://stackoverflow.com/questions/8458244/swap-letters-in-a-string-in-python#8458282 | |
def rotate (strg,n): | |
return strg[n:] + strg[:n] | |
class TestIsSubstring(unittest.TestCase): | |
def testIsCompleteStringSubstring(self): | |
s1 = 'waterbottle' | |
s2 = 'waterbottle' | |
self.failUnlessEqual(isSubstring(s1, s2), 'YES') | |
def testIsStringSubstring(self): | |
s1 = 'waterbottle' | |
s2 = 'water' | |
self.failUnlessEqual(isSubstring(s1, s2), 'YES') | |
def testIsStringSubstringRotated(self): | |
s1 = 'waterbottle' | |
s2 = 'waterbottle' | |
for index in range(len(s1)): | |
s2 = rotate(s2,0) | |
self.failUnlessEqual(isSubstring(s1, s2), 'YES') | |
# results in false positive due to string duplication | |
def testIsSpellingErrorSubstringRotated(self): | |
s1 = 'waterbottle' | |
s2 = 'waterbotle' | |
for index in range(len(s1)): | |
s2 = rotate(s2, index) | |
self.failUnlessEqual(isSubstring(s1, s2), 'NO') | |
if __name__ == '__main__': | |
unittest.main() |
Updated, got all of the tests working and added actual substring matching
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assignment described at: https://twitter.com/jonasbn/status/783354653845098496