String Methods - Thinking Like Computers - Lecture Slides, Slides of Artificial Intelligence

During the course work of Thinking Like Computers, we study the key concept of artificial intelligence. The main points in these lecture slides are:String Methods, Javascript Arrays, Separate Counters, Array of Counters, Dice Stats Page, Implementing Algorithms, Sequential Search, Binary Search, Index Variables, Writing Code, Swap List Elements

Typology: Slides

2012/2013

Uploaded on 04/24/2013

banani
banani 🇮🇳

4.3

(3)

91 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CSCI 100
Think Like Computers
Lecture 22
Fall 2008
Last Time …
String methods
charAt(i)
substring(i,j)
search(“ ”)
Javascript arrays
a[i] : element at index i.
Can be modified
Strings to array: str.split(“ ”)
Something to remember
Index starts at 0
A range (i, j) includes all numbers from i to
j-1 (exclude j)
This actually made things easy:
i=0; while ( i<a.length ) { … }
There are j-i items in range (i, j)
Loops over index is the same as loops
over a counter
Example: Dice Stats
recall our web page for
simulating repeated dice rolls
and recording the number of
times that a specific total was
obtained
statistical analysis predicts that,
given a large number of dice
rolls, the distribution of totals will
closely mirror the percentages
listed below
Approach 1: Separate
Counters
to obtain a valid distribution of dice totals, we
would need to simulate a large number of rolls
and simultaneously count the occurrences of
every total
this can be accomplished by defining 11 counters,
each corresponding to a particular total
however, this would be extremely tedious
Approach 1: Separate
Counters requires separate
assignment
statements for all 11
counters
requires a
cascading if-else
statement with 11
cases
not easily
generalized – what
if we wanted to use
8-sided dice?
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download String Methods - Thinking Like Computers - Lecture Slides and more Slides Artificial Intelligence in PDF only on Docsity!

CSCI 100

Think Like Computers

Lecture 22

Fall 2008

Last Time …

  • String methods Š charAt(i) Š substring(i,j) Š search(“ ”)
  • Javascript arrays Š a[i] : element at index i. ƒ Can be modified Š Strings to array: str.split(“ ”)

Something to remember

  • Index starts at 0
  • A range (i, j) includes all numbers from i to j-1 (exclude j)
  • This actually made things easy: Š i=0; while ( i<a.length ) { … } Š There are j-i items in range (i, j)
  • Loops over index is the same as loops over a counter

Example: Dice Stats

  • recall our web page for simulating repeated dice rolls and recording the number of times that a specific total was obtained Š statistical analysis predicts that, given a large number of dice rolls, the distribution of totals will closely mirror the percentages listed below

Approach 1: Separate

Counters

  • to obtain a valid distribution of dice totals, we would need to simulate a large number of rolls and simultaneously count the occurrences of every total Š this can be accomplished by defining 11 counters, each corresponding to a particular total Š however, this would be extremely tedious

Approach 1: Separate

Counters

Š requires separate assignment statements for all 11 counters

Š requires a cascading if-else statement with 11 cases

Š not easily generalized – what if we wanted to use 8-sided dice?

Approach 2: Array of

Counters

  • instead of representing each counter as a separate variable, we can define the counters as items in an array Š all 11 counters can be stored in an array and initialized via a single assignment statement Š any individual counter can be accessed and updated via its corresponding index ƒ since the first possible total is 2, its count is stored in index 0 ƒ the next possible total, 3, has its count stored in index 1 ƒ … ƒ for an arbitrary roll, its count is stored in index roll-

Approach 2: Array of

Counters

Š the resulting code is shorter, simpler, and easier to generalize

Dice Stats Page

  • code for maintaining statistics on repeated dice rolls can be integrated into a Web page Š the number of rolls to be simulated is entered by the user into a text box Š a button is defined to call the code for repeatedly simulating the roll and maintaining stats, with the text box contents as input Š the numbers of rolls for each total are then displayed in a (scrollable) text area

Implementing Algorithms

  • Sequential search
  • words = [“if”, “else”, “function”, “while”, “for”, “var”, “return”];
  • How to look for “function” in words?
  • function SeqSearch(list, desired) Š SeqSearch(words, “function”)

function SeqSearch(list, desired) //// Assumes: listReturns: index is of an list array where desired of items first appears, // or -1 if not found { var index; index = 0; // START AT FIRST ITEM while (index < list.length ) { // AS LONG AS ITEMS LEFT if (list[index] = desired ) { // IF NEXT ITEM IS DESIRED ONE, return index; // THEN RETURN WHERE FOUND } index = index + 1; // GO ON TO NEXT ITEM } return -1; // IF FAILED TO FIND, RETURN - }

Any error?

Binary Search

  • What is the idea?
  • Need: an already sorted list
  • [“Charlie”, “Dave”, “Jack”, “Jerald”, “Jerroll”, “Laura”, “Lois”, “Winnie”]
  • binarySearch(list, target)

Writing the Code

  • Inside the loop:
  • (1) compare list[mid] and target
  • if ( list[mid] == target ) return mid;
  • (2) // otherwise, update the range
  • else if ( list[mid] > target ) {
  • right = mid;
  • } else {
  • left = mid+1;
  • }
  • (3) don’t forget the mid:
  • mid = Math.floor( (left+right)/2 );

Writing the Code

  • If loop is finished (did not return during the loop)
  • Then target is not in the list!
  • return -1;
  • That’s it!

How to Sort?

  • Any idea?
  • list = [ 3, 20, 11, 13, 8, 10, 6, 2];

First, How to Swap?

  • You’ll need a temporary variable:
  • var temp;
  • temp = x;
  • x = y;
  • y = temp;
  • Notice the sequence: first save/write, then you can modify (and lose its original value)

Swap List Elements

  • Swap elements at index i and j:
  • var temp;
  • temp = list[i];
  • list[i] = list[j];
  • list[j] = temp;