LINKED LISTS AND MIDTERM REVIEW 6

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

LINKED LISTS AND MIDTERM REVIEW

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

April 2 to April 4, 2018

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

SEQUENCES AND TREES 4

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

RLISTS AND THE ENVIRONMENT MODEL 9

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

Structure and Interpretation of Computer Programs

MEMOIZATION, RECURSIVE DATA, AND SETS

61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS

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

Solutions to CS 61A Challenge Problems: Midpoint Review

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

INTERPRETERS AND TAIL CALLS 9

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

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

TREES AND ORDERS OF GROWTH 7

UNIVERSITY OF TORONTO Faculty of Arts and Science

20 Some terminology regarding trees: Parent node: A node that has branches. Parent nodes can have multiple branches.

Structure and Interpretation of Computer Programs

QuickSort

Structure and Interpretation of Computer Programs

TREES AND ORDERS OF GROWTH 8

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

TREES AND ORDERS OF GROWTH 7

ITERATORS AND GENERATORS 10

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Structure and Interpretation of Computer Programs

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

STREAMS, ITERATORS, AND BINARY TREES 10

Graphs & Digraphs Tuesday, November 06, 2007

Structure and Interpretation of Computer Programs

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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

61A Lecture 32. Wednesday, November 14

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CSM Mock Final Spring 2018

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Structure and Interpretation of Computer Programs

Midterm Review. CS61A Summer Katya Stukalova Jerome Baek

Structure and Interpretation of Computer Programs

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

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

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Structure and Interpretation of Computer Programs

Programming II (CS300)

Assignment 2. CS 234 Fall 2018 Sandy Graham. Create()

Sorting and Selection

Data Structures and Algorithms 2018

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

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

Real-world sorting (not on exam)

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

Lab 6 Sorting. Sup Biotech 3 Python. Pierre Parutto

COT 5407: Introduction. to Algorithms. Giri NARASIMHAN. 1/29/19 CAP 5510 / CGS 5166

Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

CSCI-140 Midterm Review October 10, 2015 Presented by the RIT Computer Science Community

61A Lecture 21. Friday, March 13

Structure and Interpretation of Computer Programs

CS-141 Final Exam Review December 8, 2017 Presented by the RIT Computer Science Community

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Structure and Interpretation of Computer Programs

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

An introduction introduction to functional functional programming programming using usin Haskell

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

CSI33 Data Structures

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

CSC324 Functional Programming Typing, Exceptions in ML

Second Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

CS-141 Final Exam Review December 16, 2015 Presented by the RIT Computer Science Community

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

n 1 i = n + i = n + f(n 1)

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

Midterm solutions. n f 3 (n) = 3

Quick-Sort. Quick-Sort 1

Chapter 4: Sorting. Spring 2014 Sorting Fun 1

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

STREAMS AND REVIEW 12

Use recursion to write a function that duplicates the following function: (def (f L) (map (lambda (x) (+ (sqr x) x)) L))

Final Exam Solutions

YOUR NAME PLEASE: *** SOLUTIONS ***

Transcription:

LINKED LISTS AND MIDTERM REVIEW 6 COMPUTER SCIENCE 61A October 13, 2016 1 Linked Lists 1.1 Implementation class Link: empty = () def init (self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest def getitem (self, i): if i == 0: return self.first return self.rest[i-1] def len (self): return 1 + len(self.rest) def repr (self): if self.rest is Link.empty: return 'Link({})'.format(self.first) else: return 'Link({}, {})'.format(self.first, repr(self.rest))

DISCUSSION 6: LINKED LISTS AND MIDTERM REVIEW Page 2 1.2 Questions 1. Write a function remove duplicates that takes as input a sorted linked list of integers, lnk, and mutates lnk so that all duplicates are removed. def remove_duplicates(lnk): >>> lnk = Link(1, Link(1, Link(1, Link(1, Link(5))))) >>> unique = remove_duplicates(lnk) >>> len(unique) 2 >>> len(lnk) 2 2. Define reverse, which takes in a linked list and reverses the order of the links. The function may not return a new list; it must mutate the original list. Return a pointer to the head of the reversed list. def reverse(lnk): >>> a = Link(1, Link(2, Link(3))) >>> r = reverse(a) >>> r.first 3 >>> r.rest.first 2

DISCUSSION 6: LINKED LISTS AND MIDTERM REVIEW Page 3 3. Write multiply lnks, which takes in a Python list of Link objects and multiplies them element-wise. It should return a new linked list. If not all of the Link objects are of equal length, return a linked list whose length is that of the shortest linked list given. You may assume the Link objects are shallow linked lists, and that lst of lnks contains at least one linked list. def multiply_lnks(lst_of_lnks): >>> a = Link(2, Link(3, Link(5))) >>> b = Link(6, Link(4, Link(2))) >>> c = Link(4, Link(1, Link(0, Link(2)))) >>> p = multiply_lnks([a, b, c]) >>> p.first 48 >>> p.rest.first 12 >>> p.rest.rest.rest ()

DISCUSSION 6: LINKED LISTS AND MIDTERM REVIEW Page 4 2 Midterm Review 1. Define a function foo that takes in a list lst and returns a new list that keeps only the even-indexed elements of lst and multiplies each of those elements by the corresponding index. def foo(lst): >>> x = [1, 2, 3, 4, 5, 6] >>> foo(x) [0, 6, 20] return [ ] 2. Implement the functions max product, which takes in a list and returns the maximum product that can be formed using nonconsecutive elements of the list. The input list will contain only numbers greater than or equal to 1. def max_product(lst): Return the maximum product that can be formed using lst without using any consecutive numbers >>> max_product([10,3,1,9,2]) # 10 * 9 90 >>> max_product([5,10,5,10,5]) # 5 * 5 * 5 125 >>> max_product([]) 1

DISCUSSION 6: LINKED LISTS AND MIDTERM REVIEW Page 5 3. An expression tree is a tree that contains a function for each non-leaf root, which can be either + or *. All leaves are numbers. Implement eval tree, which evaluates an expression tree to its value. You may want to use the functions sum and prod, which take a list of numbers and compute the sum and product respectively. def eval_tree(tree): Evaluates an expression tree with functions as root >>> eval_tree(tree(1)) 1 >>> expr = tree('*', [tree(2), tree(3)]) >>> eval_tree(expr) 6 >>> eval_tree(tree('+', [expr, tree(4), tree(5)])) 15

DISCUSSION 6: LINKED LISTS AND MIDTERM REVIEW Page 6 4. The quicksort sorting algorithm is an efficient and commonly used algorithm to order the elements of a list. We choose one element of the list to be the pivot element and partition the remaining elements into two lists: one of elements less than the pivot and one of elements greater than the pivot. We recursively sort the two lists, which gives us a sorted list of all the elements less than the pivot and all the elements greater than the pivot, which we can then combine with the pivot for a completely sorted list. First, implement the quicksort list function. Choose the first element of the list as the pivot. You may assume that all elements are distinct. def quicksort_list(lst): >>> quicksort_list([3, 1, 4]) [1, 3, 4] if : pivot = lst[0] less = greater = return 5. We can also use quicksort to sort linked lists! Implement the quicksort link function, without constructing additional Link instances. You can assume that the extend links function is already defined. It takes two linked lists and mutates the first so that the ending node points to the second. extend link returns the head of the first linked list. >>> l1, l2 = Link(1, Link(2)), Link(3, Link(4)) >>> l3 = extend_links(l1, l2) >>> l3 Link(1, Link(2, Link(3, Link(4)))) >>> l1 is l3 True

DISCUSSION 6: LINKED LISTS AND MIDTERM REVIEW Page 7 def quicksort_link(link): >>> s = Link(3, Link(1, Link(4))) >>> quicksort_link(s) Link(1, Link(3, Link(4))) if : return link pivot, = less, greater = while link is not Link.empty: curr, rest = link, link.rest if : else: link = less = greater = return

DISCUSSION 6: LINKED LISTS AND MIDTERM REVIEW Page 8 6. Implement widest level, which takes a Tree instance and returns the elements at the depth with the most elements. def widest_level(t): >>> sum([[1], [2]], []) [1, 2] >>> t = Tree(3, [Tree(1, [Tree(1), Tree(5)]),... Tree(4, [Tree(9, [Tree(2)])])]) >>> widest_level(t) [1, 5, 9] levels = [] x = [t] while : = sum(, []) return max(levels, key= ) 7. Complete redundant map, which takes a tree t and a function f, and applies f to the node (2 d ) times, where d is the depth of the node. The root has a depth of 0. def redundant_map(t, f): >>> double = lambda x: x*2 >>> tree = Tree(1, [Tree(1), Tree(2, [Tree(1, [Tree(1)])])]) >>> print_levels(redundant_map(tree, double)) [2] # 1 * 2 ˆ (1) ; Apply double one time [4, 8] # 1 * 2 ˆ (2), 2 * 2 ˆ (2) ; Apply double two times [16] # 1 * 2 ˆ (2 ˆ 2) ; Apply double four times [256] # 1 * 2 ˆ (2 ˆ 3) ; Apply double eight times t.root = new_f = t.branches = return t