Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Logic & program control part 2: Simple selection structures.
Introduction to Computing Science and Programming I
Introduction to C Programming
Conditional Statements Introduction to Computing Science and Programming I.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Structured programming
Chapter 4: Conditionals and Recursion
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Programming for Linguists An Introduction to Python 24/11/2011.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Chapter 2 - Algorithms and Design
Java Programming: From the Ground Up
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Python Conditionals chapter 5
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter Making Decisions 4. Relational Operators 4.1.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Decision Making CMSC 201 Chang (rev ).
Controlling Program Flow with Decision Structures.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
 Type Called bool  Bool has only two possible values: True and False.
Python Basics.
Control Flow (Python) Dr. José M. Reyes Álamo.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Module 3.
More important details More fun Part 3
Selection and Python Syntax
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Line Continuation, Output Formatting, and Decision Structures
Selection CIS 40 – Introduction to Programming in Python
Arithmetic operations, decisions and looping
Conditions and Ifs BIS1523 – Lecture 8.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
REPETITION Why Repetition?
Presentation transcript:

Intro to Robots Conditionals and Recursion

Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we throw away the remainder. To capture the remainder (before we lose it) we use the modulus (%) operator. then 7 / 3 = 2 7 % 3 = 1 n % d = r n / d = q n = q * d + r

Intro to Robots Use of Modulus Some numbers, like credit card or identification numbers, are encoded so that the last digit can be “calculated” from the previous digits. Suppose a 16-digit number has its first 15 digits as and that the 16 th digit is calculated by adding the digits as eight 2-digit numbers Repeat until there are only two digits the larger of the last two digits is the 16 th digit _ = =

Intro to Robots Exercise Which if these two 16-digit numbers is “valid”? Or In a short while we’ll see an algorithm for verifying that a 16 digit number is valid using this method of calculating the last digit

Intro to Robots Boolean Expressions: A boolean expression is one that is either true or false. In Python, true and false are represented as 1 and 0. The operator == compares two numbers and decides if they are equal Other such operators for numbers are >>> 5 == 6 0 >>> 5 == 5 1 x != y -- x is not equal to y x > y -- x is greater than y x = y -- x is greater than or equal to y x <= y -- x is less than or equal to y x == y -- x is equal to y

Intro to Robots BE CAREFUL!! The operator = is the “assignment operator” not the “comparison operator”. x = y -- the variable x takes on the value of the variable y x == y -- returns 1 of x and y have the same value; 0 otherwise

Intro to Robots Exercise What does the following do? We can use a function definition to capture this logic without any mistakes print ‘even’*(a%2==0) + ‘odd’*(a%2==1)‏ def parity(a): return print ‘even’*(a%2==0) + ‘odd’*(a%2==1)

Intro to Robots Boolean or Logical Operators: There are three logical operators – and, or and not. Their meaning (semantics) are the same as in English. x > 2 and x 2 and x = same as above

Intro to Robots Conditional Execution: if x > 0: print ‘x is positive’ condition; if true, everything indented is executed HEADER: STATEMENT 1 STATEMENT 2... STATEMENT n HEADER: starts a new line, ends in a : BODY: sometimes called a program block. Everything indented after the header.

Intro to Robots Alternative Execution: This can be encapsulated in a function to hide the code details: Or using the function we wrote earlier if x %2 == 0: print x, ‘is even’ else: print x, ‘is odd’ executed if condition is true executed if condition is false def printParity(x): if x %2 == 0: print x, ‘is even’ else: print x, ‘is odd’ print x, “is” + parity(x)‏

Intro to Robots Chained Conditionals: What happens if you need to choose from among three or more things (instead of just two): if x > 0: print x, ‘is positive’ elif (x == 0): print x, ‘is zero’ else: print x, ‘is negative’

Intro to Robots Exercise: Write a function called compare(x,y) that does the following: –Returns 1 if x > y –Returns 0 if x == y –Returns -1 if x < y def compare(x,y): if x > y: return 1 elif x == 0: return 0 else: return -1

Intro to Robots Exercise: Verify a 16-digit number.

Intro to Robots Nested Conditions: Decision Tree

Intro to Robots Nested Conditions if x == ‘A’ and y == 0: Do A0 elif x == ‘A’ and y == 1: Do A1 elif x == ‘B’ and y == 0: Do B0 else: Do B1 if x == ‘A’: if y == 0: Do A0 else: Do A1 else: if y == 0: Do B0 else: Do B1 if x == ‘A’ and y == 0: Do A0 elif x == ‘A’ and y == 1: Do A1 elif x == ‘B’ and y == 0: Do B0 elif x == ‘B’ and y == 1: Do B1 if x == ‘A’: if y == 0: Do A0 elif y == 1: Do A1 elif x == ‘B’: if y == 0: Do B0 elif y == 1: Do B1

Intro to Robots Return Statement: The classic reason for a function is to calculate a value (y) for a given value (x). In Python, the value returned by a function is expressed in a return statement. y = f(x)‏ def min(a,b): # returns the smallest of two numbers if a < b: return a else: return b

Intro to Robots More than one return type: The built-in math function sqrt() fails on a negative number This version of square root “gently” avoids an error message if you try to calculate the square root of a negative number

Intro to Robots Recursion: def countDown(n): if n == 0: print “Blast Off!” else: print n countDown(n-1)‏

Intro to Robots Definition: A recursive definition is one that has two properties: –A trivial (base) case with a trivial definition –The definition describes something of a certain size (n) in terms of the same thing but of a smaller size (n-1). Problems can have non-recursive and recursive definitions/descriptions. Not all problems can be described recursively

Intro to Robots Towers of Hanoi ABC Move one disk from A to B Move one disk from A to C Move one disk from B to C Move one disk from A to B Move one disk from C to A Move one disk from C to B Move one disk from A to B

Intro to Robots Towers of Hanoi Problem: Problem: Move n disks from ‘A’ to ‘B’ Using ‘C’ Solution: Move the disks using the following rules: –You may only move one disk at a time –You may never put a big disk on top of a small disk Problem: Move n disks from ‘A’ to ‘B’ Using ‘C’ Solution: 1.Move n-1 disks from ‘A’ to ‘C’ using ‘B’ 2.Move 1 disk from ‘A’ to ‘B’ 3.Move n-1 disks from ‘C’ to ‘B’ using ‘A’ Non-recursive Definition Recursive Definition How! What! same problem; smaller size problem size base case

Intro to Robots Recursion and the Stack: Remember that each time a function is called space is reserved for its variables and parameters on “the Stack”. We saw this by introducing an error in the base case of the definition of the Towers of Hanoi program (divide by 0) and when the error occurred, the Python environment prints out the stack.

Intro to Robots n = (3-1) = 2 n = (2-1) = 1 n = (1-1) = 0 Error: division by 0 _toplevel_ hanoi n = 3 hanoi n = 2 hanoi n = 1 hanoi n = 0 also called main

Intro to Robots Infinite Recursion: What happens if there is no base case. def countDown(n): countDown(n-1)‏ stack gets to be too big _toplevel_ countDown n = 3 countDown n = 2 countDown n = 1 countDown n = 0 countDown n = -1 countDown n = -2 countDown n = -3 countDown n =

Intro to Robots Keyboard Input: Python has several built-in keyboard read functions. raw_input() reads keyboard input into a string until you hit return. raw_input()‏ you can supply a prompt

Intro to Robots Keyboard Input: You can use the input() function if the input is integer type. Just make sure you enter an integer and not something else.

Intro to Robots Keyboard Input: The myro system also comes with its own keyboard input environment; a separate dialog box that is invoked by calling ask(“A Prompt”)‏

Intro to Robots Exercise: