''' Purpose: consider how to solve questions from Test 3 Fall 2019 ''' # We will provide files for the modules # for you to write the functions in. ''' TRUTH.PY ''' def dare(): # no parameters ''' Returns the logical value True''' # return 'True' # not the logical value! return True ''' EVAL.PY ''' def eval( n1, c, n2 ): # 3 parameters # Check what string we pass in for c # ('+', '-', '*') and return the result # of the right operation if ( c == '+' ): result = n1 + n2 elif ( c == '-' ): result = n1 - n2 elif ( c == '*' ): result = n1 * n2 else: result = None return result ''' GROW.PY ''' def up( s ): # "single string parameter s" result = '' # build up our string for ch in s: # Add each character twice to the result result = result + ch + ch return result # return string answer ''' CASE.PY''' def swap( s ): # single string parameter s ''' Returns s but all the characters are opposite case''' # Accumulator result = '' # We are building up a string for ch in s: # Look at each character in s if ( ch == ch.lower() ): # ch is lowercase result = result + ch.upper() # Add the uppercase version of ch else: # ch is uppercase result = result + ch.lower() # Add the lowercase version of ch return result ''' STRAY.PY ''' def conv( s ): # single string parameter s result = [] # We are building up a list for ch in s: # Look at each character in s result.append(ch) return result #result = result.append( ch )# Add each ch to our result list # ^ DONT DO THIS # We don't do result = result.append(x) # It's just result.append(x) # No result = because .append() # returns None ''' GEO.PY''' def series( x, n ): result = [] # We want to build up a new list for i in range(0,n): term = (-x)**(i+1) result.append( term ) return result # for i in range(1,n+1): # 1 to n # term = (-x)**(i) # result.append( term ) # return result ''' RING.PY ''' def chomp( s, i, j ): # Three parameters prefix = s[ 0 : i ] suffix = s[ j : ] result = prefix + suffix return result # 'ABCDEFGHIJ' # 0123456789 # s[0:i] -> s's characters from 0 to i - 1 # s[ j: ] -> s's characters from j onwards # s[i:j] gets characters from index i to j-1 # Get slice[0:2] + slice[6:] # -> 'AB' + 'GHIJ' -> 'ABGHIJ' ''' TAB.PY ''' def square( x ): # single dataset parameter x ''' Returns True if number of columns is equal to the number of rows in the dataset x''' # Recall a dataset is [[...],[...]] number_rows = len( x ) # number of # smaller lists (rows) in your outer list for row in x: number_columns_in_row = len( row ) # if # of rows is not equal to # of columns # == is testing if they're equal # != is testing if they're NOT equal if ( number_columns_in_row != number_rows) : return False # else: # We don't want this! # It wouldn't look at the other rows. # return True (can do continue/pass) return True # We never found a row where # the number of columns didn't equal the # number of rows sooo we can return True :) # Remember that once you hit a return # statement, the rest of your code # isn't evaluated. Your function is # DONE once you hit a return statement. ''' DATE.PY ''' def avg( x ): # single dataset parameter x # sum(dataset) doesn't work! # Accumulators sum_dataset = 0 nbr_values_in_dataset = 0 for row in x: # Go through each row in the dataset sum_row = sum( row ) # Calculate this to add the sum of the row numbers to the sum accumulator nbr_row = len( row ) # Calculate this to Add the number of elements to the # values accumulator sum_dataset = sum_dataset + sum_row # Update sum accumulator nbr_values_in_dataset = nbr_values_in_dataset + nbr_row # Update average = sum_dataset / nbr_values_in_dataset return average # Remember that when you write your # function definitions, make sure you just # name the function the same name as our instructions # and how many parameters / names of parameters # matches the instruction description. # "No parameters", "Three parameters", etc. # No you will not write testers. # You won't have to write the # if __name__ == '__main___' part! # Just write the functions and run them # using the testers we provide. # Please don't mess with testers. # On the exam for the coding portion, you # will be writing functions in modules # that you can use OUR testers (provided) # to run the code written in the modules. ''' TESTER ''' # This is the tester. YOU don't have to # worry about writing this part. # Just a way for us to check one function # at a time. check_dare = False check_eval = False check_up = False check_swap = False check_conv = False check_series = False check_chomp = False check_square = False check_avg = True if ( __name__ == '__main__' ) : if ( check_dare == True ) : result = dare() print( result ) if ( check_eval == True ): for c in ['+','-','*','%']: result = eval(2, c, 3) print( result ) if ( check_up == True ): for s in ['abc','woah!']: result = up( s ) print( result ) if ( check_swap == True ): for s in ['ee cummings', 'CS 1112', 'aBcD']: result = swap( s ) print( result ) if ( check_conv == True ): for s in ['apple', 'banana']: result = conv( s ) print( result ) if ( check_series == True ): tests = [(2,5), (3,4)] for test in tests: x, n = test result = series( x, n ) print( result ) if ( check_chomp == True ): result = chomp('ABCDEFGHIJ', 2, 6) print( result ) if ( check_square == True ): tests = [ [[2]], [[2, 4, 6]], [[2], [4], [6]] , [[2, 4, 6], [1, 3, 5], [7, 8, 9]] ] for test in tests: result = square( test ) print( result ) if ( check_avg == True ): tests = [ [[1, 2]], [[2, 4, 6], [8], [10, 12]] ] for test in tests: result = avg( test ) print( result )