Cookie Calculator Repetition Hans-Joachim Bckenhauer and Dennis - - PowerPoint PPT Presentation

cookie calculator
SMART_READER_LITE
LIVE PREVIEW

Cookie Calculator Repetition Hans-Joachim Bckenhauer and Dennis - - PowerPoint PPT Presentation

Cookie Calculator Repetition Hans-Joachim Bckenhauer and Dennis Komm Digital Medicine I: Introduction to Programming Local variables, scopes, and complexity Autumn 2019 October 31, 2019 Example Cookie Calculator Cookie Calculator


slide-1
SLIDE 1

Hans-Joachim Böckenhauer and Dennis Komm

Digital Medicine I: Introduction to Programming

Local variables, scopes, and complexity

Autumn 2019 – October 31, 2019

Cookie Calculator

Repetition Example – Cookie Calculator

children = int(input("Number of children:")) cookies = int(input("Number of cookies:")) print("Every child receives", cookies // children, "cookies") print("Dad receives", cookies % children, "cookies")

We want to make sure that children is positive and that each child gets at least one cookie

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 1 / 41

Cookie Calculator – Check Input

From this . . .

children = int(input("Number of children:"))

. . . we go to this

while True: children = int(input("Number of children:")) if children >= 1: break else: print("Value needs to be at least 1")

Analogously, we have to check that cookies >= children

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 2 / 41

slide-2
SLIDE 2

Cookie Calculator – Getting Complicated

while True: children = int(input("Number of children:")) if children >= 1: break else: print("Value needs to be at least 1") while True: cookies = int(input("Number of cookies:")) if cookies >= children: break else: print("Value needs to be at least", children) print("Every child receives", cookies // cookies, "cookies") print("Dad receives", cookies % children, "cookies")

Read and check number of children Read and check number of cookies

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 3 / 41

Cookie Calculator – Takeaway

The two code fragments are nearly identical The following aspects are different:

The prompt, i.e., "children:" vs. "cookies:" The minimum, i.e., 1 vs. children

We can outsource the code fragment into a function and thus feature reuse We have to parameterize the different aspects

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 4 / 41

Exercise – Cookie Calculator

Write a function that gets two parameters prompt and minimum asks the user for an integer input returns the input using return if it is at least

minimum

  • therwise asks for a new input

Use the function in the cookie calculator

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 5 / 41

Exercise – Cookie Calculator

def checkinput(prompt, minimum): while True: x = int(input(prompt)) if x >= minimum: return x else: print("Value needs to be at least", minimum) children = checkinput("Number of children:", 1) cookies = checkinput("Number of cookies:", children) print("Every child receives", cookies // cookies, "cookies") print("Dad receives", cookies % children, "cookies")

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 6 / 41

slide-3
SLIDE 3

Functions

Scope and Lifetime of Variables Local Variables

Parameters of a function are only valid within this function

def f(x): return x + 5 print(x)

NameError: name ’x’ is not defined

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 7 / 41

Local Variables

The same is true for variables that are defined in a function

def f(x): y = 5 return x + y print(y)

NameError: name ’y’ is not defined

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 8 / 41

Local Variables

Such variables (parameters) are called local variables Variables defined outside of a function are called global variables Field of validity is called scope of the variable Time in which it is defined is called lifetime of the variable

def f(x): if x < 0: return -100 y = x + 1 if y < 10: y += 10 else: y -= 20 return y scope of x def f(x): if x < 0: return -100 y = x + 1 if y < 10: y += 10 else: y -= 20 return y scope of y

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 9 / 41

slide-4
SLIDE 4

Global Variables

Global variables can be accessed within a function

x = 1 def f(): y = x + 1 print(y) return print(x) f() print(x)

global variable x local variable y gets value which depends on global variable x

  • utput global variable x
  • utput local variable y
  • utput global variable x

x = 1 def f(y): z = x + y return z print(x) print(f(2)) print(x)

global variable x parameter y local variable z gets value that depends on global variable x and parameter y

  • utput global variable x
  • utput local variable z
  • utput global variable x

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 10 / 41

Local Variables

Local and global variables can have the same name Shadowing Not forbidden, but should be avoided

x = 1 def f(): x = 2 return x print(x) print(f()) print(x)

global variable x local variable x local variable x is returned

  • utput global variable x
  • utput local variable x
  • utput global variable x

x = 1 def f(x): x = x + 1 return x print(x) print(f(2)) print(x)

global variable x parameter x parameter x gets new value that depends on its current value parameter x is returned

  • utput global variable x
  • utput parameter x
  • utput global variable x

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 11 / 41

Functions

Example – Counting Digits Counting Digits

Task Given is a list that contains digits from 0 to 9 6 5 4 2 4 5 7 8 9 1 1 3 2 4 5 We want to know how often these digits appear Procedure Create list occurrences with a cell per digit Initially, all entries are 0 Run through list In every step, increase value in cell that corresponds to entry

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 12 / 41

slide-5
SLIDE 5

Counting Digits

6 5 4 2 4 5 7 8 9 1 1 3 2 4 5 1 2 3 4 5 6 7 8 9

  • ccurrences

1 1 1 1 2 2 1 1 1 1 2 1 2 3 3

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 13 / 41

Exercise – Counting Digits

Write a function that gets a list x containing digits from the range [0, . . . , 9] uses return to return a list of length 10 that contains at position i the number of appearances of digit i in x

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 14 / 41

Counting Digits

def countdigits(x):

  • ccurrences = [0] * 10

for item in x:

  • ccurrences[item] += 1

return occurrences print(countdigits([1, 1, 1, 1, 1, 5, 6, 7, 1, 4, 6, 8]))

What happens if list may also contains letters?

➯ Project 1

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 15 / 41

Time Complexity of Algorithms

slide-6
SLIDE 6

Exercise – Primality Testing

Write a function that takes an integer x as parameter calculates whether x is prime uses the % operator depending on that either returns

True or False

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 16 / 41

Primality Test

def primetest(x): if x < 2: return False d = 2 while d < x: if x % d == 0: return False d += 1 return True

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 17 / 41

Primality Test

How long does it take the algorithm to produce the output? What is its time complexity? This depends on the number of loop iterations An absolute value does not make sense here The loop is iterated (roughly) x times (if x is prime)

➯ Time complexity grows with x . . . but how fast?

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 18 / 41

Time Complexity – Function of input size

We measure the time complexity as a function of the input size The input of our algorithm is a single number x In our computer, numbers are represented in binary Ignoring leading zeros, for n bits we obtain

2n−1 is 10 . . . 00

  • n

,

2n−1 + 1 is 10 . . . 01

  • n

, . . . , and 2n − 1 is 11 . . . 11

  • n

A number that is encoded with n bits has size around 2n

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 19 / 41

slide-7
SLIDE 7

Time Complexity – Technology Model

Random Access Machine Execution model: Instructions are executed one after the other (on one processor core) Memory model: Constant access time Fundamental operations: Computations (+, −, ·, . . . ) comparisons, assignment / copy, flow control (jumps) Unit cost model: Fundamental operations provide a cost of 1 Data types: Fundamental types like size-limited integer or floating point number

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 20 / 41

Time Complexity – Note

We are not completely accurate here Variables can have arbitrarily large values We assume that arithmetic operations can be done in constant time The time needed to add two n-bit numbers depends on n Encoding of a floating point number does not directly correspond to its size Surely an addition is faster than a multiplication

➯ Logarithmic cost model takes this into account, but we also won’t use it here

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 21 / 41

Time Complexity of Algorithms

Time Complexity of Our Primality Test Time Complexity of Our Primality Test

Suppose x is a prime number, encoded using n bits Number of loop iterations grows with size of x ≈ 2n Loop is iterated around 2n times We would like to count the fundamental operations Algorithm executes four operations per iteration

➯ In total roughly 4 · 2n operations

We would like to know how time complexity behaves when n grows

➯ Ignore constant 4

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 22 / 41

slide-8
SLIDE 8

Time Complexity of Algorithms

Asymptotic Upper Bounds Asymptotic Upper Bounds

The exact time complexity can usually not be predicted even for small inputs We are interested in upper bounds We consider the asymptotic behavior of the algorithm And ignore all constant factors Example Linear growth with gradient 5 is as good as linear growth with gradient 1 Quadratic growth with coefficient 10 is as good as quadratic growth with coefficient 1

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 23 / 41

Asymptotic Upper Bounds

Big-O Notation The set O(2n) contains all functions that do not grow faster than c · 2n for some constant c

O(g(n)) contains all functions f(n) that do not grow faster than c · g(n) for

some constant c, where f and g are positive Use asymptotic notation to specify the time complexity of algorithms We write O(n2) and mean that the algorithm behaves for large n like n2: when the input length is doubled, the time taken multiplies by four (at most)

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 24 / 41

Asymptotic Upper Bounds – Formal Definition

Big-O Notation – Formal Definition For every function g: N → R+ we have

O(g(n)) = {f : N → R+ | ∃c > 0, n0 ∈ N: ∀n ≥ n0 : f(n) ≤ c · g(n)}

We use O(g) and O(g(n)) interchangeably You will often find “f = O(g)” in the literature This should be read as f ∈ O(g)

➯ From f1 = O(g) and f2 = O(g) it does by no means follow that f1 = f2

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 25 / 41

slide-9
SLIDE 9

Asymptotic Upper Bounds – Illustration

g(n) = n2 f ∈ O(g) h ∈ O(g) n0 n

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 26 / 41

Asymptotic Upper Bounds – Examples

O(g(n)) = {f : N → R+ | ∃c > 0, n0 ∈ N: ∀n ≥ n0 : f(n) ≤ c · g(n)} f(n) f ∈ O(?)

Example

3n + 4 O(n) c = 4, n0 = 4 2n O(n) c = 2, n0 = 0 n2 + 100n O(n2) c = 2, n0 = 100 n + √n O(n) c = 2, n0 = 1

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 27 / 41

Asymptotic Upper Bounds – Property

f1 ∈ O(g) and f2 ∈ O(g) implies f1 + f2 ∈ O(g)

There are n1, c1 such that, for all n > n1, we have f1(n) ≤ c1 · g(n) Also, there are n2, c2 such that, for all n > n2, we have f2(n) ≤ c2 · g(n) Let n0 := max{n1, n2} and let c := max{c1, c2} Then f1(n) ≤ c · g(n) and f2(n) ≤ c · g(n), for all n ≥ n0 Therefore, f1(n) + f2(n) ≤ 2 · c · g(n), for all n ≥ n0

➯ It follows that f1 + f2 is in O(g)

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 28 / 41

Time Complexity of Algorithms

Asymptotic Lower Bounds

slide-10
SLIDE 10

Asymptotic Lower Bounds – Formal Definition

Big-Ω Notation – Formal Definition For every function g: N → R+ we have

Ω(g) = {f : N → R | ∃c > 0, n0 ∈ N: ∀n ≥ n0 : c · g(n) ≤ f(n)}

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 29 / 41

Asymptotic Lower Bounds – Illustration

g(n) = n f ∈ Ω(g) h ∈ Ω(g) n0 n

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 30 / 41

Time Complexity of Algorithms

Asymptotic Tight Bounds Asymptotic Tight Bound – Formal Definition

Definition For every function g: N → R+ we have

Θ(g) = Ω(g) ∩ O(g)

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 31 / 41

slide-11
SLIDE 11

Asymptotic Tight Bound – Illustration

h(n) = 0.5 · n2 g(n) = n2 f ∈ Θ(n2) n

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 32 / 41

Time Complexity of Algorithms

Time Complexity Analysis Notions of Growth

Function in Description Example O(1) constant arithmetic operation (simplified) O(log n) logarithmic binary sorted search O(n) linear unsorted naive search O(n log n) superlinear / loglinear good sorting algorithms O(n2) quadratic simple sorting algorithms O(nc) polynomial matrix multiply O(2n) exponential simple primality testing O(n!) factorial simple traveling salesman algorithm

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 33 / 41

Small n

1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 20 40 60 ln n n n2 n4 2n

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 34 / 41

slide-12
SLIDE 12

Larger n

2 4 6 8 10 12 14 16 18 20 22 24 0.2 0.4 0.6 0.8 1 ·106 log n n n2 n4 2n

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 35 / 41

“Large” n

10 20 30 40 50 60 70 80 90 100 0.2 0.4 0.6 0.8 1 ·1020 log n n n2 n4 2n

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 36 / 41

Logarithms

5 10 15 20 25 30 35 40 45 50 200 400 600 800 1,000 n n2 n3/2 log n n log n

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 37 / 41

Time Consumption

Assume that one fundamental operation takes 1µs

Input length 1 100 10000 106 109 log2 n 1µs 7µs 13µs 20µs 30µs n 1µs 100µs 1/100s 1s 17 minutes n log2 n 1µs 700µs 13/100µs 20s 8.5 hours n2 1µs 1/100s 1.7 minutes 11.5 days 317 centuries 2n 1µs 1014 centuries ≈ ∞ ≈ ∞ ≈ ∞

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 38 / 41

slide-13
SLIDE 13

Impact of Faster Computers

“Oh well. . . then I simply buy a new machine. ”

➯ If current machine can solve inputs of length n, then a 10 or 100 times faster

machine can solve. . .

Complexity (speed ×10) (speed ×100) log2 n n → n10 n → n100 n n → 10 · n n → 100 · n n2 n → 3.16 · n n → 10 · n 2n n → n + 3.32 n → n + 6.64

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 39 / 41

Asymptotic Complexity – Examples

n ∈ O(n2) is correct, but imprecise: n ∈ O(n) and even n ∈ Θ(n) 3n2 ∈ O(2n2) is correct but uncommon: omit constants: 3n2 ∈ O(n2) 2n2 ∈ O(n) is wrong, because 2n2

cn = 2 cn is not constant

O(n) ⊆ O(n2) is correct, Θ(n) ⊆ Θ(n2) is wrong n ∈ Ω(n2) ⊃ Θ(n2)

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 40 / 41

Problem Complexity

Definition The complexity of a problem P is given by the minimal (asymptotic) costs

  • ver all algorithms that solve P

Upper bound by supplying an algorithm Testing whether an n-bit number is prime has complexity in O(2n) Sorting n numbers (of constant length) has complexity in O(n · log n) Lower bounds are much harder to provide Better than Ω(n) only with additional assumptions

Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 41 / 41