What Would Python Print? Tuples, Lists, Dictionaries

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

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

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

TREES AND ORDERS OF GROWTH 7

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Intro. Speed V Growth

April 2 to April 4, 2018

Intro. Scheme Basics. scm> 5 5. scm>

Growth & Mutable Data Summer 2017 Discussion 6: July 11, Time and Growth. Big Theta Notation

Structure and Interpretation of Computer Programs

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

CPSC 217 Midterm (Python 3 version)

SCHEME AND CALCULATOR 5b

Structure and Interpretation of Computer Programs

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

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

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

Structure and Interpretation of Computer Programs

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

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

ITERATORS AND STREAMS 9

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Lists, loops and decisions

TAIL RECURSION, SCOPE, AND PROJECT 4 11

CS61A Notes Week 13: Interpreters

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

CS61A Midterm 2 Review (v1.1)

Structure and Interpretation of Computer Programs

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

Structure and Interpretation of Computer Programs

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

Structure and Interpretation of Computer Programs

TREES AND ORDERS OF GROWTH 8

61A LECTURE 16 TREES, ORDERS OF GROWTH. Steven Tang and Eric Tzeng July 22, 2013

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

CS 314 Principles of Programming Languages

Outline. The Python Memory Model A Linked Implementation of Lists In-Class Work. 1 Chapter 4: Linked Structures and Iterators

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

TREES AND ORDERS OF GROWTH 7

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

ORDERS OF GROWTH, DATA ABSTRACTION 4

Functional Programming. Pure Functional Languages

Scheme Basics > (butfirst '(help!)) ()

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

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

CSc 520. Principles of Programming Languages 7: Scheme List Processing

CS61A Notes Disc 11: Streams Streaming Along

CSE : Python Programming

CSI33 Data Structures

CS 314 Principles of Programming Languages. Lecture 16

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

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

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

User-defined Functions. Conditional Expressions in Scheme

Lecture #21: Search and Sets. Last modified: Wed Mar 9 15:44: CS61A: Lecture #21 1

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

MUTABLE LISTS AND DICTIONARIES 4

CS 314 Principles of Programming Languages

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

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Programming

A Brief Introduction to Scheme (II)

Scheme as implemented by Racket

Discussion 4. Data Abstraction and Sequences

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Functional Programming - 2. Higher Order Functions

Problem 1: Binary Trees: Flatten

CSE 341 Section Handout #6 Cheat Sheet

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

Documentation for LISP in BASIC

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

INTERPRETATION AND SCHEME LISTS 12

University of Massachusetts Lowell

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

CS3: Introduction to Symbolic Programming. Lecture 14: Lists.

61A LECTURE 17 ORDERS OF GROWTH, EXCEPTIONS. Steven Tang and Eric Tzeng July 23, 2013

Recursion & Iteration

CSI33 Data Structures

MetacircularScheme! (a variant of lisp)

Sample Final Exam Questions

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

Lisp. Versions of LISP

Execution order. main()

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

APT Session 2: Python

Typed Racket: Racket with Static Types

Fall 2017 December 4, 2017

Fall 2017 December 4, Scheme. Instructions

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Control flow statements

CSE413 Midterm. Question Max Points Total 100

Transcription:

Review Questions

What Would Python Print? Tuples, Lists, Dictionaries >>> a = (1, 2, 3, 4) >>> a[::-1] >>> a = a[:0:-1] >>> a >>> b = [1, 2, 3, 4] >>> b[3] = a[1:] >>> b >>> b[3][0] = a[:-2]

What Would Python Print? Tuples, Lists, Dictionaries >>> a = (1, 2, 3, 4) >>> a[::-1] (4, 3, 2, 1) >>> a = a[:0:-1] >>> a (4, 3, 2) >>> b = [1, 2, 3, 4] >>> b[3] = a[1:] >>> b [1, 2, 3, (3, 2)] >>> b[3][0] = a[:-2] TypeError: 'tuple' object does not support item assignment

Recursion Write a function deep_map(f, lst) which applies a one-argument function onto every element in the given list. If an element is itself a list, then you should recursively apply the function onto each of its elements. You should NOT return anything instead, mutate the original list (and any nested lists). def deep_map(f, lst): """ >>> lst = [1, 2, [3, 4, [5], 6], 7, [], 8] >>> deep_map(lambda x: x * x, lst) >>> lst [1, 4, [9, 16, [25], 36], 49, [], 64] """

Recursion def deep_map(f, lst): if lst: last = lst.pop() if type(last) is list: deep_map(f, last) else: last = f(last) deep_map(f, lst) lst.append(last)

Nonlocal Write a function that returns a function that returns the last thing it received (the first time it's called, it returns '...') >>> slowpoke = make_delayed_repeater() >>> slowpoke("hi")... >>> slowpoke("hello?") hi >>> slowpoke("stop repeating what I'm saying") hello?

Nonlocal def make_delayed_repeater(): last = '...' def delayed_repeater(phrase): nonlocal last last, to_return = phrase, last return to_return return delayed_repeater

Equality vs. Identity >>> l1, l2 = list(range(5)), list(range(5)) >>> l1 == l2 >>> l1 is l2 >>> l2 = l1 >>> l1 is l2 >>> d1, d2 = {1: 3, 5: 7}, {5: 7, 1: 3} >>> d1 == d2 >>> d1 is d2

Equality vs. Identity >>> l1, l2 = list(range(5)), list(range(5)) >>> l1 == l2 True >>> l1 is l2 False >>> l2 = l1 >>> l1 is l2 True >>> d1, d2 = {1: 3, 5: 7}, {5: 7, 1: 3} >>> d1 == d2 True >>> d1 is d2 False

What Would Python Print? OOP class Foo(object): baz = 0 bar = 'something' def init (self): self.bar = 'anything' self. qux = self.baz Foo.baz += 1 @property def foo(self): return self. qux >>> a = Foo() >>> a.bar >>> a. qux >>> a.foo >>> Foo.baz >>> b = Foo() >>> b.foo

What Would Python Print? OOP class Foo(object): baz = 0 bar = 'something' def init (self): self.bar = 'anything' self. qux = self.baz Foo.baz += 1 @property def foo(self): return self. qux >>> a = Foo() >>> a.bar 'anything' >>> a. qux AttributeError >>> a.foo 0 >>> Foo.baz 1 >>> b = Foo() >>> b.foo 1

Trees Given a binary tree (with left and right), implement a function sum_tree, which adds up all the items (assumed to be numbers) in the tree. def sum_tree(tree): """ Your Code Here """

Trees def sum_tree(tree): if tree is None: return 0 else: left = sum_tree(tree.left) right = sum_tree(tree.right) return tree.entry + left + right

Trees Implement a function same_shape, which takes two binary trees and checks if they have the same shape (not if they have the same items). def same_shape(tree1, tree2): """ Your Code Here """

Trees def same_shape(tree1, tree2): if tree1 is None and tree2 is None: return True elif tree1 is None or tree2 is None: return False left = same_shape(tree1.left, tree2.left) right = same_shape(tree1.right, tree2.right) return left and right

Orders of Growth def foo(n): if n <= 1000: return n for i in range(n): print(i) for i in range(n*n): θ(?) print(i) def bar(n): if n < 3: return n θ(?) return bar(n // 3) def blip(n): for i in range(n//2): θ(?) bar(n) def zeta(n): θ(?) if n <= 1: return 1 return zeta(n-1) + \ zeta(n-2)

Orders of Growth def foo(n): if n <= 1000: return n for i in range(n): print(i) for i in range(n*n): θ(n^2) print(i) def bar(n): if n < 3: return n return bar(n // 3) θ(log n) def blip(n): for i in range(n//2): bar(n) θ(n*log n) def zeta(n): θ(2^n) if n <= 1: return 1 return zeta(n-1) + \ zeta(n-2)

Scheme Write a function append that takes in a list a value and returns a list with that value appended. (define (append lst v) yourcodehere)

Scheme (define (append lst v) (cond ((null? lst) (list v)) (else (cons (car lst) (append (cdr lst) v))))

Scheme Implement the insert function in Scheme, which inserts item at index, if index is within the bounds of the list, or at the end of the list otherwise. (define (insert lst item index) yourcodehere)

Scheme (define (insert lst item index) (cond ((null? lst) (list item)) ((= index 0) (cons item lst)) (else (cons (car lst) (insert (cdr lst) item (- index 1))))))

Extras Write a function find_path that takes in a dictionary, friends mapping every person to the list of their friends, and returns whether it is possible to move from the person start to the person finish by following friend relationships. def find_path(friends, start, finish): """ >>> allfriends = {"Steven" : [ Eric ], "Eric": ["Mark", "Jeffrey", "Brian"], "Albert" : ["Robert", "Andrew", Leonard ]} >>> find_path(allfriends, "Eric", "Robert") True >>> find_path(allfriends, "Steven", "Robert") False """

Extras def find_path(friends, start, finish): def find_path2(visited, start): if start == finish: return True if start in friends: for vertex in friends[start]: if vertex not in visited: visited.append(vertex) if find_path2(visited, vertex): return True return False return find_path2([], start)

Extras Implement a function flatten that takes in a scheme list and removes any nested lists, replacing them with their elements. (Does not have to work for lists nested in nested lists) STK> (define a (list 1 (list 2 3 4) 5 6 (list 7 8))) STK> a (1 (2 3 4) 5 6 (7 8)) STK> (flatten a) (1 2 3 4 5 6 7 8) (define (flatten lst) yourcodehere)

Extras (define (flatten lst) (cond ((null? lst) lst) ((list? (car lst))(append (flatten (car lst)) (flatten (cdr lst)))) (else (cons (car lst) (flatten (cdr lst))))))