Overview/Questions What is an algorithm? How do we go about - - PDF document

overview questions
SMART_READER_LITE
LIVE PREVIEW

Overview/Questions What is an algorithm? How do we go about - - PDF document

CS101 Lecture 21: Writing Simple Programs: Algorithms, The Software Development Process Python Names, Expressions, Input, and Output Aaron Stevens 18 March 2009 1 Overview/Questions What is an algorithm? How do we go about writing a


slide-1
SLIDE 1

1

1

Aaron Stevens

18 March 2009

CS101 Lecture 21: Writing Simple Programs:

Algorithms, The Software Development Process Python Names, Expressions, Input, and Output

2

Overview/Questions

– What is an algorithm? – How do we go about writing a program? – What are the elements that make up a program?

slide-2
SLIDE 2

2

3

Algorithms

Algorithm A set of unambiguous instructions for solving a problem or subproblem in a finite amount of time, and using a finite amount of data. For a given set of inputs, the algorithm will always produce the same outputs.

4

Following an Algorithm

Grandpa Ron’s Chocolate Kahlua Mudslides

Ingredient List:

½ cup vodka ½ cup Kahlua ½ cup Bailey’s Irish Cream ¼ cup Hershey’s chocolate syrup 1 blender full of ice

Process:

Fill blender with ice. Pours other ingredients onto ice. Blend until smooth consistency. Pour into glasses and enjoy.

slide-3
SLIDE 3

3

5

Following an Algorithm

Algorithm for preparing Chocolate Mudslides:

If not all ingredients are present Go to store and buy missing ingredients Fill blender with ice Pour other ingredients into the blender While (not smooth) Turn on blender Wait 10 seconds Turn off blender Test consistency with wooden spoon Pour into glasses and serve

6

Pseudo code

Pseudo code is a way of expressing algorithms that uses a mixture of English phrases and indention to make the steps in the solution explicit.

– There are no grammar rules in pseudocode. – Pseudocode is not case sensitive.

Analogy: pseudo code is like an outline for writing a paper -- it contains all of the main ideas, but is not tied to the structure of grammar, etc.

slide-4
SLIDE 4

4

7

3

Problem Description Suppose you’re going to visit Mexico. You want to go shopping, but the prices are in “pesos”, and you need to know if the prices are good deals before you buy. What information do you need to solve this problem?

Software Development Example

8

3

Design Algorithm Suppose you observe that a currency exchange offers you 100 pesos for 7 American dollars. We can express this relationship as: 100 P = 7 D

Software Development Example

slide-5
SLIDE 5

5

9

3

Refine Algorithm This relationship 100 P = 7 D implies a conversion ratio, either pesos or dollars: pesos = 100 / 7 dollars

  • r

dollars = 7 / 100 pesos

Software Development Example

10

Input, Process, Output

Design Pattern A reusable model for a program. Input, Process, Output pattern – prompt the user for some input – apply algorithm to the input – display output back to the user

slide-6
SLIDE 6

6

11

Input, Process, Output

Completed algorithm: – input the price in pesos – calculate dollars = 7 / 100 * pesos – output dollars

12

The Completed Program

slide-7
SLIDE 7

7

13

Elements of Programs

Comments Lines which are ignored by the interpreter. Programmers use comments to document their programs, and to remove troublesome lines during testing. Any part of a line after the # symbol is a comment Example:

# currencyconversion.py # A program to convert a price in pesos to dollars.

14

Elements of Programs

Keywords Special words reserved by the language. Keywords are used to perform basic computation functions, and are one of the building blocks of programs. Example: print

# The word ‘print’ is a Python keyword print “Hello, world!”

slide-8
SLIDE 8

8

15

Elements of Programs

Identifiers Programmers use names to identify modules (e.g. currencyconversion) and variables (e.g. pesos) within programs. Variables Give names to values within programs

16

Variables

A variable is a spot in memory which can hold a value.

– The value is stored using a name we chose. – We can recall it when needed to use the value, or change it to hold something else. x =

42

slide-9
SLIDE 9

9

17

Elements of Programs

Identifiers in Python follow these rules:

– Must begin with a letter or underscore _ – May contain letters, numbers, underscores – May not contain spaces, punctuation, etc. – May not be Python keywords – Are case sensitive

18

Elements of Programs

Valid Identifier Examples:

x pesos dollars dollarsAndCents dollars_and_cents main

Invalid Identifier Examples:

9x print

slide-10
SLIDE 10

10

19

Elements of Programs

Expressions Any statement which produces a value. Values can be numeric, text, or other types. Variables, too are expressions. Examples (each line is a valid Python statement):

input("Enter a price in pesos: ") 7.0 / 100.0 * pesos dollars

20

Arithmetic Operators

Python built-in numeric operations:

+ addition

  • subtraction

* multiplication / division ** exponentiation % remainder (modulo) The order of operations is the familiar PEMDAS.

slide-11
SLIDE 11

11

21

Output Statements

The simplest form of output from a program is a print statement, which prints text to the console window. print takes a variable number of arguments, separated by commas: print print <expr> print <expr>,<expr>, ... , <expr> print <expr>,<expr>, ... , <expr>,

22

Assignment Statements

The process of giving a value to a variable. General form: <variable> = <expr> Examples (each line is an assignment statement): x = 5 dollars = 7.0 / 100.0 * pesos word = “hello”

slide-12
SLIDE 12

12

23

Assignment Statements

A variable can be assigned many times during a Python program or interactive session (called reassignment). It always holds the most recent value.

Examples: x = 5 print x x = x + 1 print x

24

Input Statements

An input statement uses the special built-in expression called input. input takes the general form: <variable> = input(<prompt>) Examples:

pesos = input(“Enter the price in pesos: ”) name = raw_input(“What is your name? ”)

NOTE: input evaluates the input as an expression, whereas raw_input does not. For text input, you will want to use raw_input instead.

slide-13
SLIDE 13

13

25

Input Statements

When Python encounters an input statement, the interpreter pauses, and waits for the user to input an expression (value), terminated by the <Enter> key. Only after this input does processing continue.

26

The Completed Program (again)

slide-14
SLIDE 14

14

27

Take-Away Points

– Algorithms – Software Development Process – Design Patterns – Elements of Python Programs

  • Identifiers
  • Expressions (and Arithmetic)
  • Variables
  • Output
  • Input

28

Student To Dos

–Readings:

  • Python readings will be taken from a free book

called How to Think Like A Computer Scientist: Learning with Python Available online at: http://openbookproject.net//thinkCSpy/

  • Ch02 (Wednesday), Ch03 (Friday/Monday)

–HW08 is due Wednesday 3/18