1 / 47

Programming for Engineers in Python

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.

skyler
Télécharger la présentation

Programming for Engineers in Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Lecture 12: Highlights • Dynamic programming • Overlapping subproblems • Optimal structure • Memoization • Fibonacci • Evaluating trader’s performance (maximum subarray sum) • Optimizing shipping cargo (Knapsack) 2

  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 3

  4. Solution (Recursive) 4

  5. Solution (Memoization) – The Idea M(N,W) W N 5

  6. Solution (Memoization) - Code 6

  7. Solution (Iterative) – The Idea In Class “Bottom-Up”: start with solving small problems and gradually grow M(N,W) W N 7

  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

  9. Steps in Dynamic Programming • Characterize structure of an optimal solution • Define value of optimal solution recursively • Compute optimal solution values either top-down (memoization) or bottom-up (in a table) • 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 12

  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 13

  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 14

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

  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 16

  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 17

  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 18

  19. Raid • Redundant array of independent disks • http://en.wikipedia.org/wiki/RAID • Add XOR disk • How to fix a flaw disk’s data? 19

  20. Course Summary Time travel to week #1 20

  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! 21

  22. Course Objectives Develop basic computing skills (programming, algorithms, applications) 22

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

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

  25. 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

  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 26

  27. Python as a First Language • Based on Zelle’s paper from 1999http://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

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

  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 32

  33. Python is Simple Python: C++: Java: 33

  34. Python has a Simple Uniform Data Model 34

  35. Python is Safe 35

  36. Python Supports OOP Stack as an example: 36

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

  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/ 38

  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 39

  40. Lack of Compile Time Checking 40

  41. Lack of Compile Time Checking 41

  42. סקר שביעות רצון (הרשמי) 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! 45

  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 46

  47. Good Luck! 47

More Related