def y( m , x, b ): # Define a function y() with three parameters m, x, and b result = m * x + b return result # Tester print( y( 3, 5, 7) ) print( y( 5, 7, 3) ) print( y( 7, 3, 5) ) print() def words( s ): # Define words() with a parameter s list_words = s.split() # Split the string into a list nbr_words = len( list_words ) # Get the length of the list return nbr_words # Return that number of words # Tester print( words( "The cow mooed and mooed") ) print( words( "All things must pass" ) ) print( words( "I have a dream that one day") ) print() def one( w, x, y, z ): # This function defines one() with four parameters w, x, y, z list_booleans = [w,x,y,z] # Create list of booleans n = list_booleans.count( True ) # Count how many booleans are True if ( n == 1 ): result = True else: result = False return result # Alternate # n = list_booleans.count( True ) # result = ( n == 1 ) # evaluates to True or False # return result # Tester print( one( True, False, False, False) ) print( one( False, False, True, True) ) print( one( False, False, True, False) ) print( one( False, True, False, False) ) print( one( False, False, False, False) ) print() def in_order( x ): # Defines in_order() with one list parameter x n = len( x ) for i in range( 0, n - 1 ): a = x[i] # Current index element in list b = x[i + 1 ] # Next index element in list # Compare a and b if ( a > b ): # Compare the current and next thing return False # Out of order! return True # Alternate way # Check if the sorted version matches the list passed in # sorted_version_x = sorted( x ) # This way is easier :) # if ( sorted_version_x == x ): # return True # else: # return False # Tester print( in_order( [1] ) ) print( in_order( [2, 5, 4] ) ) print( in_order( [5, 6, 8, 8] ) ) print( in_order( [7, 7, 1, 7, 9] ) ) print() def erse( d ): # Defines a function erse() with a single parameter d dataset = [] for row in d: new_dataset_row = [] for cell in row: inverse_cell = -cell # or -1 * cell new_dataset_row.append( inverse_cell ) # Add each inverse cell to the row dataset.append( new_dataset_row ) return dataset # Tester d1 = [ [ 3, 1, -4 ], [ 1, 5 ], [ -9, -2], [ -6 ] ] d2 = [ [ 1 ], [ 0 ], [-1 ] ] print( erse( d1 ) ) print( erse( d2 ) ) print() import random def ter( n, k ): # Defines a function ter() with two parameters n and k result = [] for i in range( 0, n ): # Do this n times (same as (1,n+1)) random_value = random.randrange( 0, k ) # produces a random value between 0 and k-1 result.append( random_value ) return result # Tester t1 = [ 5, 2 ] t2 = [ 8, 10 ] tests = [ t1, t2 ] for i in range( 0, len( tests ) ) : n, k = tests[ i ] random.seed( 1112 ) r = ter( n, k ) print( "ter( " + str(n) + ", " + str(k) + " ):", r ) print() def metric( d ): # Defines a function metric() with one dictionary parameter d for k in d: # same as for key in d.keys() v = d.get( k ) # or d[k] - v is k's value # Check if value was also a key mapped to the original key if ( d.get( v ) != k ): # Gets the value for v as a key in our dictionary return False # Dictionary not symmetric return True # All values were also mapped to the keys - symmetric! # for k in d: # v = d[ k ] # Get the value for that key # if ( v not in d.keys() ): # If the value is not a key # return False # not symmetric! # elif ( d[v] != k ): # If v's value is not k # return False # return True # Tester print( metric( {1: 2, 2: 3, 3: 1} ) ) print( metric( {1: 2, 2: 1, 3: 4, 4: 3} ) ) print( metric( {1: 2, 2: 5} ) ) print() def lusive( x, y ): # Define a function lusive() with two list parameters x and y result = [] # All the things x has not in y for element in x: if ( element not in y ): result.append( element ) # All the things y has not in x for element in y: if ( element not in x ): result.append( element ) return result # Tester print( lusive( [3, 5, 9], [2, 5, 3, 5, 8, 8] ) ) print( lusive( [9, 7, 3, 2], [2, 3, 7] ) ) print( lusive( [3], [1, 4] ) ) print( lusive( [], [1, 2, 3] ) ) print( lusive( [1, 2], [1, 2] ) ) # As long as you produced the same results and used # functions we've covered / from the modules and stuff # and gave it your best effort, you got it :)