cookie calculator
play

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


  1. Cookie Calculator Repetition Hans-Joachim Böckenhauer and Dennis Komm Digital Medicine I: Introduction to Programming Local variables, scopes, and complexity Autumn 2019 – October 31, 2019 Example – Cookie Calculator Cookie Calculator – Check Input From this . . . children = int(input("Number of children:")) children = int(input("Number of children:")) cookies = int(input("Number of cookies:")) . . . we go to this print("Every child receives", cookies // children, "cookies") while True: print("Dad receives", cookies % children, "cookies") children = int(input("Number of children:")) if children >= 1: break else: We want to make sure that children is positive and that each child gets at print("Value needs to be at least 1") least one cookie Analogously, we have to check that cookies >= children Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 1 / 41 Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 2 / 41

  2. Cookie Calculator – Getting Complicated Cookie Calculator – Takeaway while True: children = int(input("Number of children:")) Read and check The two code fragments are nearly identical if children >= 1: break number of children else: The following aspects are different: print("Value needs to be at least 1") The prompt, i.e., "children:" vs. "cookies:" while True: cookies = int(input("Number of cookies:")) The minimum, i.e., 1 vs. children Read and check if cookies >= children: break number of cookies We can outsource the code fragment into a function and thus feature reuse else: print("Value needs to be at least", children) We have to parameterize the different aspects 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 3 / 41 Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 4 / 41 Exercise – Cookie Calculator Exercise – Cookie Calculator def checkinput(prompt, minimum): Write a function that while True: gets two parameters prompt and minimum x = int(input(prompt)) if x >= minimum: asks the user for an integer input return x else: returns the input using return if it is at least print("Value needs to be at least", minimum) minimum otherwise asks for a new input children = checkinput("Number of children:", 1) cookies = checkinput("Number of cookies:", children) Use the function in the cookie calculator 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 5 / 41 Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 6 / 41

  3. Local Variables Parameters of a function are only valid within this function Functions def f(x): return x + 5 Scope and Lifetime of Variables 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 Local Variables Such variables (parameters) are called local variables The same is true for variables that are defined in a function Variables defined outside of a function are called global variables Field of validity is called scope of the variable def f(x): Time in which it is defined is called lifetime of the variable y = 5 return x + y def f(x): def f(x): print(y) if x < 0: if x < 0: return -100 return -100 y = x + 1 y = x + 1 scope of x scope of y if y < 10: if y < 10: NameError: name ’y’ is not defined y += 10 y += 10 else: else: y -= 20 y -= 20 return y return y Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 8 / 41 Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 9 / 41

  4. Global Variables Local Variables Local and global variables can have the same name Global variables can be accessed within a function Shadowing x = 1 x = 1 Not forbidden, but should be avoided def f(): global variable x global variable x def f(y): x = 1 x = 1 y = x + 1 z = x + y parameter y print(y) return z def f(x): global variable x global variable x def f(): return local variable y gets value local variable z gets value x = 2 x = x + 1 print(x) output global variable x which depends on global variable x that depends on global variable x parameter x return x return x print(x) output global variable x output local variable z print(f(2)) local variable x parameter x gets new value and parameter y f() output local variable y print(x) output global variable x local variable x is returned print(x) output global variable x parameter x is returned print(x) output global variable x that depends on its output global variable x print(x) print(f()) output local variable x output parameter x print(f(2)) current value print(x) output global variable x print(x) output global variable x Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 10 / 41 Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 11 / 41 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 Functions We want to know how often these digits appear Example – Counting Digits 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

  5. Counting Digits Exercise – Counting Digits 6 5 4 2 4 5 7 8 9 1 1 3 2 4 5 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 occurrences 0 0 2 1 2 1 0 1 0 2 0 1 3 3 0 2 1 0 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 13 / 41 Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 14 / 41 Counting Digits def countdigits(x): occurrences = [0] * 10 for item in x: Time Complexity of Algorithms occurrences[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

  6. Exercise – Primality Testing Primality Test Write a function that def primetest(x): if x < 2: takes an integer x as parameter return False calculates whether x is prime d = 2 while d < x: uses the % operator if x % d == 0: depending on that either returns return False True or False d += 1 return True Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 16 / 41 Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 17 / 41 Primality Test Time Complexity – Function of input size We measure the time complexity as a function of the input size How long does it take the algorithm to produce the output? The input of our algorithm is a single number x In our computer, numbers are represented in binary What is its time complexity ? Ignoring leading zeros, for n bits we obtain This depends on the number of loop iterations An absolute value does not make sense here 2 n − 1 is 10 . . . 00 2 n − 1 + 1 is 10 . . . 01 and 2 n − 1 is 11 . . . 11 , , . . . , � �� � � �� � � �� � The loop is iterated (roughly) x times (if x is prime) n n n ➯ Time complexity grows with x . . . but how fast? A number that is encoded with n bits has size around 2 n Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 18 / 41 Digital Medicine I: Introduction to Programming – Local variables and scopes Autumn 2019 Böckenhauer, Komm 19 / 41

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend