Last active
August 26, 2019 12:27
-
-
Save BenjaminUrquhart/4f667b32903a3d7e1f011967c7768ab3 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
# Usage: python add.py | |
# Enter a positive number via standard input | |
# It will print the number plus one | |
class Incrementer: | |
n = 0 | |
def __init__(self, n =0): | |
self.n = n | |
def get(self): | |
return self.n | |
def increment(self): | |
n = self.__reverse(self.__toList(self.__toBinary(self.n))) | |
tmp = self.__allocateString() | |
for i in self.__getRangeOfIntegersFromZeroToN(self.__getLength(n)): | |
tmp = self.__flipValue(self.__getValueAtN(n,i)) | |
n = self.__setValueAtN(n, i, tmp) | |
if self.__isEqualToOneAsAString(tmp): | |
break | |
if self.__isEqualToZeroAsAString(tmp): | |
n = self.__addValueToList(n, self.__allocateString(1)) | |
n = self.__join(self.__reverse(n), self.__allocateString()) | |
n = self.__getIntegerFromBinaryString(n) | |
self.n = n | |
def __isEqualToOneAsAString(self,n): | |
return self.__allocateString(n) == self.__allocateString(1) | |
def __isEqualToZeroAsAString(self,n): | |
return self.__allocateString(n) == self.__allocateString(0) | |
def __flipValue(self,value): | |
return self.__allocateString(0) if self.__isEqualToOneAsAString(value) else self.__allocateString(1) | |
def __getValueAtN(self,l, n): | |
return self.__toList(l)[int(n)] | |
def __addValueToList(self,l, v): | |
out = self.__toList(l) | |
out += [v] | |
return self.__toList(out) | |
def __setValueAtN(self,l, n, v): | |
out = self.__toList(l) | |
out[int(n)] = v | |
return self.__toList(out) | |
def __toBinary(self,n): | |
return self.__trimUselessChars(bin(n)) | |
def __getIntegerFromBinaryString(self,n): | |
return int(self.__allocateString(value=n), 2) | |
def __trimUselessChars(self,n): | |
return self.__allocateString(n[2:]) | |
def __reverse(self,n): | |
return self.__toList(reversed(n)) | |
def __toList(self,n): | |
if type(n) is int: | |
return [*self.__allocateString(n)] | |
return [*n] | |
def __join(self,n, by): | |
return by.join(n) | |
def __getLength(self,n): | |
return len(n) | |
def __getRangeOfIntegersFromZeroToN(self,n): | |
i = 0 | |
out = self.__toList(self.__allocateString()) | |
while i < n: | |
out += self.__toList(i) | |
i += 1 | |
return out | |
def __allocateString(self,value=""): | |
return str(value) | |
inc = Incrementer(int(input())) | |
inc.increment() | |
print(inc.get()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment