Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators -...
-
date post
22-Dec-2015 -
Category
Documents
-
view
224 -
download
0
Transcript of Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators -...
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 = 27 % 3 = 1
n % d = rn / 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 16th 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 16th digit
4591-3295-6843-256_
04 + 59 + 13 + 29 + 56 + 84 + 32 + 56 = 333
03 + 33 = 36
4591-3295-6843-2566
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.
5291-4528-7426-6539
5291-4582-7426-6539
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 == 60>>> 5 == 51
x != y -- x is not equal to yx > y -- x is greater than yx < y -- x is less than yx >= y -- x is greater than or equal to yx <= y -- x is less than or equal to yx == 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 yx == 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 < 10 -- true (1) if x is greater than 2 and less than 10not (x > 2 and x < 10) -- false(0) if x is greater than 2 and less than 10 -- true (1) is x is less than or equal to 2 or x is greater than or equal to 10x <= 2 or x >= 10 -- same as above
Intro to Robots
Conditional Execution:
if x > 0: print ‘x is positive’
condition; if true, everything indentedis 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
Nested Conditions:
Decision Tree
x == ‘A’
x == ‘B’
y == 0
y == 1
y == 0
y == 1
Do A0
Do A1
Do B0
Do B1
Intro to Robots
Nested Conditions
if x == ‘A’ and y == 0: Do A0elif x == ‘A’ and y == 1: Do A1elif x == ‘B’ and y == 0: Do B0else: Do B1
if x == ‘A’: if y == 0: Do A0 else: Do A1else: if y == 0: Do B0 else: Do B1
if x == ‘A’ and y == 0: Do A0elif x == ‘A’ and y == 1: Do A1elif x == ‘B’ and y == 0: Do B0elif x == ‘B’ and y == 1: Do B1
if x == ‘A’: if y == 0: Do A0 elif y == 1: Do A1elif 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
A B C
Move one disk from A to BMove one disk from A to CMove one disk from B to CMove one disk from A to BMove one disk from C to AMove one disk from C to BMove 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 = -4
.
.
.
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”)