Question
#csc220a2.py def flatten(data): return () # csc220a2_tester.py Tester for Assignment 2. ############################################################### # Auto Grader (See A Generic Python Auto Grader) # #
#csc220a2.py
def flatten(data):
return ()
# csc220a2_tester.py
""" Tester for Assignment 2. """
############################################################### # Auto Grader (See "A Generic Python Auto Grader") # # File: csc220a2_tester.py # # Author: Albert Chan # # Affiliation: Fayetteville State University # # Department of Mathematics and Computer Science # # Copyright (c) 2019 # # License: GPL 2.0 # # Exception: compiled version of the tester and test data # # files can be distributed to students without # # source or showing the license # ###############################################################
import sys
MODULE = 'csc220a2' FUNCTION = 'flatten' MAXSCORE = 15 TESTDATA = 'csc220a2_testdata'
def load_function(module, file_path, function='main'): """ Load a function from a module. Input: module - the module file_path - the path to the module function - the name of the function to load Output: the loaded function """ sys.path.insert(0, file_path) try: return getattr(__import__(module), function) finally: sys.path.pop(0)
def message(record, msg, verbose): """ Display a message if wanted. Input: record - record mechanism msg - the message verbose - control whether the messsage will be display Output: None """ if verbose: record(msg)
def convert_to_tuple(value): """ Convert value to tuple. Input: value - the value Output: the value converted to tuple """ return value if isinstance(value, tuple) else (value,)
def check_exceptions(testcase, resulting_exception, verbose, record): """ Check for exception. Input: testcase - the testcase resulting_exception - the resulting exception verbose - True if wanted verbose message record - recording mechanism Output: True if passed, False otherwise """ # pylint: disable = C0301 tc_id = testcase[0] expected_exception = None if len(testcase) > 3: expected_exception = testcase[3]
if expected_exception is None: if resulting_exception is None: return True message(record, f'TC {tc_id} failed - exception {type(resulting_exception).__name__} caught', verbose) return False
if resulting_exception is None: message(record, f'TC {tc_id} failed - expecting an exception, none raised', verbose) return False
if isinstance(resulting_exception, expected_exception): return True message(record, f'TC {tc_id} failed - unexpected exception {type(resulting_exception).__name__} caught', verbose) return False
def check_return_value(testcase, actual_result, verbose, record): """ Check for result. Input: testcase - the testcase actual_result - the result verbose - True if wanted verbose message record - recording mechanism Output: True if passed, False otherwise """ # pylint: disable = C0301, R0911 tc_id = testcase[0] expected_result = testcase[2] if expected_result is None: if not actual_result is None: message(record, f'TC {tc_id} failed - not expecting a return value, got {repr(actual_result)} instead', verbose) return False return True
if actual_result is None: message(record, f'TC {tc_id} failed - expecting {repr(expected_result)}, got nothing', verbose) return False
actual_result = convert_to_tuple(actual_result) expected_result = convert_to_tuple(expected_result) if len(actual_result) == len(expected_result) == 1: if actual_result[0] == expected_result[0]: return True message(record, f'TC {tc_id} failed - expecting {repr(expected_result [0])}, got {repr(actual_result [0])} instead', verbose) return False
actual_length = len(actual_result) expected_length = len(expected_result) if actual_length == expected_length: for index in range(actual_length): if not actual_result[index] == expected_result[index]: message(record, f'TC {tc_id} failed - expecting {repr(expected_result [index])} at position {index}, got {repr(actual_result [index])} instead', verbose) return False return True message(record, f'TC {tc_id} failed - expecting a sequence of {len(expected_result)} values, got a sequence of {len(actual_result)} instead', verbose) return False
def run_single_test(function, testcase, verbose=False, record=print): """ Run a single testcase. Input: function - the function to call testcase - the testcase verbose - True if wanted verbose message (default to False) record - recording mechanism (default to print) Output: True if passed, False otherwise """ test_input = convert_to_tuple(testcase[1]) result = None resulting_exception = None
try: result = function(*test_input) except Exception as err: # pylint: disable = W0703 resulting_exception = err
if check_exceptions(testcase, resulting_exception, verbose, record): if check_return_value(testcase, result, verbose, record): message(record, 'TC %d passed' % testcase[0], verbose) return True return False return False
def run_all_tests(test_suite, module=MODULE, path='.', record=print): """ Run all testcase. Input: test_suite - the testcases module - the module (default to MODULE defined above) path - the path of the module (default to current folder) record - recording mechanism (default to print) Output: score of running the testcases """ # pylint: disable = C0301 try: function = load_function(module, path, FUNCTION) except Exception as _: # pylint: disable = W0703 record('File loading error - testing not executed.') return 0 passed = [] failed = [] number_of_testcases = len(test_suite) for testcase in test_suite: success = run_single_test(function, testcase, False, record) if success: passed.append(testcase[0]) else: failed.append(testcase[0])
number_of_passed_testcases = len(passed) test_score = float(MAXSCORE) * number_of_passed_testcases / number_of_testcases record(f'Passed: {passed} - total {len(passed)}.') record(f'Failed: {failed} - total {len(failed)}.') record(f' {number_of_passed_testcases} of {number_of_testcases} testcases passed. Score = {test_score:.2f} of {MAXSCORE:.2f}.') return test_score
def run_testcase(tc_id, module=MODULE, path='.', record=print): """ Run a single testcase selected by testcase id. Input: tc_id - the testcase id module - the module (default to MODULE defined above) path - the path of the module (default to current folder) record - recording mechanism (default to print) Output: None """ try: function = load_function(module, path, FUNCTION) except Exception as _: # pylint: disable = W0703 record('File loading error - testing not executed.') return for testcase in TESTS: if testcase[0] == tc_id: run_single_test(function, testcase, True, print)
if __name__ == '__main__': TESTS = load_function(TESTDATA, '.', 'TESTS') run_all_tests(TESTS)
# csc220a2_testdata.py
""" Test data file. """
############################################################### # Auto Grader (See "A Generic Python Auto Grader") # # File: csc220a2_testdata.py # # Author: Albert Chan # # Affiliation: Fayetteville State University # # Department of Mathematics and Computer Science # # Copyright (c) 2019 # # License: GPL 2.0 # # Exception: compiled version of the tester and test data # # files can be distributed to students without # # source or showing the license # ###############################################################
TESTS = [(1, ((101, 969, [261, (('yfhyqwojuonagzw', [574, 712, ['tmgmdgqlwqroelgicj', 'plwhnlkwocxbszaqska', 838]], ('fqixc', (562, 'qxbywemmlf', 'hqqksykjvmo', 384), ('ygjnpijc', 183, 'atmwao'), 155, ['kuivngyhrwtavx', 'pkgkmxaymoac', 'oudyfnviprffinttaa', 'okyoctpjypdyfoqb']), (['donouivwuxzuqtenb', 'kzlirdchbmws', 'wpjjuz', 819, 293], 'rhrhrvk', 'kbzihnzyaaukaz', [977, 'wtocnhb', 0]), [['ubcnicutqonabug', 747, 'xyinolgaphjlfbmi', 264, 'fimwmazhekmqeyfpl'], 'rnpxmzncjdiepibqrc', 485]), 587, 'woxcmsylgrel', ['ssiqgcpp', (401, 511, 862), (810, (106, 875, 608, 184, 779), [503, 'hmrri', 'kfhrpefgxrjmx'], ['szxrqzbnryikxx', 482, 113, 'gdjmvpqdzqeks', 77], ['msqaqhxdyocxd', 145, 'zsphyvylnfaim', 'slqkdst']), 560, (642, 52, 185, 133, 641)], 446), 480, 'pwqyjiafmivuukf', ['giktxjdkbqwidij', 203, 'hdqwyoqcuuemrnzlezj']], 837),), (101, 969, 261, 'yfhyqwojuonagzw', 574, 712, 'tmgmdgqlwqroelgicj', 'plwhnlkwocxbszaqska', 838, 'fqixc', 562, 'qxbywemmlf', 'hqqksykjvmo', 384, 'ygjnpijc', 183, 'atmwao', 155, 'kuivngyhrwtavx', 'pkgkmxaymoac', 'oudyfnviprffinttaa', 'okyoctpjypdyfoqb', 'donouivwuxzuqtenb', 'kzlirdchbmws', 'wpjjuz', 819, 293, 'rhrhrvk', 'kbzihnzyaaukaz', 977, 'wtocnhb', 0, 'ubcnicutqonabug', 747, 'xyinolgaphjlfbmi', 264, 'fimwmazhekmqeyfpl', 'rnpxmzncjdiepibqrc', 485, 587, 'woxcmsylgrel', 'ssiqgcpp', 401, 511, 862, 810, 106, 875, 608, 184, 779, 503, 'hmrri', 'kfhrpefgxrjmx', 'szxrqzbnryikxx', 482, 113, 'gdjmvpqdzqeks', 77, 'msqaqhxdyocxd', 145, 'zsphyvylnfaim', 'slqkdst', 560, 642, 52, 185, 133, 641, 446, 480, 'pwqyjiafmivuukf', 'giktxjdkbqwidij', 203, 'hdqwyoqcuuemrnzlezj', 837), None), (2, (('bcnazfykzhd', 589, ('ulzsxluszdlvep', [661, 950, [496, 447, 'ddizrzryupmxplcbr']], 'eokzc', [885, (904, ('ovuhkxudkifsoocb', 302, ('iulairqellxlpectj', 501, 'milcawktldxzc', 617, 'oiftvfpqkbp')), 129, [(879, 'clnjfrgtfgq', 835, 953), 'cbxtc', ('icwogoo', 447, 'lrheixpjslevrmpv', 'qisip')], 541), 'tibgturlw']), ('sdgwfjbmfgylhkqmkpl', (('cfxxec', [('rivthbqvuue', 'zswhcjsou', 'kmwufgsmdlwhoeh', 'pmsleynhpnq', 'jburjcrzxq'), ['ehgmum', 392, 23, 678], (116, 896, 'ihxagntgt'), 'qaozksbtgznvrjeduud', [407, 'uasguiwwarnzvpja', 354]], (['mnblmpc', 619, 'fnnsq', 542, 299], ('cqmjwevipygck', 604, 141), 'odfmugcrolzraad', 'xdhrurjfcoemmmwbk', 832), ['ghmjgqj', ('irqfvbm', 198, 'fgssh', 'njsjffxnmeltmzdju'), [456, 382, 'hqbguf']], ([576, 'drzjzxmph', 20, 'mjikqhsgdstdgoejye', 'cdmyssjsbsgafvkej'], 'xshuzcmklhkwjgdsav', 'boibtyjwsizhmaclnb')), 960, [['xgofk', 'imfkpxbvvhpqux', (566, 'ebiggaoqriyzbkb', 'phkdwrbonkjm', 463)], 'asilx', 796, 555]), [898, [(36, ('mesoruepgvuw', 888, 'cpabo'), 'ybaylddgehjm'), 194, 'lowszaxxampaweernf', [['qirzsm', 536, 'oowxxptidwnlrq', 'orjnpjopitwmrhooomj'], [624, 'eylfqdiorn', 687], 587], 375], 'nmzfljf', ([(292, 'mqlngrwxz', 'cguxtoomnecnon', 'fmivmzzkqbifqiji', 632), 218, (434, 'htgzeu', 'zsknn'), 508], 'gvllkuiy', 'rxzhtz', [['mqhvtrjozssv', 414, 'gqfinoawezcgcqpon'], 426, (609, 'ehxgopbdqkarefzy', 'omslmpkaurlifaadste', 'eskevntpnzx', 'kdjsshut'), ('jvkazbejdu', 'waqzwshusxbgyo', 442, 729)])])),), ('bcnazfykzhd', 589, 'ulzsxluszdlvep', 661, 950, 496, 447, 'ddizrzryupmxplcbr', 'eokzc', 885, 904, 'ovuhkxudkifsoocb', 302, 'iulairqellxlpectj', 501, 'milcawktldxzc', 617, 'oiftvfpqkbp', 129, 879, 'clnjfrgtfgq', 835, 953, 'cbxtc', 'icwogoo', 447, 'lrheixpjslevrmpv', 'qisip', 541, 'tibgturlw', 'sdgwfjbmfgylhkqmkpl', 'cfxxec', 'rivthbqvuue', 'zswhcjsou', 'kmwufgsmdlwhoeh', 'pmsleynhpnq', 'jburjcrzxq', 'ehgmum', 392, 23, 678, 116, 896, 'ihxagntgt', 'qaozksbtgznvrjeduud', 407, 'uasguiwwarnzvpja', 354, 'mnblmpc', 619, 'fnnsq', 542, 299, 'cqmjwevipygck', 604, 141, 'odfmugcrolzraad', 'xdhrurjfcoemmmwbk', 832, 'ghmjgqj', 'irqfvbm', 198, 'fgssh', 'njsjffxnmeltmzdju', 456, 382, 'hqbguf', 576, 'drzjzxmph', 20, 'mjikqhsgdstdgoejye', 'cdmyssjsbsgafvkej', 'xshuzcmklhkwjgdsav', 'boibtyjwsizhmaclnb', 960, 'xgofk', 'imfkpxbvvhpqux', 566, 'ebiggaoqriyzbkb', 'phkdwrbonkjm', 463, 'asilx', 796, 555, 898, 36, 'mesoruepgvuw', 888, 'cpabo', 'ybaylddgehjm', 194, 'lowszaxxampaweernf', 'qirzsm', 536, 'oowxxptidwnlrq', 'orjnpjopitwmrhooomj', 624, 'eylfqdiorn', 687, 587, 375, 'nmzfljf', 292, 'mqlngrwxz', 'cguxtoomnecnon', 'fmivmzzkqbifqiji', 632, 218, 434, 'htgzeu', 'zsknn', 508, 'gvllkuiy', 'rxzhtz', 'mqhvtrjozssv', 414, 'gqfinoawezcgcqpon', 426, 609, 'ehxgopbdqkarefzy', 'omslmpkaurlifaadste', 'eskevntpnzx', 'kdjsshut', 'jvkazbejdu', 'waqzwshusxbgyo', 442, 729), None), (3, ([454, ((136, 603, ([('ocaiacmncnrfkuajf', 175, 779, 'cjrgpfdvjdrfkeol'), 'coquoczwmnljougdi', ['cellxyzfovzacgz', 'movtvapcb', 'igazmxdlmjtynl', 429], ['zasqkjmismjccbz', 'zpponuhiczlyjxk', 884, 936, 'hyskokumjpkerb']], 'xtmirqyetgesjs', 'mgxxeyjsbgqabkiz', 'cwoecwpjjiv', (817, 590, ('aihfahgwa', 970, 'bvyclvsloize', 'fozhmcvvolag'), (891, 'etwhu', 'mjboje', 'lylfiodg', 839), 'ptcxorufzaxbabiben'))), 'zpfmuxrbz', 429), (187, (459, 214, 'qutqbvxetametkw', 'ybbwxnqavgwariclbl'), ['kxfffspf', 'wwnrwjtswfsnczlu', 810], ['razutm', 437, 856, [[['tmzxzfebrnwx', 'ytswzonazvxgbgk', 'yfcfqkevpbizghr'], ('yvroaspexxfil', 514, 406), (878, 856, 905, 'panrfjrekl')], 644, ([829, 'rlgqtwx', 602, 390], (898, 97, 'toywkjsbmlseqt', 860, 920), 140)]], 'ccyujfuevcqcskdege'), 'iiffbkttxfbmuabsk'],), (454, 136, 603, 'ocaiacmncnrfkuajf', 175, 779, 'cjrgpfdvjdrfkeol', 'coquoczwmnljougdi', 'cellxyzfovzacgz', 'movtvapcb', 'igazmxdlmjtynl', 429, 'zasqkjmismjccbz', 'zpponuhiczlyjxk', 884, 936, 'hyskokumjpkerb', 'xtmirqyetgesjs', 'mgxxeyjsbgqabkiz', 'cwoecwpjjiv', 817, 590, 'aihfahgwa', 970, 'bvyclvsloize', 'fozhmcvvolag', 891, 'etwhu', 'mjboje', 'lylfiodg', 839, 'ptcxorufzaxbabiben', 'zpfmuxrbz', 429, 187, 459, 214, 'qutqbvxetametkw', 'ybbwxnqavgwariclbl', 'kxfffspf', 'wwnrwjtswfsnczlu', 810, 'razutm', 437, 856, 'tmzxzfebrnwx', 'ytswzonazvxgbgk', 'yfcfqkevpbizghr', 'yvroaspexxfil', 514, 406, 878, 856, 905, 'panrfjrekl', 644, 829, 'rlgqtwx', 602, 390, 898, 97, 'toywkjsbmlseqt', 860, 920, 140, 'ccyujfuevcqcskdege', 'iiffbkttxfbmuabsk'), None), (4, (((([[307, ['qmrkpivc', 248, 'gbhgzbgi', 535, 'aslzm'], (474, 'qqrdyyevswez', 'cetrzvppcpqy'), ['xeorvdpjgoceltma', 'pfdgeztoetvbfjrkkpu', 'xbfftmnskre', 149], ['lidgklbewzheqpgzigd', 125, 'jvilvklvtetdb']], 'aydnxhobz', ['nbonp', 'hjwfgnx', 895], 'dznziic', 'rezcvmmfsvtjs'], 897, [[('wuzsdlci', 953, 212, 193), ['kimtcnshrk', 'lchzzhjshcsbizzlzff', 214, 'wmprh'], 'bfycysceuxikrcdt'], 'jchskkxzmyvlffuojm', [182, 'mwwkndcnsqajrdqimoa', ['oxgjbmr', 451, 501, 'deingpoynqlzekbsqen'], (97, 'lgpehupqzounk', 'jfhgcuostbfelqn'), 'ewmtwrhkmebdnuqlsd'], 'odnhzyrocn']), 928, ([(('jlbuij', 'mbjefdwxcg', 209), (686, 'hxbyr', 'ivwskvzfhiyjyop', 'aqobzpmrdu'), [467, 782, 'sjwnk', 'whuzulxgqmya', 609]), 162, 'gphgicg', 444], ['ztqnsktbejqe', 'annpeonrx', 707], 916), 247, 572), ([((['sufqyodbklza', 'mptprptehjb', 331], ['xkjcwnqhjktny', 364, 39, 737, 554], ['bjxlcvvzgmbcqbimyvy', 852, 427]), [509, 563, ['peaccpkghvurauc', 928, 'lgnmkzcjkqwlcp', 188, 449], [424, 'zqjpoqexkbwhhaaem', 776]], ([272, 'mvzttdmqal', 'rotmnwmbrhmpbpvd', 'qiquabwmu', 618], 'vhiyqrlppathauw', [18, 524, 161], 944, [777, 30, 644, 'fmprqixsbkowzt', 865]), (['qnltvymlrgliiuwwvc', 577, 375, 240], ('dnoizgrjcancxz', 82, 'qyoebk', 'vkjeauvnxxjlx'), ('pgeznv', 159, 40), [364, 'wdfujkozjdzidm', 'imktqrohzlzpdve', 551, 531], 515)), [361, 669, [81, 708, 33]], ([304, 887, 366, 935], ('rltrykrbgzknpzzoym', (999, 784, 'czjvvsrnkal'), 'eiitvpgy'), 'bkuiikemzcx')], [424, [[['bxsvcpcbtwjcalcyuw', 'hfhfoaug', 353, 'fuoerizozhxchyl', 'hphcr'], 12, (957, 914, 975), [529, 697, 'xyamgnvrylf', 880]], 'ebxhirlw', 'xxvhkmp'], ([['rxkth', 443, 'nspdxzrrmjbdif', 'wpkvcdnx', 'rlevm'], (155, 'walfwzmcwe', 'kfostusajchditbq', 111), ('fjpolztsnsghv', 219, 'iybowkcfhwdkhrs'), 679, 'ffnbwkcojjjzqza'], ['gmohuspcmswlbntcc', 'wswxaxkrwfsltpsvq', (358, 621, 484, 'omzjlqhoeaqamh'), ('jltdnmvyuqe', 'mkyryqcixizijwfwnp', 600), ('nkduwtejcsqwcwfxjsn', 143, 518)], 'gimjoqtuwpadvpdwzxr')], 'qwqimr'), ['zbnqrmulgo', [[[996, [600, 'vpduultfikfrzrsn', 'pkdrmjtaqtrzha'], ['ilpgfaphtce', 'eiecjky', 407]], 'atmkswk', 'yflprwcpnqvoysnb', 'ilkanao', ('xpgnzeiomlknppqic', 459, [516, 'jzjtcjiwlmksdyq', 'frrsrkihg', 361], ['zejkkgziwhkhbslue', 687, 'wemxenyn', 'xjqgs'])], 280, 495, [[554, ('lvfpwrppopicxuo', 'mlyzxxswsdrtczaa', 'conxdevpkk'), 626, ['qdfttxqdmpd', 647, 'txhjtinvtizw']], 236, ((380, 'gauufvpwlkbxdsfgdvw', 'wsmxyvchgke', 'tmylbuzcaigjzg', 871), ('ymzyaqudqndxgflx', 548, 548), 857)]], 'nhbifiyltm', [868, [['ofndhfq', 'tatniszjexzarhd', [939, 562, 736, 903, 369]], (['vdjotjenpepzeorfto', 632, 615, 'fjlpby', 'wmvmyiswyewratv'], 934, ('wngyfwts', 'nnilpphac', 'xddvntlzxyjnojv', 644, 865)), [71, ('jdvchmkeqd', 'fpgqjmoukbkxup', 11, 'agwfstydnxubtk', 315), [420, 'kfkwfhgqpkjyq', 'ekptnvysxgrkjwi', 'jyvqxrvmghjytgqfh', 750]], [[80, 363, 962], ('yhxib', 470, 388, 'xgosvuljvdezijncjy'), 'ungvxqvoaysyubxv']], 914, 979]], 258, 'xpmrapfxtfnmtecbqkl'),), (307, 'qmrkpivc', 248, 'gbhgzbgi', 535, 'aslzm', 474, 'qqrdyyevswez', 'cetrzvppcpqy', 'xeorvdpjgoceltma', 'pfdgeztoetvbfjrkkpu', 'xbfftmnskre', 149, 'lidgklbewzheqpgzigd', 125, 'jvilvklvtetdb', 'aydnxhobz', 'nbonp', 'hjwfgnx', 895, 'dznziic', 'rezcvmmfsvtjs', 897, 'wuzsdlci', 953, 212, 193, 'kimtcnshrk', 'lchzzhjshcsbizzlzff', 214, 'wmprh', 'bfycysceuxikrcdt', 'jchskkxzmyvlffuojm', 182, 'mwwkndcnsqajrdqimoa', 'oxgjbmr', 451, 501, 'deingpoynqlzekbsqen', 97, 'lgpehupqzounk', 'jfhgcuostbfelqn', 'ewmtwrhkmebdnuqlsd', 'odnhzyrocn', 928, 'jlbuij', 'mbjefdwxcg', 209, 686, 'hxbyr', 'ivwskvzfhiyjyop', 'aqobzpmrdu', 467, 782, 'sjwnk', 'whuzulxgqmya', 609, 162, 'gphgicg', 444, 'ztqnsktbejqe', 'annpeonrx', 707, 916, 247, 572, 'sufqyodbklza', 'mptprptehjb', 331, 'xkjcwnqhjktny', 364, 39, 737, 554, 'bjxlcvvzgmbcqbimyvy', 852, 427, 509, 563, 'peaccpkghvurauc', 928, 'lgnmkzcjkqwlcp', 188, 449, 424, 'zqjpoqexkbwhhaaem', 776, 272, 'mvzttdmqal', 'rotmnwmbrhmpbpvd', 'qiquabwmu', 618, 'vhiyqrlppathauw', 18, 524, 161, 944, 777, 30, 644, 'fmprqixsbkowzt', 865, 'qnltvymlrgliiuwwvc', 577, 375, 240, 'dnoizgrjcancxz', 82, 'qyoebk', 'vkjeauvnxxjlx', 'pgeznv', 159, 40, 364, 'wdfujkozjdzidm', 'imktqrohzlzpdve', 551, 531, 515, 361, 669, 81, 708, 33, 304, 887, 366, 935, 'rltrykrbgzknpzzoym', 999, 784, 'czjvvsrnkal', 'eiitvpgy', 'bkuiikemzcx', 424, 'bxsvcpcbtwjcalcyuw', 'hfhfoaug', 353, 'fuoerizozhxchyl', 'hphcr', 12, 957, 914, 975, 529, 697, 'xyamgnvrylf', 880, 'ebxhirlw', 'xxvhkmp', 'rxkth', 443, 'nspdxzrrmjbdif', 'wpkvcdnx', 'rlevm', 155, 'walfwzmcwe', 'kfostusajchditbq', 111, 'fjpolztsnsghv', 219, 'iybowkcfhwdkhrs', 679, 'ffnbwkcojjjzqza', 'gmohuspcmswlbntcc', 'wswxaxkrwfsltpsvq', 358, 621, 484, 'omzjlqhoeaqamh', 'jltdnmvyuqe', 'mkyryqcixizijwfwnp', 600, 'nkduwtejcsqwcwfxjsn', 143, 518, 'gimjoqtuwpadvpdwzxr', 'qwqimr', 'zbnqrmulgo', 996, 600, 'vpduultfikfrzrsn', 'pkdrmjtaqtrzha', 'ilpgfaphtce', 'eiecjky', 407, 'atmkswk', 'yflprwcpnqvoysnb', 'ilkanao', 'xpgnzeiomlknppqic', 459, 516, 'jzjtcjiwlmksdyq', 'frrsrkihg', 361, 'zejkkgziwhkhbslue', 687, 'wemxenyn', 'xjqgs', 280, 495, 554, 'lvfpwrppopicxuo', 'mlyzxxswsdrtczaa', 'conxdevpkk', 626, 'qdfttxqdmpd', 647, 'txhjtinvtizw', 236, 380, 'gauufvpwlkbxdsfgdvw', 'wsmxyvchgke', 'tmylbuzcaigjzg', 871, 'ymzyaqudqndxgflx', 548, 548, 857, 'nhbifiyltm', 868, 'ofndhfq', 'tatniszjexzarhd', 939, 562, 736, 903, 369, 'vdjotjenpepzeorfto', 632, 615, 'fjlpby', 'wmvmyiswyewratv', 934, 'wngyfwts', 'nnilpphac', 'xddvntlzxyjnojv', 644, 865, 71, 'jdvchmkeqd', 'fpgqjmoukbkxup', 11, 'agwfstydnxubtk', 315, 420, 'kfkwfhgqpkjyq', 'ekptnvysxgrkjwi', 'jyvqxrvmghjytgqfh', 750, 80, 363, 962, 'yhxib', 470, 388, 'xgosvuljvdezijncjy', 'ungvxqvoaysyubxv', 914, 979, 258, 'xpmrapfxtfnmtecbqkl'), None)]
In this assignment, you are going to implement a recursive Python function to flatten a list or tuple. Unlike other languages, Python allows arbitrary type of data in a list and/or tuple, including other lists/tuples. Because of this, the users can create deeply nested lists and/or tuples. This can be useful in some situation (e.g., we can use a nested list to represent a tree, which is a data structure that we will study in Chapter 8). However, it can also make listttuple handling a little more complicated. There is situation that we may want to flatten a list/tuple to simplify operation. One such example is that to count how many unique elements in the list/tuple, no matter how deep it is nested inside. The flatten function takes a list or a tuple as its parameter, it returns the flattened parameter as a regular (unnested) Python tuple. All elements in the returned tuple are in the same order as they appear in the original list/tuple. For example: Please note that in the last example, the tuple is not changed since it is not nested. Download the three files "csc228a2.py", "csc228a2_tester.py", and "csc228a2_testdata.py" from Canvas and save them in the same folder. The files "csc228a2_tester.py" and "csc228a2_testdata.py" are the tester program and the test data file, respectively. Do not modify these two files. The first file, "csc228a2.py", is the file you will work on. Note that you cannot rename this file and you cannot change the name of the function (otherwise the tester will not be able to pick up your implementation). The file "csc228a2.py" contains a dummy implementation of the function that always returns a hard-coded (and probably) incorrect value: The tester program will use the test data to test your implementation. It will run a total of 100 test cases. If you have not made any change to the function, it should produce the following output: However, if you implement the function correctly, the output should look like this: Notes: 1. You can assume that the input parameter is always a list or a tuple, there is no need to check for that. 2. Please note that Python strings behave very much like lists and tuples. However, your function should never try to flatten a Python string. 3. Your program will be tested with similar but different data. The testing results will earn you up to 15 points - this is your execution score. 4. You earn up to 5 points for styling your program to adhere to the PEP-8 standard. See later for details. 5. There cannot be any import statement in your program - otherwise, 20% of your execution score will be deducted. 6. There can be only one single function (the "flatten" function) defined in "csc228a2.py". No other helper function(s) is/are allowed. Otherwise, 20% of your execution score will be deducted. 7. You must have meaningful documentation (in form of Python comments) to explain what is going on in your implementation. Otherwise, 20% of your execution score will be deducted. 8. Your code cannot print anything to the screen (both within the function and outside the function) otherwise, 20% of your execution score will be deducted. However, commented printed functions (probably for debugging purposes) are allowed. 9. Your function must be a recursive function - otherwise, 90% of your total assignment score will be deducted. 10. Your implementation should be compatible with Python 3.x. 11. Debugging tips: if your code contains syntax errors, the tester will fail to load for no apparent reason. This will generate a result of failing all test cases. Therefore, you should try to just load your code into Python to make sure it does not have any syntax error preventing the tester to load. 12. The tester contains a function "run_testcase", this function takes a testcase number and run that single testcase. It will produce a more verbose output to help you debug your program. \begin{tabular}{|l|} \hline> run_testcase(1) \\ TC 1 failed - expecting a sequence of 76 values, got a sequence of \\ instead \\ >> run_testcase(50) \\ TC 50 passed \\ \hline \end{tabular} Please note that this function is not executed by default when you load the tester. So, you may need to somehow activate Python in interactive mode to use this function. If you are using Python IDLE, you are automatically put in interactive mode after loading (and executing) the tester. However, this is not the default if you are invoking and running Python from the command line. To allow you to use the function if you load the tester from the command line, you must use the interactive flag when invoking Python: After you are done, you can use the exit() function to abort the Python session. About Programming Style Although writing correct and fast code is the main goal of programming, equally important is writing code that is easy to read and understand. To achieve this second goal, programmers around the world have established conventions and standards. For Python programs, these conventions and standards are documented in PEP (Python Enhancement Proposal) 8. You can read about PEP-8 at https://www.python.org/dev/peps/pep-0008. PEP-8 defines the conventions and standards that are agreed-upon by most Python programmers around the world. By adhering to PEP-8, you can ensure that your programs are (most likely) well-structured and easily understood by others. However, checking whether a program adheres to PEP-8 can be difficult. It would be convenient if we had a program that could check for us and point out anything that does not conform to PEP-8. Luckily, we do have such a program. pylint is an open-source program that does just that. By default, it checks whether your program conforms to PEP-8. pylint has been installed on most computers in campus laboratories and classrooms. You can also install it on your own computer using the following command (you must have Python installed on your computer to install pylint): You can run pylint from a Windows command prompt: 1. Open a Windows command prompt: It (this is the Windows logo at the lower left corner of the screen) | Windows System | Command Prompt 2. Navigate to the folder that contains your program: cd - If you do not know the path to your program, you can find it using File Explorer. Once you find it, click on the address bar and it will show the path. 3. Run the command: pylint For each instance of code that does not conform with the PEP-8 guidelines, pylint will display a message telling you what the problem is. It is, of course, your responsibility to fix the problem, and to re-run the check. The message will look something like this: program.py:39:14: C0326: No space allowed before bracket This means that in the file program.py, on line at character 14, there is an incompliance (C0326 is an internal identifier for pylint): PEP-8 says there should not be space character(s) before a bracket. At the end of the output, you will see a score on how compliant your program is. The score starts from 10.0 and each incompliance will result in a score deduction. Therefore, you will never have a score greater than 10 , but it is possible to have a negative score. Note that pylint assumes that your Python program is a valid Python program (that is, it is free of syntax errors). If any syntax error is detected during the process, it will NOT output a score. You can earn up to 5 points for conforming to the PEP-8 guidelines. This will be shown as the style score. Your style score will be calculated as follows (assuming pylint_score is the score provided by pylint): \[ \text { style_score }=\theta \text { if pylint_score }Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started