CS 115 FINAL Review Session

35
24/02/10 CS 115 FINAL Review Session

description

CS 115 FINAL Review Session. Srinidhi Sridharan. 2A Mathematics and Business Admin. Double Degree Major in Accounting, Minor in Computer Science Employers GE Capital: Assistant Account Manager (Risk Management) KPMG: Staff Accountant (Audit) Email: [email protected]. - PowerPoint PPT Presentation

Transcript of CS 115 FINAL Review Session

Page 1: CS 115  FINAL  Review Session

24/02/10

CS 115 FINAL Review Session

Page 2: CS 115  FINAL  Review Session

Srinidhi Sridharan

• 2A Mathematics and Business Admin. Double Degree

• Major in Accounting, Minor in Computer Science

• Employers- GE Capital: Assistant Account Manager (Risk Management)- KPMG: Staff Accountant (Audit)

Email: [email protected]

Page 3: CS 115  FINAL  Review Session

Topics to Cover

• Structures• Lists

• Recursion• Lists of Lists

• Processing Two Lists• Binary Trees

• Binary Search Trees• Mutual Recursion

• Local• Functional Abstraction

Page 4: CS 115  FINAL  Review Session

Structures

A structure is a way of bundling data together to form a single package

1. You can create functions that consume and or produce structures

2. Define our own structures3. Each individual structure has a

• Constructor function• Selector Function• Type Predicate Function

Page 5: CS 115  FINAL  Review Session

Structures

To define a structure:(define-struct sname (field1 field2.....))

Doing this gives you three functionsmake-sname ;constructorsname-field1, sname-field2....;selectorssname? ;type predicate

Page 6: CS 115  FINAL  Review Session

Structure Template

(define (my-structure-fn a-struct)

...(structure-f1 a-struct)...

...(structure-f2 a-struct)...

...

...(structure-fn a-struct)...)

Page 7: CS 115  FINAL  Review Session

Structure vs Data Definition

A Data Definition: is a comment specifying the types of data;;a posn is a structure (make-posn xcoord ycoord), where;;xcoord is a number;;ycoord is a number

Structure Definitions: show what each item represents(define-struct contact (name email-address));; A contact is a structure (make-contact n e) where;; n is a non empty string representing a person's name and‐;; e is a non empty string representing a person's email address.‐

Page 8: CS 115  FINAL  Review Session

List

A list is either:•empty or•(cons f r), where

*f is a value and*r is a list

The CS Data Definition My ExplanationBreak every list in CS into1. The first element2. The rest of the list*the rest of the list is also a listThe empty list is just: empty

A list in scheme looks like this:(cons ‘a (cons ‘b (cons ‘c (cons ‘d empty))))empty(list ‘a ‘b ‘c ‘c)

Page 9: CS 115  FINAL  Review Session

List Template

(define (mylist alist)(cond [(empty? alist)...] [(cons? alist) ...]))

Page 10: CS 115  FINAL  Review Session

Introduction to Recursion

Recursion in CS: The continuous application of a function on a variable that is defined in terms of a past variable

Recursive Process:Base Case: The starting & ending point of the function

Recursive Part: Only if your base case failsWhat do you want to change if your base case fails?Keep doing this until your base case passes

Page 11: CS 115  FINAL  Review Session

Introduction to Recursion

A general recursive template(define (my-recurse int)

(cond[(base-case? int) ...][ else ...... (my-recurse ......)]))

Defining A Base Case

Store/Alter The Variable Re-Apply Function on Changed Variable

Page 12: CS 115  FINAL  Review Session

Problem 1

Give the structure templates for the following structures. Then, define a function that consumes

a list of CD structures and produces the total price, using the template for CD.

(define-struct movie (title producer))(define-struct CD (artist title price))

All my solutions are on the downloadable PDF package

Page 13: CS 115  FINAL  Review Session

List of List ... Diagrams

(list (list 4 2) (list 1 7) 6)

Page 14: CS 115  FINAL  Review Session

List of List Template

(define (my-list-fn a-list)

(cond ((empty? a-list) ...)

((cons? (first a-list)) ...)

(else ...(first a-list)...(my-list-fn (rest a-list)))))

Page 15: CS 115  FINAL  Review Session

List of Lists: Association List

• AKA. Dictionaries

• Each list contains smaller lists with two elements (list (list a 5) (list srin 6) (list srinidhi 19))

• One represents a key, the other represents a value that is to be associated with a key

• An al is either empty or (cons (list k v) alst) where k is a number (key), v is a string (value) and alst is an al

Page 16: CS 115  FINAL  Review Session

Problem 2

Consume an association list and a key and find the value that corresponds to that key, or return false if

the key is not found.

All my solutions are on the downloadable PDF package

Page 17: CS 115  FINAL  Review Session

Processing Two Lists

Three Methods for Processing Two Lists

1) Have a list “go along for the ride”2) Processing in Lockstep (only for lists of equal lengths)3) Processing Lists at Different Rates

We’re going to do examples of each of these types!

Page 18: CS 115  FINAL  Review Session

Problem 3

Going along for the rideDevelop a function, cross, that consumes a list of symbols and a list of numbers and produces all

possible pairs of symbols and numbers.

Page 19: CS 115  FINAL  Review Session

Lockstep Template

(define (my-list-fn lst1 lst2)

(cond

((empty? lst1) ...)

(else ... (first lst1) ... (first lst2)...

... (my-lst-fn (rest lst1) (rest lst2))))

Page 20: CS 115  FINAL  Review Session

Problem 4

LockstepDevelop the function zip, which combines a list of names and a list of phone numbers into a list of

phone records. The lists are of the same size. Assume the following structure definition:

(define-struct phone-record (name number));;A phone-record is a structure (make-phone-record s n) ;; where s is a string representing a person’s name ;; n is a number representing their number

Page 21: CS 115  FINAL  Review Session

Processing Different Rates

These problems have basic templates that tend to work really well!

(define (my-lst-fn lst1 lst2)

(cond

((and (empty? lst1) (empty? lst2)) ...)

((and (cons? lst1) (empty? lst2)) ...)

((and (empty? lst1) (cons? lst2)) ...)

((and (cons? lst1) (cons? lst2)) ...)))

Page 22: CS 115  FINAL  Review Session

Problem 5

Processing Lists at Different RatesThe function DNAprefix consumes two lists of symbols

called pattern and search-string and returns true if pattern is a prefix of search-string, and false otherwise.

The modified function returns the first item beyond the pattern in the search-string if the pattern is a proper prefix

of search-string. Otherwise, it returns false.

Page 23: CS 115  FINAL  Review Session

Binary Trees

(define-struct node (key val left right)

A binary tree is either EMPTYOr

(make-node k v l r)k is a number => like the key from an association listv is a value => like the value from an association listl => is a binary treer => is a binary tree

Page 24: CS 115  FINAL  Review Session

Binary Search Trees

(define-struct node (key val left right)

A binary tree is either EMPTYOr

(make-node k v l r)k is a number => like the key from an association listv is a value => like the value from an association listl => is a binary tree, with keys less than kr => is a binary tree with keys greater than k

Page 25: CS 115  FINAL  Review Session

Binary Search Trees

Page 26: CS 115  FINAL  Review Session

Problem 6

Develop the function, contains-bt. The function consumes a binary tree and a number n and

determines whether the number occurs in the tree.

(define-struct node (key val left right))

Page 27: CS 115  FINAL  Review Session

Problem 7

Develop the function, contains-bst. The function consumes a binary search tree and a number n and determines whether the number occurs in the tree.

(define-struct node (key val left right))

Page 28: CS 115  FINAL  Review Session

Mutual Recursion

You are “referring” to another function when you make your recursive calls.

(define-struct ae (fn args))-A number or- (make-ae f alist), where f is a symbol, and alist is an aexplist

An aexplist is either-Empty or- (cons a alist), where a is an aexp, and alist is an aexplist

Page 29: CS 115  FINAL  Review Session

Mutual Recursion(define (eval ex)

(cond ((number? ex) ex)

(else (eval/list (ae-fn ex) (ae-args ex)))))

(define (eval/list f exlist)

(cond ((empty? exlist) ...)

((equal? f '*)(* (eval (first exlist)) (eval/list f (rest exlist))))

((equal? f '+)(+ (eval (first exlist)) (eval/list f (rest exlist))))))

Page 30: CS 115  FINAL  Review Session

Mutual Recursion

If you have two structures that refer to each other in their

definitions, it's usually a sign that mutual recursion should be used.

Page 31: CS 115  FINAL  Review Session

Local

Benefits of local definitions:– hide helper functions inside main function– improve efficiency– The exact helper functions you were using before can now

be contained in the code

(define (swap-parts mystring) (local ((define len (string-length mystring)) (define mid (quotient len 2)) (define front (substring mystring 0 mid)) (define back (substring mystring mid len)))) (string-append back front)))

Page 32: CS 115  FINAL  Review Session

Functional Abstraction

The filter function.

filter takes a question and a list; for every element in the listthat evaluates the question to true, filter returns those elements in a new list.

Contract: (X → boolean) (list of X) → (list of X)

Example: (filter even? (list 1 2 3 4)) → (list 2 4)

Page 33: CS 115  FINAL  Review Session

Problem 8

Create the function perfect-squares that consumes a list of non negative integers, lon, and produces a list

of all those elements from lon that are perfect squares

Page 34: CS 115  FINAL  Review Session

Functional Abstraction

The map functionmap takes a list of numbers and a function, and applies thefunction to the list of numbers.

Contract: (X → Y) (list of X) → (list of Y)

Example: (map add1 (list 1 2 3)) → (list 2 3 4)

Page 35: CS 115  FINAL  Review Session

Functional Abstraction

The foldr functionfoldr takes a list and function, then reduces the list to a singlevalue using the function.

Contract: (X → X) X (list of X) → X

Example: (foldr max 0 (list 5 6 3 4 3 2)) → 6