# lab12.py # YOUR NAME(S) AND NETID(S) HERE # DATE COMPLETED HERE """Loop functions (with invariants) for lab 11""" def isprime(p): """Returns: True if p a prime, False otherwise i.e. p is at least 2 and is divisible by only 1 and itself Precondition: p is an int""" assert type(p) == int, `p`+' is not an int' if p < 2: return False # CHANGE THIS EXPRESSION, IF NECESSARY # Return False if some integer in 2..p-1 divides p k = 0 # CHANGE THIS EXPRESSION, IF NECESSARY # inv: p is not divisible by integers in 2..k-1 while k < p: if False: # CHANGE THIS EXPRESSION return False # Increment to next position k= k+1 # no integer in 2..p-1 divides p return False # CHANGE THIS EXPRESSION, IF NECESSARY def count_letter(c, s): """Returns: number of times character c appears in string s Precondition: s, c are strings. len(c) == 1""" assert type(s) == str, `s`+' is not a string' assert type(c) == str and len(c) == 1, `c`+' is not a character' # INITIALIZE LOOP VARIABLES HERE # inv: x is number of times c appears in s[0..k-1] while True: # CHANGE THIS EXPRESSION if False: # CHANGE THIS EXPRESSION x = x+1 # Increment to next position k = k + 1 return 0 # CHANGE THIS, IF NECESSARY # OPTIONAL FUNCTIONS def primes(n,m): """Returns: a list that contains each prime between n and m. Example: primes(3,12) returns [3,5,7,11] primes(-1,13) returns [2,3,5,7,11,13] Precondition: n, m are ints with m >= n.""" assert type(n) == int, `n`+' is not an int' assert type(m) == int, `m`+' is not an int' assert n <= m, 'range ['+`n`+','+`m`+'] is invalid' x = NONE # CHANGE THIS EXPRESSION c = 0 # CHANGE THIS EXPRESSION # inv: list x contains each prime in n..c-1 while c <= 0: # CHANGE THIS EXPRESSION if False: # CHANGE THIS EXPRESSION x.append(0) # CHANGE THIS COMMAND # Increment to next position c = c + 1 # post: list x contains contains each prime in n..m return x def count_all_letters(s1, s2): """Returns: number of times the characters in s1 appear in s2 Each letter in s1 is treated separately. Duplicates in s1 are ignored (but not ignored in s2). Example: count_all_letters('ac', 'aaab') is 3 Example: count_all_letters('aeiou', word) gives the number of vowels in string word (for some definitions of vowels) Precondition: s1, s2 are strings""" assert type(s1) == str, `s1`+' is not a string' assert type(s2) == str, `s2`+' is not a string' # STUB: IMPLEMENT ME # Hint1: Don't nest one while loop in another; # use count_letter as a helper function # Hint2: You need to find the unique elements # of s1 at the same time that you are counting return 0 # CHANGE THIS, IF NECESSARY