Jump to content

AntonisG

Member
  • Posts

    2
  • Joined

  • Last visited

Reputation Activity

  1. Like
    AntonisG reacted to Mira Yurizaki in The under 100 line challenge!   
    If I understood the theory correctly, I made a basic Huffman codec in Python
    def bucketize_string(input): buckets = {} for idx in xrange(0, len(input), 1): char = input[idx] if char not in buckets: buckets[char] = 1 else: buckets[char] += 1 return buckets def create_nodes(leaves, freq_idx = 1): nodes = [] if len(leaves) > 1: for idx in xrange(0, len(leaves)-1, 2): if type(leaves[idx+1]) is tuple: nodes.append([leaves[idx], leaves[idx+1], leaves[idx][freq_idx] + leaves[idx+1][1]]) else: nodes.append([leaves[idx], leaves[idx+1], leaves[idx][freq_idx] + leaves[idx+1][freq_idx]]) if idx < len(leaves) - 2: nodes.append(leaves[idx+2]) else: nodes.append(leaves[0]) return nodes def build_sym_table(tree): sym_table = {} map_node(tree, sym_table, '') return sym_table def map_node(node, sym_table, code): for x in xrange(0, len(node), 1): if type(node[x]) is list: map_node(node[x], sym_table, code + str(x)) elif type(node[x]) is tuple: sym_table[node[x][0]] = code + str(x) def huffman_encode(in_string): import operator buckets = bucketize_string(foobar) tree = create_nodes(sorted(buckets.items(), key=operator.itemgetter(1))) while len(tree) > 2: tree = create_nodes(tree, 2) sym_table = build_sym_table(tree) code = '' for x in xrange(0, len(in_string), 1): code += sym_table[in_string[x]] return code, tree def huffman_decode(in_stream, tree): message = '' node = tree for x in xrange(0, len(in_stream), 1): symbol = int(in_stream[x]) node = node[symbol] if type(node) is tuple: message += node[0] node = tree return message This has no practical purpose (for one it expands rather than compresses), but I was bored.
     
    EDIT again: I think I finally got it down. It now theoretically compresses :3
  2. Informative
    AntonisG reacted to Mayube in The under 100 line challenge!   
    Decided to go a little overboard here for my first post on the LTT forums, so here's a fully functional Login AND Register script in PHP, complete with input validation and exit codes + messages, all in only 88 lines of code!
    <?php if (isset($_POST['login']) && $_POST['login']) { if (!isset['username'] && !isset['email']) { echo 'No username or email provided!'; exit(1); } if (!isset['passwd']) { echo 'No password provided!'; exit(1); } if (!($con = new mysqli('localhost', 'mysql_user', 'mysql_pass', 'users'))) { echo 'Could not connect to database!'; exit(2); } $qry = $con->prepare('SELECT * FROM accounts WHERE ' . ((isset($_POST['username'])) ? 'user' : 'email') . ' = ?'; if (!$qry->bind_param('s', ((isset($_POST['username'])) ? $_POST['username'] : $_POST['email']))) { echo 'Could not query database!'; exit(2); } if (!$qry->execute()) { echo 'Could not query database!'; exit(2); } $qry->bind_result($account); if (($res = $qry->fetch()) === null) { echo 'User does not exist!'; exit(3); } if (!$res) { echo 'Could not query database!'; exit(2); } if (hash("sha256", $_POST['passwd'] . $account['salt']) != $account['passwd']) { echo 'Incorrect password!'; exit(3); } $qry = $con->prepare('UPDATE accounts SET last_login = NOW() WHERE ' . ((isset($_POST['username'])) ? 'user' : 'email') . ' = ?') if (!$qry->bind_param('s', ((isset($_POST['username'])) ? $_POST['username'] : $_POST['email']))) { echo 'Could not update database!'; exit(2); } if (!$qry->execute()) { echo 'Could not query database!'; exit(2); } if (!$qry->affected_rows()) { echo 'Could not query database!'; exit(2); } session_start(); $_SESSION['uuid'] = $account['uuid']; $_SESSION['name'] = $account['user']; echo 'Logged in successfully!'; exit(0); } else { if (!isset($_POST['username']) || !isset($_POST['email'])) { echo 'No username or email provided!'; exit(1); } if (!isset($_POST['passwd'])) { echo 'No password provided!'; exit(1); } if (!($con = new mysqli('localhost', 'mysql_user', 'mysql_pass', 'users'))) { echo 'Could not connect to database!'; exit(2); } $saltStr = ''; $len = random_int(16, 32); for($i = 0; $i < $len; $i++) $saltStr .= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_'[random_int(0, 63)]; $saltStr = hash("sha256", $saltStr); $passwd = hash("sha256", ($_POST['passwd'] . $saltStr)); $qry = $con->prepare("INSERT INTO accounts (user, email, passwd, salt) VALUES (?, ?, ?, ?)"); if (!$qry->bind_param('ssss', $_POST['username'], $_POST['email'], $passwd, $saltStr)) { echo 'Could not query database!'; exit(2); } if (!$qry->execute()) { echo 'Could not query database!'; exit(2); } if (!$qry->affected_rows()) { echo 'Could not query database!'; exit(2); } echo 'Account created successfully!'; exit(0); }  
  3. Like
    AntonisG reacted to CPotter in Most INSANE SSD RAID Setup – IT BOOTS!   
    Thanks to Samsung for sponsoring this video! 
    Buy Samsung 960 PRO NVMe SSD on Amazon: http://geni.us/K6OzakN
    Buy Samsung 960 PRO NVMe SSD on Newegg: http://geni.us/YHYBAJe
  4. Like
    AntonisG reacted to Mira Yurizaki in How memory management in Windows (and others) work   
    A random, perhaps somewhat useless topic on the topic of how modern operating systems manages memory.
     
    So first a primer on the history of memory management.
     
    That's enough history, let's talk about how memory management works! (or the burning question: why shouldn't I turn off the page file when I got like a bazillion GB of memory?)
    Some other reading:
    https://blogs.msdn.microsoft.com/oldnewthing/20091002-00/?p=16513
    https://support.apple.com/en-us/HT201538
    https://www.etalabs.net/overcommit.html
×