Programming for Engineers in Python

Post on 21-Jan-2016

71 views 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 of Programming for Engineers in Python

1

Programming for Engineers in

Python

Autumn 2011-12

Lecture 13: Shit Happens

2

Lecture 12: Highlights

• Dynamic programming• Overlapping subproblems

• Optimal structure

• Memoization

• Fibonacci

• Evaluating trader’s performance (maximum subarray sum)

• Optimizing shipping cargo (Knapsack)

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

4

Solution (Recursive)

5

Solution (Memoization) – The Idea

N

W

M(N,W)

6

Solution (Memoization) - Code

7

Solution (Iterative) – The IdeaIn Class

N

W

M(N,W)

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

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

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

10

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

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

11

Extensions

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

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

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

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

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

15

Magic

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

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

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

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

19

Raid

• Redundant array of independent disks

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

• Add XOR disk

• How to fix a flaw disk’s data?

20

Course SummaryTime travel to week #1

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!

22

Course Objectives

Develop basic computing skills (programming, algorithms, applications)

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…

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…

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

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

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)

28

Compiler

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

29

Hello World (in C) ;-)

30

Interpreter

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

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

33

Python is Simple

Python:

C++:

Java:

34

Python has a Simple Uniform Data Model

35

Python is Safe

36

Python Supports OOPStack as an example:

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

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/

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

40

Lack of Compile Time Checking

41

Lack of Compile Time Checking

42

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

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

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)

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!

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

47

Good Luck!