# in operator # in operator checks to see if there's a value on the left side # of the operator and a sequence on the right side of the operator # String Example ch = 'C' s = 'Computer' if ( ch in s ): # tells us if a character is in a string pass # List Example element = 'Nadia' list_staff = ['Professor Cohoon', 'Nadia', 'Ann', 'All of the other amazing TAs <3'] if ( element in list_staff ): # tells us if the element is in a list pass # Cannot check the entire list for a substring # Can only check if the entire element 'l' is in the list # if one of the elements has an 'l' in it: (doesn't work -need for loop) # checks for element 'l' if ('l' in list_staff): # (evaluates to False) because 'l' is not an element pass # If you want to check if 'l' is in any of the strings: for p in list_staff: if ( 'l' in p ): print( p ) # The format of an in operator is # if (what we wanna find) in (sequence) # for loopvariable in sequence: for ch in s: # in operator allows you to see whether the left operand is part of the right operand print( ch ) # Nested Looping is usually for datasets! (digitbox.py was the exception) # Reminder: dataset is a list of lists # [[1,2,3], [4,5,6]] # ^ column (or cell) # row row # for row in dataset: # for column/cell in row: # ... # Nested Looping # We must finish the iteration in the inner for loop before # moving onto the next value of the outer for loop for i in range(1,4): for j in range(1,4): print( i, j ) # i = 1 # j = 1 # j = 2 # j = 3 # i = 2 # j = 1 # j = 2 # j = 3 # i = 3 # j = 1 # j = 2 # j = 3 # While Loop - no while loops on exam! # Basically a while loop is like a for loop BUT # it's conditional. It will only run while the condition inside is true! # while (condition): # if condition evaluates to true then we jump into the loop # and run forever until we stop it / condition is evaluated to false! looking_for_positive_number = True while( looking_for_positive_number == True ): # while the variable running our loop is True reply = input("Enter a number: ") int_reply = int( reply ) if ( int_reply > 0 ): # even number check would be if( int_reply % 2 == 0 ) looking_for_positive_number = False # stops the while loop # or break! # Dataset Example # Spring 2020 function nbr( v, d ) # This function hands back a list of the number of occurences of v in each row of the dataset d # d1 = [ [ 3, 1, 4, 1, 5, 9, 2 ], # [ 6, 5, 3, 5, 8, 9, 7, 9, 3, 2 ], # [ 3, 8, 4, 6, 2, 6, 4, 3, 3 ], # [ 8, 3, 2, 7, 9, 5, 0 ] ] # nbr( d1, 9 ): [1, 2, 0, 1] (output) # d is d1 and v is 9 # The count of 9 in each row ^ each inner list is [1,2,0,1] def nbr( d, v ): # This function takes in a value and a dataset # This function hands back a list of the number of occurences of v in each row of the dataset d result = [] # Accumulator where we will add the count of v in each row for row in d: # How many copies of v in each row? row_counter = 0 # Accumulator to get the row count for cell in row: # Go through each cell in each row if ( cell == v ): # Add 1 to the count for each cell that equals v row_counter = row_counter + 1 result.append( row_counter ) # Add the row counter (accumulator) to the list for each row return result # Alternate Version of nbr(v,d): (no double for loop) # result = [] # for row in d: # count_v_in_row = row.count( v ) # Count the number of elements in the list row # result.append( count_v_in_row ) # return result # Example of a dictionary problem! def build( x, y ): # Build a dictionary out of x and y where each # element in x at index position i is mapped to # each element in y at index position i nx = len( x ) ny = len( y ) if ( nx != ny ): # The lengths of the lists are not equal return None # Cannot build a dictionary return None # If it doesn't jump into the if statement on line 119, it # goes to the next line so we don't include an else (we don't need an else) # but you can use it if you want! # for value in x: # We don't want to go through each "value" because then you # map multiple y values to a single x value result = {} # Dictionary Accumulator for i in range( 0, nx ): k = x[i] # Element from list x at position i v = y[i] # Element from list y at position i result[k] = v # Create a mapping of the key to the value return result # Hodge.py # Return whether w has a vowel in it def has_vowel( w ): # has_vowel takes in a single string word w VOWELS = 'aeiou' # or ['a','e','i','o','u'] if you want a list # Return True if it has as vowel (the minute we find a vowel, we can return True!) for ch in w: if ( ch in VOWELS ): return True return False # We never returned True so that means none of the characters were vowels # You don't want to do else: # return False # Because that will stop your loop before checking all the characters # Once you hit a return statement, we don't go through the rest of the loop/ # execute any more code! # Alternate: # found_vowel = False # for ch in w: # if ( ch in VOWELS ): # found_vowel = True # return found_vowel # Another alternate (no loop) # VOWELS = 'aeiou' # 01234 # if ('a' in w): # or VOWELS[0] # return True # elif ('e' in w): # or VOWELS[1] # return True # elif ('i' in w): # or VOWELS[2] # return True # elif ('o' in w): # or VOWELS[3] # return True # elif ('u' in w): # or VOWELS[4] # return True # else: # return False # You can also do: # for vowel in VOWELS: # Go through list of vowels # if vowel in w: # check if vowel we're on in VOWELS is in w # return True # if it is word has a vowel # return False # Spring 2020 com.py # w = ( x // ( y • z ) ) # Number of ways we're calculating (return value) # where # x = 1 • 2 • 3 • … • n # y = 1 • 2 • 3 • … • c # z = 1 • 2 • 3 • … • ( n – c ) # Returns number of combinations of choosing c things from n things def sob( n, c ): # Factorials (x,y,z) x = 1 # Accumulator starts at 1 for multiplication (product accumulator) for i in range( 1, n+1 ): x = x * i y = 1 # Accumulator starts at 1 for multiplication (product accumulator) for i in range( 1, c+1 ): y = y * i z = 1 # Accumulator starts at 1 for multiplication (product accumulator) for i in range( 1, ( n-c )+1 ): z = z * i w = ( x // ( y * z )) return w # One loop will not work for this problem because the ranges are different # for the calculations for the for loops for x, y, and z. So you need 3 loops! # It's 3 small product accumulations! and then a calculation using those # accumulated values! # Spring 2019 al.py # Test input # x0 = [1, 1]; x1 = [1, 2, True, 2]; x2 = [ 1, 2, 'a']; x3 = [] # y0 = [1, 1, 1]; y1 = [True, 2, 2, 1]; y2 = [2, 1, 2]; y3 = [] # This function takes in 2 lists and it returns whether these lists # contain the same elements and number of elements # Same lists but can be different orders! def ike( x, y ): # Get the lengths of the two lists nx = len( x ) ny = len( y ) if ( nx != ny ): # If your two lists are not the same length, they are not the same list! return False else: # For the elements in the list, what matters here? # The number of times the element occurs! (aka the count) # Go through each element in one list and see if it's count is the same as its # count in the other list things_look_good = True for element in x: # Check if the number of times element occurs in x is equal to # the number of times the element occurs in y # Calculate the counts of each element in x and y count_element_x = x.count( element ) count_element_y = y.count( element ) if ( count_element_x != count_element_y ): things_look_good = False # The lists aren't alike! Can also return False # If we go through the entire loop and we don't return False / find # the counts aren't the same, then we know they're alike! return things_look_good # Can also return True # If all the counts of the elements are the same, you know that # every count of an element in y also matches the count of the element in x # Ex. x1 = [1, 2, True, 2] y1 = [True, 2, 2, 1] # There is a count of 1 for 1 in both x and y # There is acount of 2 for 2 in both x and y # There is a count of 1 for True in both x and y # All counts match - we left nothing out! # This input would return True for ike! # Condensed ike: # def ike( x, y ): # if ( len(x) != len(y) ): # return False # for element in x: # if ( x.count(element) != y.count(element) ): # return False # return True # Yes, it'll be in artifacts :) # != means not equals # == means equals # Fall 2019 No. 15 fi.py # GOOD PRACTICE AND GOOD GENERAL FORMAT FOR SEQUENCE ACCUMULATOR PROBLEMS # 1/2^0 + 1/2^1 + 1/2^2 + 1/2^3 + ... 1/2^n def ser( n ): # If it's a sequence, chances are you want a for loop! sum_sequence = 0 # Accumulator for i in range( 0, n+1 ): # Goes from 0 to n ith_value = 1 / ( 2 ** i ) sum_sequence = sum_sequence + ith_value # Add each term of the sequence to the sum_sequence return sum_sequence