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

Slides:



Advertisements
Similar presentations
As you come in…  Sign in (in back) and pick up  Badge  Name Card – write your first name LARGELY on back  Log in:  Launch/Start Alice  Any questions?
Advertisements

Permutations and Combinations
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
Practice Quiz Question
Logic Networks …and the card game Boolette
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
Best-First Search: Agendas
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
1 Section 5.1 Discrete Probability. 2 LaPlace’s definition of probability Number of successful outcomes divided by the number of possible outcomes This.
Data Structures Topic #12.
MAE 552 Heuristic Optimization Instructor: John Eddy Lecture #15 2/27/02 Bit Manipulation.
CS 240: Data Structures Thursday, July 12 th Sorting – Bubble, Insertion, Quicksort, Mergesort, Analysis, STL.
CS 106 Introduction to Computer Science I 04 / 07 / 2010 Instructor: Michael Eckmann.
Probability Using Permutations and Combinations
Computer Systems Nat 4/5 Computing Science Lesson 1: Binary.
Binary “There are 10 types of people in the world… those that understand binary and those that don’t.”
Lecture 2 MAS 714 Hartmut Klauck
Binary Addition Addition Rules: = = = = = carry 1 1 carry 1 Example 1: Example 2:
Warm-up Problem 2 Write three positive integers in a line. In the space just below and between each pair of adjacent integers, write their difference.
Learning what questions to ask. 8/29/03Decision Trees2  Job is to build a tree that represents a series of questions that the classifier will ask of.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
IT253: Computer Organization
Welcome to my conference! February th grade Guadalupe.
JAOIT 8. Binary Programming  As someone just starting their adventures in the networking world binary can be both scary and confusing. I am going to.
Recursion. Review  Recursive solutions, by definition, are built off solutions to sub-problems.  Many times, this will mean simply to compute f(n) by.
CPS120: Introduction to Computer Science Computer Math: Signed Numbers.
Computer Science Searching & Sorting.
Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Positional Number Systems
Week 9 - Wednesday.  What did we talk about last time?  Exam 2  Before that: review  Before that: relations.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
4. Computer Maths and Logic 4.2 Boolean Logic Logic Circuits.
AP STATISTICS Objective: Understanding Randomness Do Now: Take out any completed contracts, personal profiles, as well as your written design study. HW:
Hashing – Part I CS 367 – Introduction to Data Structures.
1 CSC 143 Two' s complement. 2 10's complement  How to represent negative numbers?  Use a sign → but −0 is the same as +0  10's complement  Example.
Binary A double zero educational presentation. Binary Basics Binary is the language computers use Only 1’s and 0’s can be found in Binary Very large numbers.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
MA471 Fall 2003 Lecture 2. On With The Games Today we are going to watch each group play a couple of rounds of cards. We will go through the game slowly.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
1 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
Computer Systems Nat 4/5 Computing Science Lesson 1: Binary.
Streatham Wells Parents’ Evening Wednesday 14 October Parents, Children and Homework What can a parent do?
Do This file can be found at
Chapter 3 Whole Numbers Section 3.5 Algorithms for Whole-Number Addition and Subtraction.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
Introduction to TouchDevelop Lesson 3 – Comments & Lists Created by S. Johnson
Chance We will base on the frequency theory to study chances (or probability).
Computer Engineering page 1 Integer arithmetic Depends what you mean by “integer”. Assume at 3-bit string. –Then we define: zero = 000 one = 001 Use zero,
STARTER – CRACK THE CODE
By: Megan Funk. I will: 1. Explain the binary number system How to: -Generate binary from a number -Add binary 2. Explain the base-b number system 3.
CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;
Advanced Binary b. describe and use two’s complement and sign and magnitude to represent negative integers; c. perform integer binary arithmetic, that.
 You want to choose a professor who can write a letter that includes specifics about your personal characteristics or accomplishments rather than "glittering.
COMPUTER ORGANIZATION 4 TH LECTURE. ASCII Code  ASCII ( American Standard Code for Information Interchange).  Input and output devices that communicate.
WHAT IS BINARY? Binary is a number system that only uses two digits: 1 and 0. Any information that processed by a computer it is put into sequence of.
How hypothesis testing works
Sequences, Series, and Probability
WHEN CONFLICT HAPPENS AND IT WILL HAPPEN…….
Understanding Randomness
Subtraction The arithmetic we did so far was limited to unsigned (positive) integers. Today we’ll consider negative numbers and subtraction. The main problem.
Designing the Workflow
Lesson 26 Miscellaneous Topics
= 4 7 x = 4 27 x = x How many of you would agree with these statements? And you can be honest, I’m a recorded voice, I can’t really see you. Is multiplying.
Binary Code By Jonathan Kayne.
Randomized Algorithms
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Randomized Algorithms
Presentation transcript:

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

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 , but “forget” to carry. I then get 323.  Add but only do the carrying, rather than the addition of each digit. I then get  Add the result of the first two operations (recursively, using the same process described in step 1 and 2): = 1433.

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.

Solution

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.

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.

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.