def q_1(result:dict): """ Autograder for You are given a dictionnary *dIn* (that you should not modify) and a list of key-words *keyIn* that may or may-not be used within *dIn*. Construct a new dictionnary containing only the keys (and the corresponding values in *dIn*) that appear as keywords in *dIn* **and** *keyIn*. :param result: :return: bool """ isOk = False assert ((__dict is not None) and (__keys is not None)), "Not initialized" try: print("Provided result {0}".format(result)) if not isinstance(result,dict): print("Is not a dict -> false") else: # Get the dict resDict = dict( [(aKey, __dict[aKey]) for aKey in __keys if aKey in __dict.keys()] ) isOk = (result == resDict) print("The provided result is {0}".format(isOk)) except: print("Automatic string conversion failed, result cannot be correct") writeRes("{0}_1 : {1:d} of 1".format(__globString, isOk)) return isOk
def q_1(result: List[int]): """ Autograder for Generate a list containing all integers which are the square of some other integer in the range [10, 10000[ using list comprehension in its **for** - **if** form and save it to *res_3_1* :param result: :return: bool """ isOk = False try: print("Provided result {0}".format(result)) if not isinstance(result, List): print("Is not a list -> false") else: if not all([isinstance(aInt, int) for aInt in result]): print("All elements must be integer -> false") else: trueList = [ i for i in range(10, 10000) if int((int(i**.5))**2) == i ] isOk = (result == trueList) print("The provided result is {0}".format(isOk)) except: print("Automatic string conversion failed, result cannot be correct") writeRes("{0}_1 : {1:d} of 1".format(__globString, isOk)) return isOk
def q_1(result:float): """ Autograder for Use string formating and the eval command to determine the rounding error made by rounding ππ to a five digit expression. Store the obtained error in res_2_1 :param result: :return: bool """ from math import pi isOk = False try: print("Provided result {0}".format(result)) if not isinstance(result,float): print("Is not an float -> false") else: piround = eval("{0:.5f}".format(pi)) pierr = pi - piround isOk = (result == pierr) print("The provided result is {0}".format(isOk)) except: print("Automatic string conversion failed, result cannot be correct") writeRes("{0}_1 : {1:d} of 1".format(__globString, isOk)) return isOk
def q_1(result: int): """ Autograder for Store the square of $\pi$ rounded to the next smaller integer in the variable called *res_1_1* :param result: :return: bool """ from math import pi, floor isOk = False try: print("Provided result {0}".format(result)) if not isinstance(result, int): print("Is not an integer -> false") else: isOk = (result == floor(pi**2)) print("The provided result is {0}".format(isOk)) except: print("Automatic string conversion failed, result cannot be correct") writeRes("{0}_1 : {1:d} of 1".format(__globString, isOk)) return isOk
def q_2(refTarg: int, refList: List[int], result: List[int]): """ Autograder for You are given a random string (in length and characters) stored in *s_in* and must extract the shortest possible substring starting after the first appearance of "iizu" and ending before "8oz". Store it in *res_2_2*. :param result: :return: bool """ from math import fabs assert ((__targInt is not None) and (__intList is not None)), "Not initialized" isOk = False try: print("Provided result {0}".format(result)) if not isinstance(result, list): print("Is not a list -> false") else: if not ((refTarg == __targInt) and (refList == __intList)): print("Modified target or list -> false") else: if not all([isinstance(aInt, int) for aInt in result]): print("All elements must be integer -> false") else: #Get the correct list newList = [ fabs(aInt - __targInt) for aInt in __intList if not aInt == __targInt ] isOk = (result == newList) print("The provided result is {0}".format(isOk)) except: print("Automatic string conversion failed, result cannot be correct") writeRes("{0}_2 : {1:d} of 1".format(__globString, isOk)) return isOk
def q_2(result:str): """ Autograder for You are given a random string (in length and characters) stored in *s_in* and must extract the shortest possible substring starting after the first appearance of "iizu" and ending before "8oz". Store it in *res_2_2*. :param result: :return: bool """ isOk = False assert len(__stringQ2), "Not initialized" try: print("Provided result {0}".format(result)) if not isinstance(result,str): print("Is not a string -> false") else: #Get the substring idx0 = __stringQ2.find(__subString1)+len(__subString1) idx1 = __stringQ2.find(__subString2) subString = __stringQ2[idx0:idx1] isOk = (result == subString) print("The provided result is {0}".format(isOk)) except: print("Automatic string conversion failed, result cannot be correct") writeRes("{0}_2 : {1:d} of 1".format(__globString, isOk)) return isOk
def q_2(result: bool): from math import pi #"Determine wether the fourth root of $2^\\pi$ is confined between the closed interval between one and two, save the answer in *res_1_2*" isOk = False try: print("Provided result {0}".format(result)) if not isinstance(result, bool): print("Is not an boolean -> false") else: isOk = (result == 1. <= (2 ^ pi)**(1. / 4.) <= 2.) print("The provided result is {0}".format(isOk)) except: print("Automatic string conversion failed, result cannot be correct") writeRes("{0}_2 : {1:d} of 1".format(__globString, isOk)) return isOk
def q_1(func: Callable): """ Autograder for Code a recursive function that computes and returns the factorial of a number, that is the function has to accept integers and return (as first output) the corresponding factorial (also as integer). It should moreover raise a TypeError if anything other then an integer is given as input. :param result: :return: bool TBD check for a better solution to check recursion """ from random import randint from math import factorial isOk = False if not callable(func): print("No function or callable provided -> False") writeRes("{0}_1 : {1:d} of 1".format(__globString, isOk)) return isOk listTest = [randint(-200, -100) for _ in range(20) ] + [-2, -1, 0, 1, 2] + [randint(100, 200) for _ in range(20)] isOk = True #Build the decorator callCounter = 0 isDone = False for aInt in listTest: if isDone: break if aInt < 0: try: func(aInt) except ValueError: pass except ... as me: print("Failed on func({0}) without ValueError with {1}".format( aInt, me)) isOk = False isDone = True continue else: isOk = False isDone = True continue else: if aInt > 100: try: sys.setrecursionlimit(aInt - 2) func(aInt) print( "Succeeded to compute {0}! with a recursion limit of {1} -> not an recursive algorithm" .format(aInt, aInt - 2)) isOk = False isDone = True continue except RecursionError: pass except ... as me: print("Failed on func({0}) with {1}".format(aInt, me)) isOk = False isDone = True continue try: if aInt > 100: sys.setrecursionlimit( aInt + 4) # I do not know why plus 4 is necessary res = func(aInt) except RecursionError: print("Failed on func({0}) as it took too many recursions". format(aInt)) isOk = False isDone = True continue except ... as me: print("Failed on func({0}) with {1}".format(aInt, me)) isOk = False isDone = True continue else: pass isOk = isOk and (res == factorial(aInt)) writeRes("{0}_1 : {1:d} of 1".format(__globString, isOk)) return isOk