# redirect stdout (https://stackoverflow.com/questions/1218933/can-i-redirect-the-stdout-in-python-into-some-sort-of-string-buffer) from cStringIO import StringIO import sys import challenge # funcname, [[args]], solstring challenges=[["hello",[[]],"Hello world\n"], ["hifive",[[3],[7],[11]],"Hi!\nHi!\nHi!\nFive\nHi!\nHi!\nHi!\nHi!\nHi!\nHi!\nHi!\nFive\nHi!\nHi!\nHi!\nHi!\nHi!\nHi!\nHi!\nHi!\nHi!\nHi!\nHi!\nFive\n"], ["rampe",[[1],[5],[8]],"#\n#\n##\n###\n####\n#####\n#\n##\n###\n####\n#####\n######\n#######\n########\n"], ["tree",[[1],[5],[8]],"^\n|\n ^\n ^^^\n ^^^^^\n ^^^^^^^\n^^^^^^^^^\n |\n ^\n ^^^\n ^^^^^\n ^^^^^^^\n ^^^^^^^^^\n ^^^^^^^^^^^\n ^^^^^^^^^^^^^\n^^^^^^^^^^^^^^^\n |\n"], ["teiler",[[1],[5],[8],[720],[101],[123456]],"1\n1\n5\n1\n2\n4\n8\n1\n2\n3\n4\n5\n6\n8\n9\n10\n12\n15\n16\n18\n20\n24\n30\n36\n40\n45\n48\n60\n72\n80\n90\n120\n144\n180\n240\n360\n720\n1\n101\n1\n2\n3\n4\n6\n8\n12\n16\n24\n32\n48\n64\n96\n192\n643\n1286\n1929\n2572\n3858\n5144\n7716\n10288\n15432\n20576\n30864\n41152\n61728\n123456\n"], ["grid",[[1,1],[5,1],[1,5],[2,2]],"+---+\n| |\n+---+\n+---+---+---+---+---+\n| | | | | |\n+---+---+---+---+---+\n+---+\n| |\n+---+\n| |\n+---+\n| |\n+---+\n| |\n+---+\n| |\n+---+\n+---+---+\n| | |\n+---+---+\n| | |\n+---+---+\n"], ["prim",[[0],[1],[2],[3],[4],[5],[6],[57],[101],[123456],[99991],[10000001]],"0 nicht prim\n1 nicht prim\n2 prim\n3 prim\n4 nicht prim\n5 prim\n6 nicht prim\n57 nicht prim\n101 prim\n123456 nicht prim\n99991 prim\n10000001 nicht prim\n"]] results = [" n/a " for c in challenges] counters = [0,0,0,0] for i in range(len(challenges)): old_stdout = sys.stdout sys.stdout = mystdout = StringIO() cfunc = None try: cfunc = getattr(challenge, challenges[i][0]) except: counters[3]+=1 if cfunc!=None: allOk = True for args in challenges[i][1]: try: cfunc(*args) except: allOk = False sys.stdout = old_stdout if allOk: result = mystdout.getvalue().replace("\r\n","\n") #print(repr(result)) if result==challenges[i][2]: results[i] = " OK " counters[0]+=1 else: results[i] = "FAIL " counters[1]+=1 else: results[i] = "CRASH" counters[2]+=1 print("[%s] Challenge %s" % (results[i], challenges[i][0])) print("\n\nSummary\n==========\n\nOK : %d\nFAIL : %d\nCRASH: %d\nn/a : %d" % (counters[0], counters[1], counters[2], counters[3]))