1 of 16

CS 149

Professor: Alvin Chao

What is 20/3 + 5*8 - 4?

"Answer to the Ultimate Question of Life, the Universe, and Everything"

Hitchhikers Guide to the Galaxy

2 of 16

Circle math

  • Counting on a Line:

    • x+a moves you a units to the right of x
    • x−b moves you b units to the left of x
  • Counting on a Circle
    • (x+a) moves you a units clockwise of x
    • (x−b) moves you b units counterclockwise of x

3 of 16

Clock Arithmetic

  • Background:
    • A 24-hour clock (00 - 23)
  • It is now 17. What time will it be in 12 hours?
  • The Naive Approach:
    • 17+12 is 29. So, we have advanced a day. That means the time is actually 29-24 or 5.
  • A Shortcoming of this Approach:
    • We might advance more than one day! (For example, advancing 93 hours from now.)

4 of 16

Clock Arithmetic(contd.)

  • A Better Way
    • Use arithmetic on a circle (that goes from 0 to 23)
  • Using int variables and %
    • future = (current + change) % 24;

5 of 16

Other Time/Date examples

  • Minutes
    • Go from 0 to 59
    • Be mindful if you also need to count hours(which can be calculated using integer division).
  • Days of the Week ( 0 to 6)
  • Months of the Year ( 0 to 11 not 1 to 12)
  • Weights: Ounces (0 – 15) then use pounds
    • Pounds ( 0 – 1999) then use tons
  • Distances:
    • Inches (0 – 11) then use feet
    • Feet( 0 – 2 ) then use yards
    • Yards ( 0 – 5279) then use miles

6 of 16

Another time example

“Twenty-nine days” means the same thing as “Four weeks and one day”. If days is a Python integer variable containing some number of days, develop expressions for:

  • The number of weeks in days (4 in the example above). [floor division]
  • The number of days that are left over. (1 in the example above). [remainder/modulus]

7 of 16

Even/Odd Numbers

  • Definition
    • A number is even if it can be divided by 2 with no remainder
  • Observe
    • If we think of all numbers as being either even or odd we can conceptualize this as a circle with two items in the cycle.
    • Using %:
    • Does x % 2 equal 0?

8 of 16

Cycling through a Set

  • Examples
    • Turn-taking by different # of players
    • Cycling through a set of colors
    • Repeating a set of instructions
  • Observation
    • An element in a set can be identified by its number
    • If we start at 0 and let n be the cardinality in the set then we can use index =(index + 1) % n

9 of 16

Divisibility: Census Example

  • Background: The U.S. Census Bureau conducts a census every 10 years(in years ending with a zero)
  • Problem: Find the previous census year for a given year
  • Using // :
    • censusYear = (year // 10) * 10;

10 of 16

Digit Manipulation

  • Note an int value is ‘atomic’ meaning it has no sub-parts.
  • Many times we want to find the ones digit or tens digit of a number.
  • Get the left-most digits
    • Use integer or floor division (i.e. // )
    • Use a right-side operand of 10N-n
  • Get the right-most digits
    • Use remainder after division (i.e. %)
    • Use a rightside operand of 10n

11 of 16

In practice

  • Retrieving the Left-Most n Digits:
    • The left-most digit of 7198 is 7198 // 1000
    • The left-most two digits of 531768 are 531768 // 10000
  • Retrieving the Right-Most n Digits:
    • The right-most digit of 7198 is 7198 % 10
    • The right-most two digits of 531768 are 531768 % 100

12 of 16

Operations

  • An operator is a symbol indicating that an operation is to be performed on one or more operands
  • An operand can be a variable, literal, or expression
  • Number of Operands:
    • unary operator has one operand
    • binary operator has two operands
    • ternary operator has three operands

13 of 16

Binary Operators

  • Numeric Operations and Operators:
    • Addition (+)
    • Subtraction (-)
    • Multiplication (*)
    • Division (/)
    • Floor or Integer Division (//)
    • Remainder or Modulo (%)
    • Exponent(**)

14 of 16

Unary Operators

  • Operations and Operators:
    • "Positive" (+)
    • Negative (-)

  • Operand:
    • A numeric type

15 of 16

Math module

Must do import math at top of program to use these:

  • math.cos(a) Cosine of angle a(a is in radians)
  • math.radians(a) converts angle a to radians from degrees
  • math.degrees(a) converts angle a from radians to degrees
  • math.sqrt(x) - Square root of x
  • math.fabs(v) - Absolute value of v
  • math.exp(x) - returns e to the power of x.
  • math.pow(x,y) - returns x raised to the power of y
  • math.pi - constant for pi.
  • math.e - constant for e

16 of 16

  • Acknowledgements

Parts of this activity are based on materials developed by David Bernstein

</end>