Created
September 18, 2012 19:56
-
-
Save prettymuchbryce/3745462 to your computer and use it in GitHub Desktop.
Find minimum swaps to order a string of 0's and 1's
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
def findOperations(string) | |
operations = 0 | |
list = string.split("") | |
startIndex = 0 | |
endIndex = list.length-1 | |
while startIndex < endIndex | |
if list[endIndex]=="0" | |
if list[startIndex] == "1" | |
list[startIndex], list[endIndex] = list[endIndex], list[startIndex] | |
operations = operations+1 | |
endIndex-=1 | |
end | |
startIndex+=1 | |
else | |
endIndex-=1 | |
end | |
end | |
operations | |
end | |
input = gets | |
returnValue = findOperations(input) | |
puts returnValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment