Example #1
0
    def submit_solution(self, problemid, language, code):
        # save the code into a file name like <problemid>.<anything>.ext
        # use tempfile here to create a temporary file
        # mkstemp returns (fd, fname) where fd is a filedescriptor
        _, fname = tempfile.mkstemp(prefix="%s." % problemid,
                                    suffix=".%s" % language)
        print "MADE TEMP FILE", fname
        with open(fname, "w") as f:
            f.write(code)

        # call leetcode CLI with leetcode submit <filename> and retrieve output
        try:
            output = run_leetcode_command(["submit", fname])
            if output.find("Accepted") != -1:
                print "ACCEPTED", output
                submission = Question.create(leetcode_id=problemid,
                                             submission=code,
                                             status=True)
            elif output.find("Wrong Answer") != -1:
                print "WRONG ANSWER", output
                submission = Question.create(leetcode_id=problemid,
                                             submission=code,
                                             status=False)
            else:
                print "UNKNOWN", output
                submission = Question.create(leetcode_id=problemid,
                                             submission=code,
                                             status=False)
            return output
        finally:
            print "REMOVING", fname
            os.remove(fname)