def home(): x = random.randint(-100, 100) y = random.randint(-100, 100) statement_str = generate_statement_string(2) tree = BinTree.build_tree(statement_str) statement_result = BinTree.solve_tree(tree, x, y) form = forms.TrueOrFalseForm(flask.request.form) if form.validate_on_submit(): if not flask_login.current_user.is_anonymous: if form.choice.data == form.hidden.data: with db.create_connection() as connection, connection.cursor() as cursor: sql = "UPDATE users SET score = score + 2 WHERE id = %s" cursor.execute(sql, flask_login.current_user.get_id()) connection.commit() flask.flash('Correct! +2 points!', 'success') else: with db.create_connection() as connection, connection.cursor() as cursor: sql = "UPDATE users SET score = score - 1 WHERE id = %s" cursor.execute(sql, flask_login.current_user.primary_id) connection.commit() flask.flash('Incorrect! -1 points!', 'error') else: if form.choice.data == form.hidden.data: flask.flash('Correct!', 'success') else: flask.flash('Incorrect!', 'error') elif form.errors: for item in form.errors.items(): flask.flash(item, 'error') return flask.render_template('home.html', x_value=str(x), y_value=str(y), statement=statement_str, result=str(statement_result), form=form)
def toTree(head: ListNode, tail: TreeNode): if head == tail: return None slow, fast = head, head while fast != tail and fast.next != tail: fast = fast.next.next slow = slow.next root = TreeNode(slow.val) root.left = toTree(head, slow) root.right = toTree(slow.next, tail) return root
def tester(): form = forms.StatementForm() if form.validate_on_submit(): try: solution = BinTree.solve_tree(BinTree.build_tree(form.statement.data), 0, 0) if type(solution) is not bool: raise ValueError flask.flash('Given statement is {0}, solution is {1}'.format(form.statement.data, str(solution))) except (ValueError, IndexError): flask.flash('Invalid statement!', 'error') elif form.errors: for item in form.errors.items(): flask.flash(item, 'error') return flask.render_template('tester.html', form=form)
def home(): x = random.randint(-100, 100) y = random.randint(-100, 100) if flask_login.current_user.is_anonymous: statement_str = generate_statement_string(2) else: statement_str = generate_statement_string( flask_login.current_user.difficulty) tree = BinTree.build_tree(statement_str) statement_result = BinTree.solve_tree(tree, x, y) form = forms.TrueOrFalseForm(flask.request.form) if form.validate_on_submit(): if not flask_login.current_user.is_anonymous: difficulty = flask_login.current_user.difficulty if form.choice.data == form.hidden.data: with db.create_connection() as connection, connection.cursor( ) as cursor: sql = "UPDATE users SET score = score + %s WHERE id = %s" cursor.execute( sql, (difficulty, flask_login.current_user.primary_id)) connection.commit() flask.flash('Correct! +{0} points!'.format(difficulty), 'success') else: with db.create_connection() as connection, connection.cursor( ) as cursor: sql = "UPDATE users SET score = score - %s WHERE id = %s" cursor.execute( sql, (difficulty / 2, flask_login.current_user.primary_id)) connection.commit() flask.flash( 'Incorrect! -{0} points!'.format(difficulty / 2), 'error') else: if form.choice.data == form.hidden.data: flask.flash('Correct!', 'success') else: flask.flash('Incorrect!', 'error') elif form.errors: for item in form.errors.items(): flask.flash(item, 'error') return flask.render_template('home.html', x_value=str(x), y_value=str(y), statement=statement_str, result=str(statement_result), form=form)
def main(): ## Argument Handling if len(sys.argv) != 3: print( "Wrong number or arguments:\nUsage: fukai_gen.py <input> <output>") sys.exit(-1) ## Object Initialization try: iFile = open(sys.argv[1], 'r') oFile = open(sys.argv[2], 'w') except IOError: print("Error at opening files. Exiting...") sys.exit(0) tree = BinTree() ## Command Processing try: for line in iFile: output = cmdParser(tree, line) if output != None: oFile.write(output) oFile.write("\n") except IOError: print("Error at working with files. Exiting...") sys.exit(0)
def tester(): form = forms.StatementForm() if form.validate_on_submit(): try: solution = BinTree.solve_tree( BinTree.build_tree(form.statement.data), 0, 0) if type(solution) is not bool: raise ValueError flask.flash('Given statement is {0}, solution is {1}'.format( form.statement.data, str(solution))) except (ValueError, IndexError): flask.flash('Invalid statement!', 'error') elif form.errors: for item in form.errors.items(): flask.flash(item, 'error') return flask.render_template('tester.html', form=form)
def increasingBST_Basic(root: BinTree) -> BinTree: def helper(node:BinTree): if node.left: helper(node.left) sortedVals.append(node.value) if node.right: helper(node.right) if not root: return sortedVals = [] helper(root) # start building the new Tree newTree = BinTree(-1) curr = newTree for i, val in enumerate(sortedVals): curr.right = BinTree(val) curr = curr.right return newTree.right
def huffman(_dataSet): dataSet = [WeightTNode((item[0], item[1])) for item in _dataSet] heap = HeapQueue(dataSet) while len(heap) > 1: t1, t2 = heap.dequeue(), heap.dequeue() newNode = BinTNode(('', t1.data[1] + t2.data[1]), t1, t2) t1.parent = newNode t2.parent = newNode t1.type = 'l' t2.type = 'r' heap.enqueue(newNode) return BinTree(heap.dequeue())
def increasingBST(root: BinTree) -> BinTree: # this appraoch aims to create the tree while we are traversing and does so in an # iterative fashion if not root: return stack, node = [root], root newTree = BinTree(-1) curr = newTree while stack: while node and node.left: node = node.left stack.append(node) node = stack.pop() node.left = None curr.right = node curr = curr.right if node and node.right: node = node.right stack.append(node) else: node = None return newTree.right