The Scala Experience Safe Programming Can be Fun!

advertisement

Programming for

Engineers in

Python

Lecture 13: Shit Happens

Autumn 2011-12

1

Lecture 12: Highlights

• Dynamic programming

• Overlapping subproblems

• Optimal structure

• Memoization

• Fibonacci

• Evaluating trader’s performance (maximum subarray sum)

• Optimizing shipping cargo (Knapsack)

2

Optimizing Shipping Cargo (Knapsack)

• Shipping capacity W = 1000

• Offers from potential shippers n = 100

• Each offer i has a weight w i and an offered reward v

• Maximize the reward given the W tonnage limit i

• A(n,W) - the maximum value that can be attained from considering the first n items weighting at most W tons

3

Solution (Recursive)

4

Solution (Memoization) – The Idea

M(N,W)

N

5

Solution (Memoization) - Code

6

Solution (Iterative) – The Idea

In Class

“Bottom-Up”: start with solving small problems and gradually grow

M(N,W)

N

7

DP: Iterative VS. Memoization

• Same

Big O computational complexity

• If all subproblems must be solved at least once, iterative is better by a constant factor due to no recursive involvement

• If some subproblems may not need to be solved, Memoized algorithm may be more efficient, since it only solve these subproblems which are definitely required

8

Steps in Dynamic Programming

1. Characterize structure of an optimal solution

2. Define value of optimal solution recursively

3. Compute optimal solution values either topdown (memoization) or bottom-up (in a table)

4. Construct an optimal solution from computed values

Why Knapsack?

בנגה תייעב http://en.wikipedia.org/wiki/Knapsack_problem

10

Extensions

• NP completeness http://en.wikipedia.org/wiki/NP-complete

• Pseudo polynomial http://en.wikipedia.org/wiki/Pseudo-polynomial_time

11

Plan

• (

Quick overview on) Reading and writing files

• (

Quick overview on) Exceptions handling

• Error correction / error detection

• (Brief) Course summary

• Python as a first language

• HW, exams and exam tips

12

IO in Python

• This is NOT exam material

• http://www.penzilla.net/tutorials/python/fileio/

• http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files

13

Exceptions Handling in Python

• This is NOT exam material

• http://www.penzilla.net/tutorials/python/exceptions/

• http://docs.python.org/tutorial/errors.html#exceptions

14

Magic

Source: http://csu-il.blogspot.com/

15

Mind Reading Card Trick

• Error correction / error identification

• Error correcting for one card flip

• What if 2 cards flip? 3? 4?

• Applications:

• Messages between computers

• Hard disk drive

• CD

• Barcode

• Spelling corraction

16

Israeli ID Error Detection

• Israeli ID: unique per person, 9 digits

• Right most digit is control digit

• How is the control checked?

• Consider first 8 ID digits

• For every 2 nd digit d:

• d < 5  write 2*d

• d > 5  write 2*d + 1 – 10 (sum of 2*d digits)

• The rest of the digits remain without change

• ID 053326187

17

Example: 053326187

0 5 3 3 2 6 1 8 7

0 + 1 + 3 + 6 + 2 + 3 + 1 + 7 = 23

(23 + control_digit) % 10 = 0

18

Raid

• Redundant array of independent disks

• http://en.wikipedia.org/wiki/RAID

Add XOR disk

How to fix a flaw disk’s data?

19

Course Summary

Time travel to week #1

20

Welcome!

• You are about to take a new programming course in Python

• This is the first run ever of this course

• The idea is to enable you to use programming as a tool to solve “real world” problems

• Hard work is required!

21

Course Objectives

Develop basic computing skills

(programming, algorithms, applications)

22

Practical Sessions

• In a standard classroom

• Purposes:

• Practice topics that were presented in class

• “Preparations” for next week’s class

• Background for homework assignments

• Learn practical tools

• Lectures will be harder to understand, and it is ok…

23

A Personal Note on HW

• It will take you a lot of time and frustration

• It is an engineering difficulty: figuring out what's wrong with a system and how to fix it

• You're engineers: make it work!

• There is no other way to learn how to program

• Believe me…

24

Syllabus

Tentative, not in order, probably too ambitious

• Python programming basics

• Using packages

• Recursion

• Sort & search algorithms, runtime analysis

• Dynamic programming

• Error handling

• Input/output

• Graphical user interface (GUI)

• Simulation

• Optimization

• Data analysis

• Control

• Signal processing

25

My Motivation

• What computational capabilities engineers “need”?

• Be able to write simple scripts

• Be able to use existing tools (modules)

• Problems solving capabilities

• Understand what and how CS people do

• For better collaboration

• General knowledge

26

Python as a First Language

• Based on Zelle’s paper from 1999 http://mcsp.wartburg.edu/zelle/python/python-first.html

• Popular alternatives: C, C++, Java

• The advantages of using a scripting * language as a first language, specifically Python

* - Zelle’s words, never let someone tell you python isn't good because it's a scripting language (we’ll see why soon)

27

Compiler

(C, C++, Java (not exactly))

28

Hello World (in C) ;-)

29

Interpreter

30

Criteria for a First Language

• Requirements from a first programming course:

• Learning how to program

• Problem solving, design

• Implications for Programming Language Choice

• Simple syntax and semantics (simple problems should be solved simply)

• Hands on: high level and flexible languages allow designs to be expressed with minimal overhead

 encourages experimentation

• Supports modern approaches to design and abstraction

(e.g., OOP)

• Widely available

(not for “teaching only”, used in the “real world”)

31

The Case for Scripting Languages

• System programming languages

(C, C++, Pascal, Java)

• Statically typed

• Usually compiled

• Modest abstraction from the underlying machine

• Scripting languages:

• Dynamically typed

• Usually interpreted

• Very high level

• Modern scripting languages are not “toy”/”glue” languages!

• Pros

• Very flexible encourage experimentation

• Allow students to build more sophisticated and interesting projects with less implementation effort

32

Python:

C++:

Java:

Python is Simple

33

Python has a Simple Uniform Data

Model

34

Python is Safe

35

Python Supports OOP

Stack as an example:

36

Python is Fun

• Simplicity makes it fun to learn

• Data structures

(dynamic array, hash table, immutable lists) and class mechanism can be used to quickly build

• No type declarations  less code, more flexible

• Huge libraries of modules

• E.g., for GUI, client-server applications, html viewers, databases, animation

• E.g., PIL, pygame, scipy, numpy, swampy, matplotlib,…

37

Python is Practical

• Scripting languages are gaining popularity. By some accounts, more software is written in scripting than system languages

• Python is free

• Python is a mature language

• Widely industrial used

(e.g., YouTube, BitTorrent, Google,

Yahoo!, IDF, NASA), https://us.pycon.org/2012/sponsors/

38

Some Obstacles

(Real and imagined)

• Lack of compile time checking – next slide

• Scripting languages are too inefficient

• Python is unfamiliar (to lecturers…)

• Our students want languages X

• There aren’t any textbooks

39

Lack of Compile Time Checking

40

Lack of Compile Time Checking

41

( ימשרה ( ןוצר תועיבש רקס

42

Course’s HW

• Total of 11 hw assignments

• You must pass at least 7 assignments to be eligible to pass the course

• HW will count for 20% of course’s grade

• 10 highest assignments will count for the final grade

43

Exam

• One hand written double sided A4 page is allowed. Each student has to prepare his/her own page, photo-copying is not allowed

• Two weeks before the exam we will publish sample questions in the spirit of the exam

• A Q&A session will be held with the TAs a few days before the exam

• Meanwhile, you can “feel” how it looks like by checking previous years exams (in C)

44

Exam Tips

• The exam material includes all that was taught in class, tirgul, and hw assignments. Yes, including today’s class

• You can get 20% of each part by answering “I don’t know”

 knowing that you do not know is much better than don’t knowing

• I like to give questions that are highly similar to examples that were shown during the course

• Read the questions carefully. Understanding what you are asked about is critical!

45

Exam Tips

• Question parts (םיפיעס) are usually dependent.

Most times you should use an earlier part when solving later parts in a given question.

• You may (and should) solve a question part even if you have not solved previous parts

• How to prepare?

• Dealing with exam stress

46

Good Luck!

47

Download