Hard Problem. Problem Write a function that adds two numbers. You should not use + or any...

10
Hard Problem

Transcript of Hard Problem. Problem Write a function that adds two numbers. You should not use + or any...

Page 1: Hard Problem. Problem  Write a function that adds two numbers. You should not use + or any arithmetic operators.

Hard Problem

Page 2: Hard Problem. Problem  Write a function that adds two numbers. You should not use + or any arithmetic operators.

Problem Write a function that adds two numbers. You

should not use + or any arithmetic operators.

Page 3: Hard Problem. Problem  Write a function that adds two numbers. You should not use + or any arithmetic operators.

Hints To investigate this problem, let’s start off by

gaining a deeper understanding of how we add numbers.

We’ll work in Base 10 so that it’s easier to see. To add 759 + 674, I would usually add digit[0]

from each number, carry the one, add digit[1] from each number, carry the one, etc.

You could take the same approach in binary: add each digit, and carry the one as necessary.

Page 4: Hard Problem. Problem  Write a function that adds two numbers. You should not use + or any arithmetic operators.

Hints Can we make this a little easier?

Yes! Imagine I decided to split apart the “addition” and “carry” steps.

That is, I do the following: Add 759 + 674, but “forget” to carry. I then get

323. Add 759 + 674 but only do the carrying, rather

than the addition of each digit. I then get 1110. Add the result of the first two operations

(recursively, using the same process described in step 1 and 2): 1110 + 323 = 1433.

Page 5: Hard Problem. Problem  Write a function that adds two numbers. You should not use + or any arithmetic operators.

Hints Now, how would we do this in binary?

If I add two binary numbers together but forget to carry, bit[i] will be 0 if bit[i] in a and b are both 0 or both 1.This is an XOR.

If I add two numbers together but only carry, I will have a 1 in bit[i] if bit[i-1] in a and b are both 1’s. This is an AND, shifted.

Now, recurse until there’s nothing to carry.

Page 6: Hard Problem. Problem  Write a function that adds two numbers. You should not use + or any arithmetic operators.

Solution

Page 7: Hard Problem. Problem  Write a function that adds two numbers. You should not use + or any arithmetic operators.

Discussion There are a couple of suggestions for figuring

out this problem: Our first instinct in problems like these should

be that we’re going to have to work with bits. Why? Because when you take away the + sign,

what other choice do we have? Plus, that’s how computers do it.

Our next thought in problems like these should be to really, really understand how you add. Walk through an addition problem to see if you

can understand something new—some pattern—and then see if you can replicate that with code.

Page 8: Hard Problem. Problem  Write a function that adds two numbers. You should not use + or any arithmetic operators.

Problem Write a method to shuffle a deck of cards. It must be a perfect shuffle - in other words, each

52! permutations of the deck has to be equally likely. Assume that you are given a random number

generator which is perfect.

Page 9: Hard Problem. Problem  Write a function that adds two numbers. You should not use + or any arithmetic operators.

non-CS mind solution My first thought was to generate an unshuffled

deck as an array-like structure -- all cards in order by suit.

Then I'd create a second array-like structure. I'd walk through each card in the unshuffled deck,

pick a random number, and insert the card at the randomly selected spot in the second array.

If the randomly chosen position in the second array was already occupied, I'd choose another random number, see if it was used, and so on until the random selection happened to land on a free spot.

I'll call this the Random Insert approach.

Page 10: Hard Problem. Problem  Write a function that adds two numbers. You should not use + or any arithmetic operators.