Last active
April 13, 2017 17:00
-
-
Save opethe1st/332f872cceba4913b93f4a559e9e11c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 flip(s): | |
res = [] | |
for l in s: | |
if l=='+': | |
res.append('-') | |
else: | |
res.append('+') | |
return "".join(res) | |
def countFlips(s,k): | |
count = 0 | |
for i in xrange(len(s)-k+1): | |
#print s | |
if s[i]=='-': | |
s = s[:i]+flip(s[i:i+k])+s[i+k:] | |
count+=1 | |
if s.count('+')==len(s): | |
return count | |
else: | |
return -1 | |
#tests | |
print countFlips('---+-++-',3) #this should be 3 | |
print countFlips('+++++', 4) #this should be 0 | |
print countFlips('-+-+-', 4) #this should be -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment