Back to General discussions forum
Hello there! I got stuck with the problem #81, Bit Count.
I couldn't be able to work and have a soluction with binary negative numbers.
I've tryed the Method Complement of 2, but it gives to me a wrong answer.
I'm looking forward to any help.
Sinceraly,
Marcos Glasner
Below is my code.
from csv import reader
def stringToInteger(): """ This function open a .csv file and return a list of integer. :return: """ arq = open("problem081.csv") l1 = reader(arq, delimiter=" ") l1 = list(*l1) l2 = [] for i in l1: i = int(i) l2.append(i) return l2
def fromIntegerToBinary(l2): """ This function take a parameter with a list of integer, transform it in binary numbers and put it in a list. :param l2: :return: """ print(l2) l3 = [] for i in range(0, len(l2)): nb = bin(l2[i]).replace("b", "").replace("-", "") # I used this as shown from the example to get the number -1 in the binary system. if l2[i] == -1: l3.append("11111111111111111111111111111111") # I'm trying to get the binary negative number using 2's complement method. elif l2[i] < 0: nb = bin(l2[i]).replace("b", "").replace("-", "") nb = nb[:-1] + "0" # I've using these trick to invert the binary number. nb = nb.replace("0", "x").replace("1", "y") nb = nb.replace("x", "1").replace("y", "0") l3.append(nb) else: l3.append(nb) return l3
def bitCount(l3): """ This function take a parameter with a list of binary numbers from the function above and count in binary system how many bits is in each binary number. P.S.: The number of bit is equal to the number of "1" in that represent the integer number in binary system. :param l3: :return: """ print(l3) cont, l4 = 0, [] for i in range(0, len(l3)): cont = l3[i].count("1") l4.append(cont) return print(*l4)
bitCount(fromIntegerToBinary(stringToInteger()))
Here's a hint.
What do you think -2 (a negative, decimal number) is in binary?
I don't mean what Python returns for bin(-2)
. I mean this: if -1, in the context of the problem, is represented as a sequence of 32 ones, what is the logical way to represent -2 in the same system?
Hello guys!
I'm still having trouble troubleshooting issue #81, Bit Count.
I made some changes to the code but I still can't get the exact answer for all the integers received as input.
Below is my code:
from csv import reader
def stringToInteger(): """ This function open a .csv file and return a list of integer. :return: """ arq = open("problem081.csv") l1 = reader(arq, delimiter=" ") l1 = list(*l1) l2 = [] for i in l1: i = int(i) l2.append(i) return l2
def fromIntegerToBinary(l2): """ This function take a parameter with a list of integer, transform it in binary numbers and put it in a list. :param l2: :return: """ print(l2) strb, l3 = "", [] for i in range(0, len(l2)): nb = bin(l2[i]).replace("0b", "").replace("-", "") n = 32 - len(nb) strb = "" for k in range(n): strb += "1" if l2[i] == -1: nb = "11111111111111111111111111111111" l3.append(nb) elif l2[i] < 0: nb = bin(l2[i]).replace("0b", "").replace("-", "") nb = nb.replace("1", "x").replace("0", "y") nb = nb.replace("x", "0").replace("y", "1") if nb[-1] == "1": nb = nb[0:-1] + "0" nb = strb + nb elif nb[-1] == "0": nb = nb[0:-1] + "1" nb = strb + nb l3.append(nb) print(len(l3[i])) else: nb = bin(l2[i]).replace("0b", "") l3.append(n_b) return l3
def bitCount(l3): """ This function take a parameter with a list of binary numbers from the function above and count in binary system how many bits is in each binary number. P.S.: The number of bit is equal to the number of "1" in that represent the integer number in binary system. :param l3: :return: """ cont, l4 = 0, [] for i in range(0, len(l3)): cont = l3[i].count("1") l4.append(cont) return print(*l4)
bitCount(fromIntegerToBinary(stringToInteger()))
Hi there!
no need to modify the code unless you get clear understanding of what needs to be done :) regretfully Python, while good for most of problems, is bewildering you here due to its built-in "wrong ints".
Try the wikipedia article on signed integers representation, and particularly on "two's complement". That's really quite easy despite so much text is written here.
https://en.wikipedia.org/wiki/Signed_number_representations
Also no need to paste your code - whoever solved the problem can see your submission.
Ok friends!
Thanks so much for yours suports. I think I understand the problem.
As you said, the Python's built-in function dosen't treat the negative binary numbers correctly.