Cs1 final exam review
Download
1 / 43

CS1 Final Exam Review - PowerPoint PPT Presentation


  • 268 Views
  • Updated On :

CS1 Final Exam Review By Rebecca Schulman December 4, 2002 Quick Overview Topics from the first part of term will not be explicitly covered on the exam, but if you do not understand this material, you will have trouble with the exam Substitution model Standard vs. Special Forms

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS1 Final Exam Review' - benjamin


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Cs1 final exam review l.jpg

CS1 Final Exam Review

By Rebecca Schulman

December 4, 2002


Quick overview l.jpg
Quick Overview

  • Topics from the first part of term will not be explicitly covered on the exam, but if you do not understand this material, you will have trouble with the exam

  • Substitution model

  • Standard vs. Special Forms

  • Higher order procedures

  • Asymptotic Complexity


Slide3 l.jpg

Make sure you understand the topics from the second half of the course. These include:

  • Data structures: lists, trees, and data processing

  • Message Passing

  • Operations with Mutation

  • Environment Diagrams

  • Tagged Data


Exam structure l.jpg
Exam Structure the course. These include:

  • You will have to answer one question on each major concept.

  • There will be two “tracks” on the exam, so if you do both, your score will be the minimum of your best score for each concept


The substitution model l.jpg
The Substitution Model the course. These include:

Three steps:

  • Evaluate the operands

  • Evaluate the operator

  • Apply the operator to the operands


Substitution and mutation l.jpg
Substitution and Mutation the course. These include:

  • We did not get rid of the substitution model when we introduced mutation. But we did make an important change:

  • We do not substitute the value for parameters into an expression when we apply. Instead, when they are needed, we look up the value of a parameter in the environment


More substitution and mutation l.jpg
More Substitution and Mutation the course. These include:

  • Remember also that begin statements mustbe evaluated in left to right order

  • These type of expressions include the body of begin clauses, but also the consequent portion of cond statements and the body of let expressions


Special forms l.jpg
Special Forms the course. These include:

  • Some scheme expressions do not obey the substitution model. They require operands to be evaluated in a certain order, or require some operands to not be evaluated at all before application


A list of special forms l.jpg
A List of Special Forms the course. These include:

  • define: Evaluate only the second operand, and associate its value with the first operand, which should be a variable

  • if : Evaluate the predicate and only evaluate the clause pertaining to whether the predicate is true or false

  • cond: Evaluate only the predicates, until a true one is found, and then return the value of the consequent expression that matches


More special forms l.jpg
More Special Forms the course. These include:

  • let: Evaluate the values in the binding section and associate them with their corresponding variables inside the let environment

  • quote: The result of the expression is a symbol with the name given as the sole argument

  • set!: Only evaluate the second expression, and change the value associated with the variable to be this result


Asymptotic complexity l.jpg
Asymptotic Complexity the course. These include:

  • We use O notation to talk about approximately how long it will take a procedure to complete

  • No formal methods are required for CS1: just an informal counting should be enough

  • Example run times we saw were O(n), O(n2), O(n log n) and O(2n)


Symbols l.jpg
Symbols the course. These include:

  • We can represent a name using symbols in Scheme. A symbol is created using the quote special form

    bob => error: Unbound variable bob

    (quote bob) => bob

  • We abbreviate quote with the ‘ character

    ‘bob => bob


Cons pairs l.jpg
cons the course. These include:pairs

  • cons pastes together two elements. By using it recursively, we can create lists, trees and any other data structure we might like

  • For example, a list of 1,2, and 3 would be:

    (cons 1 (cons 2 (cons 3 nil)))


Box and pointer diagrams l.jpg
Box and Pointer Diagrams the course. These include:

  • We illustrate cons pairs using a pair of boxes. Each box points to its contents

  • cons cells can point to numbers, symbols, or other cons pairs, among other things


List processing l.jpg
List processing the course. These include:

In class we talked about algorithms for

  • Adding items from a list

  • Removing items from a list

  • Searching for items in a list

  • Doing the above with and without mutation


Data representation l.jpg
Data Representation the course. These include:

  • Once we have the ability to represent collections of things, we’re left with the obvious question of how to organize it.

  • We spent the next several weeks in CS1 thinking about several ways to do this.


Abstraction barriers l.jpg
Abstraction Barriers the course. These include:

  • The simplest technique we discussed is separating the representation of the data from its meaning

  • This requires creating an explicit representation, and creating procedures that can interact with the data by creating it and accessing it by meaning, rather than by structure


Tagged data l.jpg
Tagged Data the course. These include:

  • We used tagged data in order to label numbers so that they had meaning. For example:

    (make-dollars 40) => (dollars . 40)

    (make-pounds 100) => (pounds . 100)


Generic operations l.jpg
Generic Operations the course. These include:

  • We then extended this idea to being able to do operations on data in different units

    (add-money (make-pounds 100)

    (make-dollars 40)) =>

    (dollars . 196.7)

    (as of today)


Message passing l.jpg
Message Passing the course. These include:

  • Message Passing allowed us to encapsulate data and operations into the same object


Mutation l.jpg
Mutation the course. These include:

  • Mutation is the ability to change the value of a variable, once it has been defined. This is something that we don’t do in math, and it caused us to make our old substitution model more complicated


The environment model l.jpg
The Environment Model the course. These include:

  • We extended the substitution model by no longer using substitution to associate variables with their value, but by creating a set of environments

  • We still evaluate and apply procedures in the environment model but we introduced two new concepts


Environment model 2 l.jpg
Environment Model (2) the course. These include:

  • Procedures are explicitly created in the environment model, and are evaluated in a particular environment that captures their local state

  • Instead of substituting variables at the time of application, we look each one up the environment as needed


Environment model rules l.jpg
Environment Model Rules the course. These include:

  • Binding variables: Bind simple variables in the current environment

  • Procedures are created when they are defined. They form a pair, one of which points to its body, the other points to the enviroment where the procedure is created


Environment model rules 2 l.jpg
Environment Model Rules (2) the course. These include:

  • Applying a procedure creates a new environment in which the parameters of the procedure are bound to the operand

  • set! changes the value of the variable that is referred to in the current environment


Procedures as local state l.jpg
Procedures as Local State the course. These include:

  • The first way we used mutation was to create procedures that hold state. We did this by creating procedures that had their own environment. For example:

    (define (make-accum initial)

    (let ((valueinitial))

    (lambda (change)

    (set!value (+valuechange))

    value)))


Mutation of data l.jpg
Mutation of Data the course. These include:

  • The other way we learned how to do mutation was to change data structures

  • We did this with set-car! and set-cdr!

  • set-car! points the first part of a cons pair to the same thing pointed to by its second argument, and set-cdr! does the same to the cdr part of the cons pair


Eq and equal when is the whole not the sum of its parts l.jpg
eq? the course. These include: and equal?: When is the whole not the sum of its parts?

  • eq? tests whether the two whole objects are the same

  • equal? tests whether the parts of two data structures are all the same

  • Two objects that can be equal but not eq would be:

  • (define a ‘(1 2 3))

  • (define b ‘(1 2 3))


Extended example gambling l.jpg
Extended Example: Gambling the course. These include:

  • We’ll go on an extended exercise to pit your scheme skills against the casinos and try not to lose all of your money…


A blackjack game l.jpg
A Blackjack Game the course. These include:

You decide to test your strategy ideas in simulation first to see how much money you will win or lose. Dividing this into a few tasks we will:

  • Build a message-passing deck of cards

  • Build a few blackjack players

  • And a table, that will simulate the playing of many games


The deck of cards l.jpg
The Deck of Cards the course. These include:

  • We begin with the ranks and suits

    (definesuits '(clubs diamonds hearts spades))

    (defineranks '(A 2 3 4 5 6 7 8 9 10 J Q K))

    The deck of cards are simply all combinations of these


List processing all combinations l.jpg
List processing: the course. These include: all-combinations

(define (all-combinations proc first second)

(define (helper abcurrent)

(cond ((null? a) current)

((null? (cdrb)) (helper (cdra) second

(cons (proc (cara) (carb))

current)))

(else (helper a (cdrb) (cons (proc (cara) (carb))

current)))))

(helper first second (list)))


A deck of cards l.jpg
A deck of cards the course. These include:

  • A deck of cards is simply the this applied to a single make-card procedure

    (define deck-of-cards

    (all-combinations make-card suits ranks))

    (define (decks-of-cards n)

    (if (= n 0)

    (list)

    (append deck-of-cards

    (decks-of-cards (- n 1)))))


Shuffling l.jpg
Shuffling the course. These include:

  • Shuffling cards can be reduced to transposing each element so that it ends up in a random position. We’ll do this functionally, to show some more list processing techniques


Slide35 l.jpg

(define (transpose-two-elements lst x y) the course. These include:

(let ((xth (nth-element lst x))

(yth (nth-element lst y)))

(define (transpose-helper current n result)

(cond ((null? current) result)

((= n x)

(transpose-helper (cdr current)

(+ n 1)

(append result (list yth))))

((= n y)

(transpose-helper (cdr current)

(+ n 1)

(append result (list xth))))

(else

(transpose-helper (cdr current)

(+ n 1)

(append result (list (car current)))))))

(transpose-helper lst 1 (list))))


Shuffling 2 l.jpg
Shuffling (2) the course. These include:

Shuffling is simple now that we have the transposition procedure:

(define (shuffle list-of-cards)

(define (shuffle-helper position current-cards)

(if (= position 0) current-cards

(let ((other-element (random-1-to-n position)))

(shuffle-helper (- position 1)

(transpose-two-elements

current-cards

position

other-element)))))

(shuffle-helper (length list-of-cards)

list-of-cards))


The deck message passing l.jpg
The Deck (Message Passing) the course. These include:

(define (make-decks deck-count)

(let ((deck (shuffle (decks-of-cards deck-count))))

(define (draw-cards n result)

(if (= n 0) result

(begin (let ((next (cons (car deck) result)))

(set! deck (cdr deck))

(draw-cards (- n 1) next)))))

(define (enough-cards deck arg) (>= (length deck) arg))

(lambda (message arg)

(cond ((eq? message 'draw)

(if (not (enough-cards deck arg))

(set! deck (shuffle (decks-of-cards deck-count))))

(draw-cards arg (list)))))))


The blackjack dealer l.jpg
The Blackjack Dealer the course. These include:

(define (blackjack-dealer deck)

(define (hit current-hand)

(if (>= (score current-hand) 17)

current-hand)

(hit (append (deck 'draw 1) current-hand))))

(hit (list)))


The blackjack player l.jpg
The Blackjack Player the course. These include:

(define (good-player deck dealers-card)

(define (hit current-hand)

(cond ((> (score current-hand) 17) current-hand)

((< (score current-hand) 12) (hit (append (deck 'draw 1)

current-hand)))

((memq (score current-hand) '(13 14 15 16))

(if (< (card-value dealers-card) 7) current-hand

(hit (append (deck 'draw 1) current-hand))))

(else (if (memq (card-value dealers-card) '(4 5 6)) current-hand

(hit (append (deck 'draw 1) current-hand))))))

(hit (list)))


Playing a game 1 l.jpg
Playing a Game (1) the course. These include:

(define (make-blackjack-table player deck-count house-cut)

(let ((deck (make-decks deck-count))

(bet 10) (takings 0))

(define (bust? hand) (> (score hand) 21))

(define (blackjack? hand)

(and (has-ace? hand) (has-face-card? hand)))

(define (play-round)

(let ((dealers-hand (blackjack-dealer deck)))

(let ((players-hand (player deck (showing-card dealers-hand))))

(cond ((bust? players-hand) -1)

((bust? dealers-hand) (- 1 house-cut))

((blackjack? players-hand) 1.5)

((> (score players-hand)

(score dealers-hand)) (- 1 house-cut))

(else -1)))))


Playing a game 2 l.jpg
Playing a Game (2) the course. These include:

(lambda (message)

(cond ((eq? message 'play)

(let ((result (play-round)))

(set! takings

(+ takings (* result bet)))))

((eq? message 'takings)

takings)))))


The game s environment the beginning l.jpg
The Game’s Environment: The beginning the course. These include:


The game in play l.jpg
The Game in Play the course. These include:


ad