1 The Practice of Computing Using PYTHON William PunchRichard Enbody Chapter 3 Algorithms & Program Development Copyright © 2011 Pearson Education, Inc.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Sampling Distribution Models.
Advertisements

Copyright © 2010, 2007, 2004 Pearson Education, Inc. Chapter 18 Sampling Distribution Models.
Algorithms Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the five essential properties of an algorithm.
Chapter 10 Algorithmic Thinking. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives List the five essential.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Chapter 2: Algorithm Discovery and Design
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Chapter 2: Input, Processing, and Output
Modules, Hierarchy Charts, and Documentation
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
Technology in Action Chapter 10 Behind the Scenes: Software Programming Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall.
Introduction to Python
WJEC Applied ICT Spreadsheet Skills 1.Introduction to Financial Modelling Definition A model is a program which has been developed to copy the way.
Chapter An Introduction to Problem Solving 1 1 Copyright © 2013, 2010, and 2007, Pearson Education, Inc.
Invitation to Computer Science, Java Version, Second Edition.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
Testing Michael Ernst CSE 140 University of Washington.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
SE: CHAPTER 7 Writing The Program
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
We came up with the general rule for expanding binomials, in particular squaring the sum and difference of two terms:- (a + b)²= a²+ 2ab + b² and (a –
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Algorithms CS139 – Aug 30, Problem Solving Your roommate, who is taking CS139, is in a panic. He is worried that he might lose his financial aid.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Sampling Distribution Models.
CS221 Algorithm Basics. What is an algorithm? An algorithm is a list of instructions that transform input information into a desired output. Each instruction.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Copyright © 2006 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.
Algorithms and DeMorgan’s Laws Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Newton’s Method, Root Finding with MATLAB and Excel
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Chapter 6: Analyzing and Interpreting Quantitative Data
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
1 Running Experiments for Your Term Projects Dana S. Nau CMSC 722, AI Planning University of Maryland Lecture slides for Automated Planning: Theory and.
CS 206 Introduction to Computer Science II 01 / 30 / 2009 Instructor: Michael Eckmann.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
The Scientific Method: How to solve just about anything.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Copyright © 2010, 2007, 2004 Pearson Education, Inc. Chapter 18 Sampling Distribution Models.
Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.
Getting Started With Python Brendan Routledge
Algorithms and Flowcharts
CSC 108H: Introduction to Computer Programming
CS1371 Introduction to Computing for Engineers
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Programming We have seen various examples of programming languages
Algorithms and DeMorgan’s Laws
Flowcharts and Pseudo Code
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Lecture 6 - Recursion.
Presentation transcript:

1 The Practice of Computing Using PYTHON William PunchRichard Enbody Chapter 3 Algorithms & Program Development Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

2 What is an Algorithm? process or a set of rules to be followed in calculations or other problem-solving operations more informally: a recipe for solving a problem The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

3 Example: Square Root Algorithm 1. Guess the square root of the number 2. Divide the working number by the guess 3. Average the quotient (from 2) and the guess 4. Make the new guess the average from step 3 5. If the new guess is “sufficiently different” from the old guess, go back to step 2, else halt. The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

4 Algorithm vs. Program an algorithm is a description of how to solve a problem a program is an implementation of an algorithm in a particular language to run on a computer (usually a particular kind of computer) difference between what we want to do and what we actually did The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

5 What’s the Difference, Really? we can analyze the algorithm independent of its implementation. This is the science in Computer Science we can examine how easily, or with what difficulty, a language allows us to realize an algorithm we can examine how different computers impact the realization of an algorithm The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

6 Aspects of an Algorithm Detailed: Provide enough detail to be implementable. Can be tricky to define completely, relies on “common sense” Effective: the algorithm should eventually halt, and halt in a “reasonable” amount of time. “reasonable” might change under different circumstances (faster computer, more computers, etc.) The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

7 Aspects of an Algorithm (2) Specify Behavior: the algorithm should be specific about the information that goes in (quantity, type, etc.) and the information that comes out. General Purpose: algorithms should be idealized and therefore general purpose. A sorting algorithm should be able to sort anything (numbers, letters, patient records, etc.) The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

8 A Lot to Do! That is a lot to do for the burgeoning programmer. Get better as we go along, but good to know what the standards are! The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

9 Aspects of a Program: Readability We will emphasize, over and over, that a program is an essay on problem solving intended to be read by other people, even if “other people” is you in the future! Write a program so that you can read it, because it is likely that sometime in the future you will have to read it! The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

10 Readability(2): Naming The easiest thing to do that affects readability is good naming use names for the items you create that reflect their purpose to help keep straight the types used, include that as part of the name. Python does not care about the type stored, but you do! The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

11 What Does this Do? a = input("give a number: ") b,c = 1,0 while b <= a : c = c + b b = b + 1 print (a,b,c) print ("Result: ", float(c)/b - 1) CL03-1.py The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

12 # average of a sum of integers in a given range limit_str = input(“range is 1 to input:”) limit_int = int(limit_str) count_int = 1 sum_int = 0 while count_int <= limit_int: sum_int = sum_int + count_int count_int = count_int + 1 average = float(sum_int)/(count_int – 1) CL03-2.py The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

13 Readability(3): Comments info at the top, the goal of the code purpose of variables (if not obvious by the name) purpose of other functions being used anything “tricky”. If it took you time to write, it probably is hard to read and needs a comment The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

14 Readability(4): Indenting indenting is a visual cue to say what code is “part of” other code. This is not always required as it is in Python, but Python forces you to indent. This aids readability greatly. The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

15 Aspects of Programming (2) Robust: As much as possible, the program should account for inputs that are not what is expected. More on this with error handling in Chapter 14 Correct: Our programs should produce correct results. Much harder to ensure than it looks! The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

16 More on Problem Solving

17 The Problem is “Problem- Solving” Remember, two parts to our goal: Understand the problems to be solved Encode the solution in a programming language, e.g. Python The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

18 Mix of Both The goal in each class is to do a little of both: problem solving and Python Terribly important that we impress on you to try and understand how to solve the problem first before you try and code it. The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

19 Steps to Problem Solving From “PProblem SSSolving”, DeFranco & Vinsonhaler Be Proactive See it Simplify it Stir it up Pause and Reflect The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

20 Steps to Problem Solving Engage/Commit Visualize/See Try it/Experiment Simplify Analyze/Think Relax The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

21 Engage You need to commit yourself to addressing the problem. Don’t give up easily Try different approaches Set the “mood” Just putting in time does not mean you put in a real effort!!! The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

22 Visualize/See the Problem Find a way that works for you, some way to make the problem tangible. draw pictures layout tables literally “see” the problem somehow Everyone has a different way, find yours! The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

23 Try It/Experiment For some reason, people are afraid to just “try” some solution. Perhaps they fear failure, but experiments, done just for you, are the best way to figure out problems. Be willing to try, and fail, to solve a problem. Get started, don’t wait for enlightenment! The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

24 Simplify Simplifying the problem so you can get a handle on it is one of the most powerful problem solving tools. Given a hard problem, make is simpler (smaller, clearer, easier), figure that out, then ramp up to the harder problem. The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

25 Think it Over/Analyze If your solution isn’t working: stop evaluate how you are doing analyze and keep going, or start over. People can be amazingly “stiff”, banging their heads against the same wall over and over again. Loosen up, find another way! The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

26 One More Thing: Relax Take your time. Not getting an answer right away is not the end of the world. Put it away and come back to it. You’d be surprised how easy it is to solve if you let it go for awhile. That’s why starting early is a luxury you should afford yourself. The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

27 Babylonian Square Root Example The Practice of Computing Using Python, Punch, Enbody, ©2011 Pearson Addison-Wesley. All rights reserved

28 1.Guess the square root of the number. 2. Divide the number by the guess. 3.Average the quotient (from step 2) and the guess 4. Make the new guess the average from step If the new guess differs from the old guess by more than the specified tolerance, go back to step 2; otherwise stop Algorithm

29 User provides 3 inputs: an integer number to find the square root of, an integer initial guess, a floating-point tolerance. Apply the algorithm Output the original conditions, its square root and the number of iterations. CL03-5.py Program Algorithm Skeleton