1 / 10

Hard Problem

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.

blithe
Download Presentation

Hard Problem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hard Problem

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

  3. 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.

  4. 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.

  5. 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.

  6. Solution

  7. 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.

  8. 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.

  9. 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.

More Related