LINKED LISTS AND MIDTERM REVIEW

Similar documents
LINKED LISTS AND MIDTERM REVIEW 6

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

CS 61A Orders of Growth & Linked Lists Spring 2018 Discussion 6: March 7, Warmup

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

STREAMS, ITERATORS, AND BINARY TREES 10

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

Solutions to CS 61A Challenge Problems: Midpoint Review

Structure and Interpretation of Computer Programs

April 2 to April 4, 2018

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Structure and Interpretation of Computer Programs

61A Lecture 21. Friday, March 13

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Midterm 2 Review Fall 2017 October 13, Lists & Tree Recursion. Instructions

OOP 1 OOP CS 61A GROUP MENTORING. July 19, 2017

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

MEMOIZATION, RECURSIVE DATA, AND SETS

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

GENERATORS AND STREAMS

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Lecture #21: Search Trees, Sets. Last modified: Tue Mar 18 18:15: CS61A: Lecture #21 1

Structure and Interpretation of Computer Programs

Tree-Structured Data. A tree can contains other trees: [5, [6, 7], 8, [[9], 10]] (+ 5 (- 6 7) 8 (* (- 9) 10))

61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS

Guerrilla Section 5: Object Oriented Programming, Nonlocal & Mutable Trees

Lists, Mutability, ADTs, and Trees Spring 2019 Guerrilla Section 2: March 2, 2019 Solutions. 1 Sequences. Questions. lst = [1, 2, 3, 4, 5] lst[1:3]

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Structure and Interpretation of Computer Programs

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

OOP, Nonlocal, Trees, LLs, Growth Spring 2019 Guerrilla Section 3: March 16, 2019 Solutions 1 OOP. Questions

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Structure and Interpretation of Computer Programs

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

RLISTS AND THE ENVIRONMENT MODEL 9

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CSI33 Data Structures

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

ENVIRONMENT DIAGRAMS AND RECURSION 2

INTERPRETERS AND TAIL CALLS 9

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

STREAMS AND REVIEW 12

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

TAIL RECURSION, SCOPE, AND PROJECT 4 11

FINAL REVIEW 13. December 4, Sample Final Questions

Binary Search Tree. Revised based on textbook author s notes.

Structure and Interpretation of Computer Programs

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

TREES AND ORDERS OF GROWTH 7

Lists, Mutability, ADTs, and Trees Spring 2019 Guerrilla Section 2: March 2, Sequences. Questions. lst = [1, 2, 3, 4, 5] lst[1:3]

Last name... First name... Q1: / 5 Q2: / 6 Q3: / 9

Structure and Interpretation of Computer Programs

UNIVERSITY OF TORONTO Faculty of Arts and Science

Structure and Interpretation of Computer Programs

CSM Mock Final Spring 2018

Structure and Interpretation of Computer Programs

Midterm Review. CS61A Summer Katya Stukalova Jerome Baek

ITERATORS AND GENERATORS 10

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

CS 1110 Final, December 17th, Question Points Score Total: 100

Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections)

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

TREES AND ORDERS OF GROWTH 8

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 24 Infinite Sequences LAZY EVALUATION AGENDA 8/7/2012. Jom Magrotker UC Berkeley EECS July 30, 2012

Structure and Interpretation of Computer Programs

CS61A Lecture 24 Infinite Sequences. Jom Magrotker UC Berkeley EECS July 30, 2012

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Abstract Data Types Chapter 1

TREE RECURSION AND DATA ABSTRACTION

TUPLES AND RECURSIVE LISTS 5

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

6.01, Spring Semester, 2008 Assignment 3, Issued: Tuesday, February 19 1

RECURSION, RECURSION, (TREE) RECURSION! 2

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

CONTROL AND ENVIRONMENTS 1

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

61A Lecture 32. Wednesday, November 14

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

PREPARING FOR PRELIM 1

RECURSION 3. 1 Recursion COMPUTER SCIENCE 61A. June 30, 2016

Structure and Interpretation of Computer Programs

SQL AND FINAL REVIEW

Midterm 1 Review Fall 2017 September 8, Functions. Instructions

Introduction to Problem Solving and Programming in Python.

Midterm 2 Exam Principles of Imperative Computation. Tuesday 31 st March, This exam is closed-book with one sheet of notes permitted.

Context Free Grammars and Recursive Descent Parsing

RECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012

Generic BST Interface

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Transcription:

LINKED LISTS AND MIDTERM REVIEW COMPUTER SCIENCE MENTORS 61A March 12 to March 14, 2018 For each of the following problems, assume linked lists are defined as follows: class Link: empty = () def init (self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Linked Lists To check if a Link is empty, compare it against the class attribute Link.empty: if link is Link.empty: print('this linked list is empty!') 1

GROUP TUTORING HANDOUT 5: LINKED LISTS AND MIDTERM REVIEW Page 2 1. What will Python output? Draw box-and-pointer diagrams to help determine this. = Link(1, Link(2, Link(3))).first.first = 5.first.rest.first.rest.rest.rest.rest.first.rest.rest.rest = a.rest.rest.rest.rest.first

GROUP TUTORING HANDOUT 5: LINKED LISTS AND MIDTERM REVIEW Page 3 2. Write a function skip, which takes in a Link and returns a new Link with every other element skipped. def skip(lst): = Link(1, Link(2, Link(3, Link(4)))) Link(1, Link(2, Link(3, Link(4)))) >>> b = skip(a) >>> b Link(1, Link(3)) Link(1, Link(2, Link(3, Link(4)))) # Original is unchanged if : : elif : 3. Now write function skip by mutating the original list, instead of returning a new list. Do NOT call the Link constructor. def skip(lst): = Link(1, Link(2, Link(3, Link(4)))) >>> b = skip(a) >>> b None Link(1, Link(3))

GROUP TUTORING HANDOUT 5: LINKED LISTS AND MIDTERM REVIEW Page 4 4. Write a function reverse, which takes in a Link and returns a new Link that has the order of the contents reversed. Hint: You may want to use a helper function if you re solving this recursively. def reverse(lst): = Link(1, Link(2, Link(3))) >>> b = reverse(a) >>> b Link(3, Link(2, Link(1))) Link(1, Link(2, Link(3))) Midterm Review For each of the following problems, assume the Tree class is defined as follows: class Tree: def init (self, label, branches=[]): self.label = label self.branches = branches def is_leaf(self): return not self.branches

GROUP TUTORING HANDOUT 5: LINKED LISTS AND MIDTERM REVIEW Page 5 1. Write a function that returns true only if there exists a path from root to leaf that contains at least n instances of elem in a tree t. def contains_n(elem, n, t): >>> t1 = Tree(1, [Tree(1, [Tree(2)])]) >>> contains(1, 2, t1) True >>> contains(2, 2, t1) False >>> contains(2, 1, t1) True >>> t2 = Tree(1, [Tree(2), Tree(1, [Tree(1), Tree(2)])]) >>> contains(1, 3, t2) True >>> contains(2, 2, t2) # Not on a path False if n == 0: return True elif : return elif t.label == elem: return else: return

GROUP TUTORING HANDOUT 5: LINKED LISTS AND MIDTERM REVIEW Page 6 2. Define the function factor_tree which returns a factor tree. Recall that in a factor tree, multiplying the leaves together is the prime factorization of the root, n. See below for an example of a factor tree for n = 20. def factor_tree(n): for i in : if : return Tree(, )

GROUP TUTORING HANDOUT 5: LINKED LISTS AND MIDTERM REVIEW Page 7 3. Draw the environment diagram that results from running the following code. If the code errors, draw the environment diagram up to the point that the error occurs. earth = [0] earth.append([earth]) def wind(fire, groove): fire[1][0][0] = groove def fire(): nonlocal fire fire = lambda fantasy: earth.pop(1).extend(fantasy) return fire(groove) return fire() sep = earth[1] wind(earth, [earth[0]] + [earth.append(0)])