Silly little programs

Yup, binary to decimal was easy.  Play with the algorithm, do a little pseudocode, poke at the code a little to figure out the Python syntax (reference manual!), debug a little and poof, it works.

  1. binary = input(“Enter a binary number.”)
  2. lenBinary = len(binary)
  3. dec = 0
  4. for i in range(lenBinary):                               #Go through the length of the binary number.
  5.     if binary[lenBinary-(i+1)] == “1”:           #Go from right to left checking if the value is a “1”.
  6.         dec = dec + 2**i                                         #If a “1” convert to base 10 and add it up.
  7. print(“The decimal equivalent of “, binary,” base 2 is “, dec, ” base 10.”)

Undoubtedly the Python whizzes out there can snipe the heck out of it with syntax short cuts but this is what the kids will build initially.  I do very few short cuts because they are often language specific. Of course now that I look at my code I think I can redo that range parameter to do some of what that if statement is doing…

There are some things in Python that really throw me off.  I guess the biggest is the range object in the for statement.  I keep forgetting the range is one less than the value.  Of course I take a few minutes to remember that little detail.  Like I said too many languages.

A comment to my last post by Bri Morrison suggested looking at Roman numeral conversions.  I hate it when people make project suggestions like that.  Especially good suggestions like that.  Now I am going to sit and stare at the ceiling and have to figure out how to do it.  As though I did not have enough things to stare at the ceiling about.  So now, Roman numerals have no place value and a smaller value to the left of a bigger value means subtract…hmmm.  What a strange way to have fun.  Thank God I like to mountain bike and snowboard because otherwise I would live in front of this stupid computer.

Advertisements

5 Responses to “Silly little programs”

  1. gasstationwithoutpumps Says:

    That’s not binary to decimal. That’s binary as a string to binary internal representation. You’re using someone else’s binary to decimal converter (hidden in the print statement).

  2. gflint Says:

    Hmmm. I do not see it. I do treat the binary number as a string so I can work on one place value at a time. If the value is a one compute the power of 2 for that place. Add them up as you go. This is the algorithm I would use to do it by hand, which was my objective. The print statement does no converting, it just prints out my two variables. I would definitely agree this is not an efficient way to do it or that it is the way it would be converted by mathematical software. I am trying to reinforce converting from a manual algorithm to a computer program and the power of 2 place values.

  3. Bri Morrison Says:

    I think gasstationwithoutpumps is referring to the fact that you are printing the decimal number (which is stored internally in binary) to decimal. I.e., the computer is converting the internal binary to a decimal string representation. But yes, you’ve also already done the conversion as well :). Perhaps he wants you to convert to a decimal string??? I would give your solution an A.

  4. gflint Says:

    Consider me dense. I still do not see it. I have been programming at the kid level to long. How is the computer doing an internal conversion? “binary” and “dec” are just variables. “binary” is a string representation of a binary number, not a binary number such that the computer would recognize as binary. The algorithm just plucks it apart in the same manner as would be done by hand. If I were to do this with pencil and paper I would be doing the exact same process. What am I missing here?

  5. gasstationwithoutpumps Says:

    There are 3 different representations of numbers that are involved:
    binary string
    internal representation
    decimal string

    What you have done is “binary string”->”internal”.
    You then used the “print” statement to invoke someone else’s “internal”->”decimal” conversion.

    Usually, when people talk about binary->decimal conversion, they mean “internal”=>”decimal string”, not “binary string”=>internal, because computers haven’t used decimal representation internally since the early 1960s (except for calculators).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: