-
-
Save kikers25/dc211f9c40c99ec5647af60cce41e044 to your computer and use it in GitHub Desktop.
CodeJam2017QualificationRound
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
lastIChange = -1 | |
def addingCase(n, text): | |
resultCase = "Case #" + str(n) + ": " + text + "\n" | |
print resultCase | |
return resultCase | |
def minimumK(K): | |
if (K % 2 == 0): | |
return K / 2 | |
return (K / 2) + 1 | |
def flipK(pancakes, i, K): | |
lenght = len(pancakes) | |
flipping = pancakes[i:i+K] | |
# print "flipping", flipping | |
flipping = ''.join('+' if x == '-' else '-' for x in flipping) | |
# print pancakes[0:i], flipping, pancakes[i+K:lenght] | |
return pancakes[0:i] + flipping + pancakes[i+K:lenght] | |
def exec_maneuver(pancakes, K): | |
lenght = len(pancakes) | |
for i in range(0, lenght): | |
if (pancakes[i] == "-"): | |
print "lastIChange", lastIChange, " i + K: ", i + K | |
if (i + K > lenght or lastIChange >= i): | |
return "IMPOSSIBLE" | |
lastIChange = i | |
return flipK(pancakes, i, K) | |
return "IMPOSSIBLE" | |
def numbersExecutionManeuver(pancakes, K): | |
result = 0 | |
happyFacePancakes = "+" * len(pancakes) | |
# print "happyFacePancakes", happyFacePancakes | |
lastIChange = -1 | |
while (pancakes != happyFacePancakes and pancakes != "IMPOSSIBLE"): | |
result += 1 | |
lenght = len(pancakes) | |
for i in range(0, lenght): | |
if (pancakes[i] == "-"): | |
if (i + K > lenght or lastIChange > i): | |
return "IMPOSSIBLE" | |
lastIChange = i | |
pancakes = flipK(pancakes, i, K) | |
break | |
print "after maneuver: " + pancakes | |
if (pancakes == "IMPOSSIBLE"): | |
return "IMPOSSIBLE" | |
return str(result) | |
inFile = open("A-small-attempt3.in", "r") | |
outFile = open("A-small-attempt3.out", "w") | |
t = int(inFile.readline()) | |
print "number of test cases is", t | |
print "testing ", 15 / 2 | |
icase = 1 | |
while icase <= t: | |
line = inFile.readline().split(" ") | |
pancakes = line[0] | |
K = int(line[1]) | |
print "n is '" + pancakes + "' and K is " + str(K) | |
outFile.write(addingCase(icase, numbersExecutionManeuver(pancakes, K))) | |
icase = icase + 1 | |
outFile.close() | |
inFile.close() |
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
TIDY = -1 | |
def addingCase(n, text): | |
resultCase = "Case #" + str(n) + ": " + str(text) + "\n" | |
print resultCase | |
return resultCase | |
def nextPosibleNumber(result, wrongI): | |
resultString = str(result) | |
length = len(resultString) | |
minus = int(resultString[wrongI:length]) + 1 | |
print "minus", str(minus) | |
return result - minus | |
def tidy(n): | |
nString = str(n) | |
for i in range(0, len(nString) - 1): | |
if (int(nString[i]) > int(nString[i+1])): | |
print "no tidy", n | |
return i | |
return TIDY | |
def lastTidyNumber(n): | |
result = n | |
print result | |
tries = 0 | |
while (result >= 10): | |
wrongI = tidy(result) | |
if (wrongI == TIDY): | |
return result | |
else: | |
result = nextPosibleNumber(result, wrongI + 1) | |
tries = tries + 1 | |
return result | |
inFile = open("B-small-attempt0.in", "r") | |
outFile = open("B-small-attempt0.out", "w") | |
t = int(inFile.readline()) | |
print "number of test cases is", t | |
icase = 1 | |
while icase <= t: | |
n = int(inFile.readline()) | |
outFile.write(addingCase(icase, lastTidyNumber(n))) | |
icase = icase + 1 | |
outFile.close() | |
inFile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment