Programming Languages and Techniques ..., Exercises of Algorithms and Programming

Mock Exam and Review Session ... common data structures and algorithms ... Penn Course evaluations also provide useful feedback.

Typology: Exercises

2022/2023

Uploaded on 05/11/2023

koss
koss 🇺🇸

4.8

(16)

242 documents

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming Languages
and Techniques
(CIS120)
Lecture 39
Semester Recap
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Programming Languages and Techniques ... and more Exercises Algorithms and Programming in PDF only on Docsity!

Programming Languages

and Techniques

(CIS120)

Lecture 39

Semester Recap

Announcements

  • HW9: Game – Due tonight at 11:59pm

Final Exam:

  • Tuesday, December 17 th 6:00-8:00 PM
  • 4 Locations:
    • Meyerson B1 Last Names A - L
    • Towne 100 Last Names M - Sh
    • Skirkanich Auditorium Last Names Si – W
    • Moore 216 Last Names X - Z
  • Makeup exam offered Weds. December 18 th
  • required by registrar for exam conflicts
  • time: noon – 2:00PM
  • sign up form on course web page

CIS 120 Recap

From Day 1

  • CIS 120 is a course in program design
  • Practical skills:
    • ability to write larger (~1000 lines) programs
    • increased independence ("working without a recipe")
    • test-driven development, principled debugging
  • Conceptual foundations:
    • common data structures and algorithms
    • several different programming idioms
    • focus on modularity and compositionality
    • derived from first principles throughout
  • It will be fun!

Promise: A challenging

but rewarding course.

CIS 120 Concepts

13 concepts in 38 lectures

Interface vs. Implementation

  • Concept: Type abstraction hides the actual implementation of a data structure, describes a data structure by its interface (what it does vs. how it is represented), supports reasoning with invariants
  • Examples: Set/Map interface (HW3), queues in OCaml and Java, encapsulation and access control
  • Why?
    • Flexibility: Can change the implementation without modifying clients
    • Correctness: Can preserve invariants about the implementation Invariants are a crucial tool for reasoning about data structures: 1. Establish the invariants when you create the structure. 2. Preserve the invariants when you modify the structure. 1 3 0 abstract view 1 0 3 < > concrete representation BST:

Testing

  • Concept: Write tests before coding
    • "test first" methodology
  • Examples:
    • Simple assertions for declarative programs (or subprograms)
    • Longer (and more) tests for stateful programs / subprograms
    • Informal tests for GUIs (can be automated through tools)
  • Why?
    • Tests clarify the specification of the problem
    • Helps you understand the invariants
    • Thinking about tests informs the implementation
    • Tests help with extending and refactoring code later
    • Industry practice; useful for coordinating teams

Persistent data structures

  • Concept: Store data in persistent, immutable structures; implement computation as transformations of those structures
  • Examples: immutable lists and trees in OCaml (HW 1/2/3), images, Strings, Streams in Java (HW 7/9)
  • Why?
    • Simple model of computation, similar to mathematics
    • Simple interface: Don't have to reason about aliasing (no implicit communication between various parts of the program, all interfaces are explicit)
    • Recursion amenable to mathematical analysis (CIS 160/121)
    • Plays well with parallelism Recursion is the natural way of computing a function f(t) when t belongs to an inductive data type:
      1. Determine the value of f for the base case(s).
      2. Compute f for larger cases by combining the results of recursively calling f on smaller cases.
      3. Same idea as mathematical induction (a la CIS 160)

Concept: Tree Structured data

  • Lists (i.e. “unary” trees)
  • Simple binary trees
  • Trees with invariants: e.g. binary search trees
  • Widget trees: screen layout + event routing
  • Swing components
  • Why? Trees are ubiquitous in CS! - file system organization - languages, compilers - domain name hierarchy www.google.com let rec length (l:int list) : int = begin match l with | [] - > 0 | _::tl - > 1 + length(tl) end

Types, Generics, and Subtyping

  • Concept: Static type systems prevent many errors. Every expression has a static type, and OCaml/Java use the types to rule out buggy programs. Generics and subtyping make types more flexible and allow for better code reuse.
  • Why?
    • Easier to fix problems indicated by a type error than to write a test case and then figure out why the test case fails
    • Promotes refactoring: type checking ensures that basic invariants about the program are maintained let rec contains (x:’a) (l:’a list) : bool = begin match l with | [] - > false | h::tl - > x = a || (contains x tl) end

Mutable data

  • Concept: Some data structures are ephemeral : computations mutate them over time
  • Examples: queues, deques (HW4), GUI state (HW5, 9), arrays (HW 6), dictionaries (HW8)
  • Why?
    • Common in OO programming, which simulates the transformations that objects undergo when interacting with their environment
    • Heavily used for event-based programming, where different parts of the application communicate via shared state
    • Default style for Java libraries (collections, etc.)

Lists, Trees, BSTs, Queues, and Arrays

  • Concept: There are implementation trade-offs for abstract types
  • Examples:
    • Binary Search Trees vs. Lists vs. Hashing for sets and maps
    • Linked lists vs. Arrays for sequential data
  • Why?
    • Abstract types have multiple implementations
    • Different implementations have different trade-offs. Need to understand these trade-offs to use them well.
    • For example: BSTs use their invariants to speed up lookup operations compared to linked lists. interface Set {boolean isEmpty(); …}

Abstract Stack Machine

  • Concept: The Abstract Stack Machine is a detailed model of how programs execute in OCaml/Java