#! /usr/bin/python # ******************************************************** # CS 200 HW #0 DUE Thursday 9/14/23, 11:59 pm # ** using the Zoo submit system ** # ******************************************************** # Name: # Email address: # ******************************************************** # This file may be loaded into Python using import, or run from the # UNIX command line. # Lines beginning with hash marks are comments. # Homework #0 will be worth 20 points -- other # homeworks will be worth 100 points. # One purpose of homework #0 is to make sure # you can use the submit system on the Zoo. # You will receive no credit for this assignment # until you successfully use the submit system to submit it. # You will be submitting *two* files for homework #0. # Please name them: # hw0.py (for the Python definitions and procedures) # response.txt (for the reading response) # ******************************************************** # ** problem 0 ** (1 easy point) # replace the number 0 in this definition to # indicate how many hours you spent doing this assignment. # Fractions are fine, eg, 3.14159. def hours(): return 0 # ******************************************************** # ** problem 1 ** (5 points) ''' Write a procedure productorsum(x, y) which takes two integer arguments, and returns their product, unless the product is greater than 1000, in which case it returns their sum. Examples >>> productorsum(1,2) => 2 >>> productorsum(100,200) => 300 >>> productorsum(40,50) => 90 >>> productorsum(40,-50) => -2000 ''' # ******************************************************** def productorsum(x,y): pass # Replace the pass statement above with your procedure # Your procedure will # be tested automatically, but it will # be called only on integer arguments. # ******************************************************** # ** problem 2 ** (4 points) # Write a procedure food() # that takes no arguments and # returns a *string* indicating # your favorite food # Example (yours will likely be different) # food() => "bacon" # ******************************************************** def food(): pass # Replace the pass statement above this comment with your procedure. # Note that it is named food and takes # no arguments. Your procedure will be # tested automatically, and will be called # only with no arguments. Note the parentheses # following food indicating a procedure # application. # ******************************************************** # ** problem 3 ** (10 points) # For this problem, you are asked to find one # article (of 2 pages or more) in # the magazine "Communications of the ACM", # in one of the issues: June, July, August, # September 2023 # See http://cacm.acm.org/ # Read the article and answer # the following three questions: # a. What did you know about the topic # prior to reading the article? # b. What did you learn from reading the # article? # c. What more would you like to know # about the topic? # Your answer should be *at most* 400 words, # saved in .txt format, and submitted as # the file response.txt for assignment 0. # Please include in your file # your name and email address # and the title and author(s) of the article # you are responding to. ### test function from google python course ### with modifications for function calls ### ======================================= # Provided simple test() function used in main() to print # what each function returns vs. what it's supposed to return. def test(got, expected): if (hasattr(expected, '__call__')): OK = expected(got) else: OK = (got == expected) if OK: prefix = ' OK ' else: prefix = ' X ' print ('%s got: %s expected: %s' % (prefix, repr(got), repr(expected))) # Provided main() calls the above functions with interesting inputs, # using test() to check if each result is correct or not. def main(): print ('hours') print('# is it greater than 0?') test(hours(), lambda x: x > 0) print print ('productorsum') test(productorsum(1,2), 2) test(productorsum(100,200), 300) test(productorsum(40,50), 90) test(productorsum(40,-50), -2000) print print ('food') print('# is it a string?') test(food(), lambda s: type(s) == type('')) # Standard boilerplate to call the main() function. if __name__ == '__main__': main() # ******************************************************** # ******** end of homework #0 # ********************************************************