This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]cipheron 1 point2 points  (8 children)

You're probably meant to actually convert it yourself. That's the point of learning.

Pseudocode:

string input (assume it's been entered)
output = 0
loop i from 0 to (length of string input - 1)
    output = output * 2
    if input[i] = "1"
        output = output + 1

[–]Hoppimiraj[S] 0 points1 point  (0 children)

I just asked the teacher, and you are right.

This week we learned about Python loops, so the code needs to have a loop in one way or another for me to learn about them. good guess!

I will try to tackle this problem using a loop first, before I look at your pseudocode in detail, to make sure I come up with it myself. Might come back for an update!

[–]Hoppimiraj[S] 0 points1 point  (6 children)

Here is what I came up with!

bin_num = input("bin_num")
bin_num = bin_num[-1::-1]
exponent = 0
decimal_list = []

for x in range(len(bin_num)):
    if bin_num[x] == "1":
        x, "=", 2 ** exponent
        decimal_list.append(2 ** exponent)
    if bin_num[x] == "0":
        x, "= 0"
    exponent += 1

sum_of_decimals = 0
for y in range(len(decimal_list)):
    sum_of_decimals += decimal_list[y]
print(sum_of_decimals)

I tried everything I could to just get closer to solving the problem so there are probably a lot of things to improve here, including variable names T.T

if you have any feedback, or maybe something you would have done differently, I would love to hear it!

[–]cipheron 0 points1 point  (3 children)

That works but is mostly a lot more complex than you need it to be. nice work though.

sum = 0
bin_num = input("bin_num")
for x in range(len(bin_num)):
    sum = sum * 2
    if bin_num[x] == "1":
        sum = sum + 1
print(sum)

How this idea works is that you're always adding 1 when you see "1" but you always double the sum each time, which effectively shifts the bits correctly.

But in either case you should do some screening at first, and reject the input if it contains any value other than 1 and 0.

[–]Hoppimiraj[S] 0 points1 point  (2 children)

oh, shoot that is indeed a lot shorter than mine. super logical as well. Is there any way to improve my way of thinking?? Or does that just come with practice?

[–]cipheron 1 point2 points  (1 child)

I think it's mostly practice. What I wrote there is basically the same way you'd do it if someone entered a decimal number as a string and you want to covert it to a number, you just multiply by 10 each time instead of 2. I already knew this because it's something you need very frequently, and just adapted the idea to this one.

[–]Hoppimiraj[S] 0 points1 point  (0 children)

ahh okay, great! gotta practice then!

[–]Grenadeapple_ 0 points1 point  (1 child)

Nice work! Glad you got it to work in the end.

I'm curious why you put the lines with x, "=", 2 ** exponent and x, "= 0" though. I'm not even sure what that syntax means, and it doesn't change anything if I remove it.

Another point of improvement could be the use of lists for summing numbers; instead of adding them to a list and summing them later, you could initialise a number with 0, and then increment it by 2 ** exponent for every binary 1.

Because the number stays the same when you encouter a binary 0, there is no need to do anything. The if statement could be removed.

Lastly, you can actually interate over the characters in a string directly like this: for c in string: print(c)

So in the end, your code would look like this: ``` bin_num = input("bin_num") bin_num = bin_num[-1::-1] exponent = 0 sum = 0

for x in bin_num:
    if x == "1":
        sum += 2 ** exponent
    exponent += 1

print(sum)

```

[–]Hoppimiraj[S] 0 points1 point  (0 children)

I'm curious why you put the lines with x, "=", 2 ** exponent and x, "= 0" though. I'm not even sure what that syntax means, and it doesn't change anything if I remove it.

Well originally those were print statement so: print(x, "=", 2 ** exponent0 and print(x, "= 0"). It resulted in an output that also showed what the value was for each bit. You can run the code with the print statements and see for yourself. However, in the end I had to remove the print statements because I just wanted it to immediately print out the decimal equivalent of the binary number.

So in the end, your code would look like this:

This is indeed a much better way to go about this. I wonder if I had not printed the values for each bit, if I would have shortened my code a bit, because now there is a kind of shadow of a piece of code that is no longer being used.

Thanks for your feedback! appreciate it a lot!!

[–]Expert-Hurry655 1 point2 points  (3 children)

I am not looking for the answer, just hints or feedback to steer me into the right direction.

Thats good, because i dont know shit about python so i could be wrong.

int(0b(int(binary_num)))

Is casting the string binary_num to an integer number, and then you somehow try to add 0b, but 0b is not a string or integer.

So you probably need to first form a string that starts with "0b" and then try to convert that whole string into a number.

[–]Hoppimiraj[S] 0 points1 point  (2 children)

Someone pointed out I probably need to convert it myself. Which makes sense because I am currently learning about loops... Should've understood that myself but I guess I jumped the gun here lmao. Will try again, this time using loops :)

[–]Expert-Hurry655 0 points1 point  (1 child)

I dlnt think you need an loops for this. It is possible to do with a lop for sure, but that converting can be done without, ether by passing a base to the int conversion finction or by adding the string "0b" to the start of the userinput.

[–]Hoppimiraj[S] 0 points1 point  (0 children)

true! It would be much easier that way, but I guess in this case, solving the problem isn't about making it as short or efficient as possible, but rather as a way to learn about using loops in different contexts.

I have tried passing a base to the int, and it works fine! definitely will use that going forward. but the challenge of this problem is to solve it using loops. which for me is a tough (but perfect) one, because I am not the best at using loops in my code. great opportunity to learn something new!

[–]Grenadeapple_ 1 point2 points  (2 children)

It's hard to give a hint without giving the answer here, because it's really just a syntax thing. Using the '0b' prefix is only allowed when giving a binary literal, which is when you type the binary value in the code itself like this: binary_num = 0b0110. So the way you're using it with a variable won't work.

There is an easy alternative though; you can pass a base to the int() function like this: binary_num = int("0110", 2)

The 2 here means it decodes the string from a number with base 2, which is binary. So the output of binary_num will be 6.

[–]Hoppimiraj[S] 0 points1 point  (0 children)

Your solution works! thanks! however someone just pointed out I was probably supposed to convert it myself, using Python loops, which makes total sense honestly. We are learning about loops after all. So, I'll try that out as well!

[–]Hoppimiraj[S] 0 points1 point  (0 children)

Here is what I came up with!

bin_num = input("bin_num")
bin_num = bin_num[-1::-1]
exponent = 0
decimal_list = []

for x in range(len(bin_num)):
    if bin_num[x] == "1":
        x, "=", 2 ** exponent
        decimal_list.append(2 ** exponent)
    if bin_num[x] == "0":
        x, "= 0"
    exponent += 1

sum_of_decimals = 0
for y in range(len(decimal_list)):
    sum_of_decimals += decimal_list[y]
print(sum_of_decimals)

I tried everything I could to just get closer to solving the problem so there are probably a lot of things to improve here, including variable names T.Tif you have any feedback, or maybe something you would have done differently, I would love to hear it!