Problem Solving & Review

CSCI 1012

Announcements

  • Unit 1 Exam: 6 Oct
    • 1957 E St. Room 113 3:45-5:00 PM
    • 60 minutes
    • One side of one page (standard Letter) of notes
    • Similar format to quizzes
  • DSS: See me!

In-Class Exercises

  • File names:
    • sum_check.py
    • fibonacci.py
    • two_dice.py
  • Turn in on the submit server before the end of lecture for credit
  • Turn in one .zip with all the .py files
    • Unlimited attempts during lecture
  • We’ll provide solutions – pay attention!

Today

  • Review
  • Problem Solving

Operators

Operator Function (with numbers)
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
// Quotient
% Remainder

Tracing Example - Operators

x = 2
x = 3 + x
y = x // 2
z = x + 1
z = z + y
print(z)

Ints, Floats, and Strings

Does this work?

s = ""
a = 3
s = s + a

Ints, Floats, and Strings

s = "1"
a = 3
s = s + a
print(a)
print(s)

Ints, Floats, and Strings

s = "1"
a = 3
s = s + str(a)
a = a + int(s)
print(a)
print(s)

Functions

def function_name(arg1, arg2):
  # function body
  # code to execute
  return

# this is outside the function

Function - Tracing

def func(a, b):
  a = a + b
  if a > b:
    return a
  return b

x = func(-1, -2)
y = func(2, 2)
print(x + y)

Exercise 1

sum_check.py
def sum_check(A, B, C):
  # your code here!
  • Arguments A, B, and C will be ints
  • Return bool True if any two args sum to the third
    • Otherwise, return False

Examples:

  • sum_check(1, 3, 4) returns True
  • sum_check(2, 3, 4) returns False
  • sum_check(0, 0, 0) returns True

Functions Calling Functions

def funcA(argA):
  return argA + 1

def funcB(argB):
  C = funcA(argB)
  return C * 2

z = funcB(3)
print(z)

Review: Comparison Operators

Operator Function Syntax
== Equal? 3 == 5
!= Not equal? 3 != 5
< Less than? 3 < 5
> Greater than? 3 > 5
<= Less than or equal to 3 <= 5
>= Greater than or equal to? 3 >= 5

Conditionals

  • if
    • Starts the conditional block, necessary
    • Only one allowed
  • elif (“else if”)
    • Zero or more allowed
    • Checked in sequence
  • else
    • Always

Iteration

  • while loop
    • superficially similar to if
    • watch out for infinite loops!
  • for loop
    • range function
  • 1 argument: “stop”
  • 2 arguments: “start,” “stop”
  • 3 arguments: “start,” “stop,” “skip”

Exercise 2

fibonacci.py
def fibonacci(n):
  # your code here!
  • Return a string consisting of the fibonacci sequence values less than argument n
    • Separate numbers with spaces
    • Argument will always be at least 2

Examples:

  • fibonacci(12) returns string '1 1 2 3 5 8'
  • fibonacci(30) returns string '1 1 2 3 5 8 13 21'

Exercise 3 🎲 🎲

two_dice.py
def two_dice(m):
  # your code here!

Return how many ways there are to get the sum of two 6-sided dice to add to the argument.

Examples:

  • two_dice(1) returns 0
  • two_dice(2) returns 1 (1+1)
  • two_dice(3) returns 2 (1+2, 2+1)
  • two_dice(4) returns 3 (1+3, 3+1, 2+2)

Next Week

Exam on 6 October. (in lecture)

Homework 6 due Sunday, 5 Oct 11:55 PM

  • Late days allowed…
  • Material might be on exam