-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Counter.py
80 lines (65 loc) · 2 KB
/
Binary Counter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# check if carry, if carrey set this number to zero move to next number
#from String to List and List to String File
def stringOrIntegerToList(string):
list = []
for i in userInput:
try: #make parts of list integers
i = int(i)
except:
i = str(i)
list.append(i)
return list
def listToString(list):
string = ""
for i in list:
#make parts of list a string
i = str(i)
string += i
return string
def zeroRemover(list):
firstOne = "not yet"
newlist = []
for i in list: #remove zeros in front
if i != 0 and firstOne == "not yet":
firstOne = "one has been found"
newlist.append(i)
elif firstOne == "one has been found":
newlist.append(i)
return newlist
# ---------------------------------
def checkCarry(list):
carry = "n"
start = "y" #this stops the list once everything is carried
newlist = []
list.reverse()
if list[-1] == 1:
list.append(0)
for i in list:
if i == 1 and start == "y":
newlist.append(0)
carry = "y"
elif i == 0 and carry == "y" and start == "y":
newlist.append(1)
carry = "n"
start = "n"
elif i == 0 and start == "y":
newlist.append(0)
# once one is added
elif start == "n":
newlist.append(i)
newlist.reverse()
return newlist
def binaryAdd(list):
newList = []
if list[-1] == 1:
list = checkCarry(list)
return zeroRemover(list)
elif list[-1] == 0:
list[-1] = 1
return zeroRemover(list)
userInput = input("Please give the binary number you want to add one too: ")
userList = stringOrIntegerToList(userInput)
userListPlus1 = binaryAdd(userList)
print(userListPlus1)
awnser = listToString(userListPlus1)
print("Here is the binary number you are looking for:", awnser)