### 2018-02-13

posted Feb 13, 2018, 5:33 AM by Konstantinovich Samuel   [ updated Feb 13, 2018, 7:17 AM ]
 `Both of these functions demonstrate that you can modify a numerical sequence in various ways within a single while loop.````def mrE(n): current = 0 count = 0 while current < n: if current % 3 == 0: current = current * 2 + 1 else: current = current + 1 count = count + 1 return count ``````def mrF(n): current = 0 count = 0 increment = 1 while current < n: current = current + increment increment = increment + 1 count = count + 1 return count````DO NOW: Evaluate the functions that are to be printed by tracing through the loop.hint: write out the value of all variables and how they change EACH iteration of the loop.``print "mrE(29) =",mrE(29)print "mrF(29) =",mrF(29)``DISCUSS + Write modifications``How can the functions be modified to :``a)calculate the sum of the values``b)print out all of the values.``STRINGS:``A complete list of string methods are here:https://docs.python.org/2/library/stdtypes.html#string-methodss='abcdbcA' #Get single characters:s[0] # 'a's[1] # 'b'#slices of the string s[i,j] gives you the charactes starting at index i#up to but not including index j. This can be multiple characters! s[1:3] # 'bc'   s[2:7] # 'cdbcA'#using a variable:s[x:x+n] gives you n letters, starting at x.#There is a function to calculate the length.len(s)  # 7 #examples:#Access and print each letter inside the string:s = "Hello!!!"i = 0while i < len(s):   print s[i]   i = i + 1``Classwork + Complete the functions for homework (they will need to be submitted on codingbat at a later date.``Non loop functions:``Write a function hello(name) that returns the string "hello ???"where ??? is the contents of the string stored in the parmeter name.e.g.Function Call Returned Valuehello("bob") "hello bob"hello("Keiko") "hello Keiko"hello("Bismark") "hello Bismark"``Write a function firstHalf(s) that returns the first half of the string. ``(rounded down for odd length strings)``e.g. ``Function Call Returned Value``firstHalf("asdf") "as"firstHalf("asfdf") "as"firstHalf("fishfishfish") "fishfi"firstHalf("Y XT") "Y "Loop functions:``Write a function count(s,letter) that returns the number of occurences of letter inside the string s. Assume letter is length 1, and s is 0 or more characters long. (Do not use the built in count)e.g. Function Call Returned Valuecount("abba!","a") 2count("ABBA","a") 0count("Fish fry","f") 1Write a function find(s,letter) that returns the index of the first occurence of letter inside the string s. Assume letter is length 1,and s is 0 or more characters long. When letter is not found, return -1.(do not use the built in find)e.g. Function Call Returned Valuefind("abba!","a") 0find("ABBA","a") -1find("Fish fry","f") 5``More Functions!``isVowel(letter) - returns true when letter is a vowel (not Y). False otherwise. ``Both upper and lower case vowels should be true.``e.g.`isVowel("A") -> TrueisVowel("e") -> True`isVowel("Y") -> FalseisVowel("f") -> FalseisVowel("G") -> FalseWrite a function countVowels(s) that returns the number of occurences of vowels in the string s. Assume s is 0 or more characters long. e.g. Function Call Returned ValuecountVowels("abba!") 2countVowels("ABBA") 2countVowels("abcdeou") 4countVowels("Fish fry") 1`