a semantic error in google last weekend! someone in google typed an extra ‘/’ character into...

19
A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

Upload: elwin-owen

Post on 31-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

A Semantic Error in Google last weekend!

Someone in Google typed an extra ‘/’ character into their URL List

Link to CNN video report posted on Collab

Page 2: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

Recap of Arrays

Page 3: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

To make an array: declare, create, and initialize it. To access element i of array named a, use a[i]. Array indices start at 0.

Compact alternative.

3

Arrays in Java

Indexed sequence of values of the same type To make an array: declare, create, and initialize it. To access element i of array named a, use a[i]. Array indices start at 0.

int N = 10; double[] a; // declare the arraya = new double[N]; // create the arrayfor (int i = 0; i < N; i++) // initialize the array a[i] = 0.0; // all to 0.0

int N = 10;double[] a = new double[N]; // declare, create, init

Page 4: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

Memory Representation and Allocation of Arrays

Arrays occupy consecutive addresses in the program address space:

int[] a = {5, 6, 10};

Arrays need to be explicitly allocated memory.

Arrays cannot grow and cannot shrink

Name of the array indirectly references its starting address.

int[] b = new int[3]; b=a;//b references the SAME data in memory.

4

0 1 2 3 4 5 N-2 N-1 a[0] a[1] a[2] 5 6 10

Addresses

Page 5: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

Examples of Programs that Use Arrays

5

Page 6: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

Shuffling a Deck of Cards

Page 7: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

7

Setting Array Values at Compile Time

Ex. Print a random card.

String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades"};

int i = (int) (Math.random() * 13); // between 0 and 12int j = (int) (Math.random() * 4); // between 0 and 3

System.out.println(rank[i] + " of " + suit[j]);

Page 8: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

8

Setting Array Values at Run Time

Ex. Create a deck of playing cards and print them out.

String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades"};

String[] deck = new String[52];for (int i = 0; i < 13; i++) for (int j = 0; j < 4; j++) deck[4*i + j] = rank[i] + " of " + suit[j];

for (int i = 0; i < 52; i++) System.out.println(deck[i]);

Page 9: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

9

Shuffling

Goal. Given an array, rearrange its elements in random order.

Shuffling algorithm. In iteration i, pick random card from deck[i] through deck[N-1], with each card equally likely.

Exchange it with deck[i].

Page 10: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

Shuffle an ArrayIteration i=0

Shuffle a deck of cards. In ith iteration, choose a random element from remainder of

deck and put at index i.– choose random integer r between i and N-1– swap values in positions r and i

4 5 6 2 3 10 JValue 8 9

2 3 4 50 1 8 9Array index 6 7

9 2

random integer = 7

Page 11: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

Shuffle an ArrayIteration i=1

Shuffle a deck of cards. In ith iteration, choose a random element from remainder of

deck and put at index i.– choose random integer r between i and N-1– swap values in positions r and i

4 5 6 9 3 10 JValue 8 2

2 3 4 50 1 8 9Array index 6 7

5 3

random integer = 3

Page 12: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

Shuffle an ArrayIteration i=2

Shuffle a deck of cards. In ith iteration, choose a random element from remainder of

deck and put at index i.– choose random integer r between i and N-1– swap values in positions r and i

4 3 6 9 5 10 JValue 8 2

2 3 4 50 1 8 9Array index 6 7

J 4

random integer = 9

Page 13: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

After the Final Iteration

Shuffle a deck of cards. In ith iteration, choose a random element from remainder of

deck and put at index i.– choose random integer r between i and N-1– swap values in positions r and i

J 4 8 39 5 6 2Value 10 7

2 3 4 50 1 8 9Array index 6 7

Page 14: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

14

Shuffling

In iteration i, pick random card from deck[i] through deck[N-1], with each card equally likely.

Exchange it with deck[i].

• The cards in the deck should be the same as those before the shuffle.

• Need to pick a random card from those not already chosen for the shuffle.

int N = deck.length;for (int i = 0; i < N; i++) { int r = i + (int) (Math.random() * (N-i)); String t = deck[r]; deck[r] = deck[i]; deck[i] = t;}

swapidiom

Page 15: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

15

Shuffling a Deck of Cards

public class Deck { public static void main(String[] args) { String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; int SUITS = suit.length; int RANKS = rank.length; int N = SUITS * RANKS;

String[] deck = new String[N]; for (int i = 0; i < RANKS; i++) for (int j = 0; j < SUITS; j++) deck[SUITS*i + j] = rank[i] + " of " + suit[j];

for (int i = 0; i < N; i++) { int r = i + (int) (Math.random() * (N-i)); String t = deck[r]; deck[r] = deck[i]; deck[i] = t; }

for (int i = 0; i < N; i++) System.out.println(deck[i]); }}

build the deck

shuffle

print shuffled deck

Dr. Java Demo

Page 16: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

Coupon Collector

Page 17: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

17

Coupon Collector Problem

Coupon collector problem. Given a shuffled deck of cards and N different card types, how many do you have to collect before you have (at least) one of each type? (Assume each possibility is equally likely for each card you collect)

Simulation algorithm. Repeatedly choose an integer i between 0 and N-1.Stop when we have at least one card of every type.

Q. How to check if we've seen a card of type i?A. Maintain a boolean array so that found[i] is true if we've already collected a card of type i.

Page 18: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

18

Coupon Collector: Java Implementation

public class CouponCollector { public static void main(String[] args) { int N = Integer.parseInt(args[0]); // value range int cardcnt = 0; // number of cards collected int valcnt = 0; // number of distinct cards // do simulation boolean[] found = new boolean[N]; while (valcnt < N) { int val = (int) (Math.random() * N); cardcnt++; if (!found[val]) valcnt++; found[val] = true; } System.out.println(cardcnt); }}

type of next card(between 0 and N-1)

Dr. Java Demo

Page 19: A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

19

Coupon Collector: Scientific Context

Q. Given a sequence from nature, does it have same characteristicsas a random sequence?

A. No easy answer - many tests have been developed.

Coupon collector test. Compare number of elements that need to be examined before all values are found against the corresponding answer for a random sequence.