programming for engineers in python

47
1 Programming for Engineers in Python Autumn 2011- 12 Lecture 13: Shit Happens

Upload: skyler

Post on 21-Jan-2016

73 views

Category:

Documents


3 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming for Engineers in Python

1

Programming for Engineers in

Python

Autumn 2011-12

Lecture 13: Shit Happens

Page 2: Programming for Engineers in Python

2

Lecture 12: Highlights

• Dynamic programming• Overlapping subproblems

• Optimal structure

• Memoization

• Fibonacci

• Evaluating trader’s performance (maximum subarray sum)

• Optimizing shipping cargo (Knapsack)

Page 3: Programming for Engineers in Python

3

Optimizing Shipping Cargo (Knapsack)

• Shipping capacity W = 1000• Offers from potential shippers n = 100

• Each offer i has a weight wi and an offered reward vi

• Maximize the reward given the W tonnage limit• A(n,W) - the maximum value that can be attained from

considering the first n items weighting at most W tons

Page 4: Programming for Engineers in Python

4

Solution (Recursive)

Page 5: Programming for Engineers in Python

5

Solution (Memoization) – The Idea

N

W

M(N,W)

Page 6: Programming for Engineers in Python

6

Solution (Memoization) - Code

Page 7: Programming for Engineers in Python

7

Solution (Iterative) – The IdeaIn Class

N

W

M(N,W)

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

Page 8: Programming for Engineers in Python

8

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

Page 9: Programming for Engineers in Python

Steps in Dynamic Programming

1. Characterize structure of an optimal solution

2. Define value of optimal solution recursively

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

4. Construct an optimal solution from computed values

Page 10: Programming for Engineers in Python

10

Why Knapsack?בעיית הגנב

http://en.wikipedia.org/wiki/Knapsack_problem

Page 11: Programming for Engineers in Python

11

Extensions

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

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

Page 12: Programming for Engineers in Python

12

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

Page 13: Programming for Engineers in Python

13

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

Page 14: Programming for Engineers in Python

14

Exceptions Handling in Python

• This is NOT exam material• http://www.penzilla.net/tutorials/python/exceptions/

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

Page 15: Programming for Engineers in Python

15

Magic

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

Page 16: Programming for Engineers in Python

16

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

Page 17: Programming for Engineers in Python

17

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 2nd 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

Page 18: Programming for Engineers in Python

18

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

Page 19: Programming for Engineers in Python

19

Raid

• Redundant array of independent disks

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

• Add XOR disk

• How to fix a flaw disk’s data?

Page 20: Programming for Engineers in Python

20

Course SummaryTime travel to week #1

Page 21: Programming for Engineers in Python

21

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!

Page 22: Programming for Engineers in Python

22

Course Objectives

Develop basic computing skills (programming, algorithms, applications)

Page 23: Programming for Engineers in Python

23

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…

Page 24: Programming for Engineers in Python

24

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…

Page 25: Programming for Engineers in Python

25

SyllabusTentative, 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

Page 26: Programming for Engineers in Python

26

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

Page 27: Programming for Engineers in Python

27

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)

Page 28: Programming for Engineers in Python

28

Compiler

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

Page 29: Programming for Engineers in Python

29

Hello World (in C) ;-)

Page 30: Programming for Engineers in Python

30

Interpreter

Page 31: Programming for Engineers in Python

31

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”)

Page 32: Programming for Engineers in Python

32

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

Page 33: Programming for Engineers in Python

33

Python is Simple

Python:

C++:

Java:

Page 34: Programming for Engineers in Python

34

Python has a Simple Uniform Data Model

Page 35: Programming for Engineers in Python

35

Python is Safe

Page 36: Programming for Engineers in Python

36

Python Supports OOPStack as an example:

Page 37: Programming for Engineers in Python

37

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,…

Page 38: Programming for Engineers in Python

38

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/

Page 39: Programming for Engineers in Python

39

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

Page 40: Programming for Engineers in Python

40

Lack of Compile Time Checking

Page 41: Programming for Engineers in Python

41

Lack of Compile Time Checking

Page 42: Programming for Engineers in Python

42

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

Page 43: Programming for Engineers in Python

43

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

Page 44: Programming for Engineers in Python

44

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)

Page 45: Programming for Engineers in Python

45

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!

Page 46: Programming for Engineers in Python

46

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

Page 47: Programming for Engineers in Python

47

Good Luck!