Skip to content

Instantly share code, notes, and snippets.

View f0lie's full-sized avatar
💪
still alive

f0lie f0lie

💪
still alive
View GitHub Profile
@f0lie
f0lie / tic_tac_toe.py
Created August 21, 2021 00:13
Tic Tac Toe in Python with Alpha Beta Pruning and Minimax
# https://stackabuse.com/minimax-and-alpha-beta-pruning-in-python/
# Changes include:
# 1. Reducing the amount of code for looking for end of the game
# 2. Allow for boards of N size. But the game is impossible to play
# because it takes too long to find the optimial moves even with
# alpha beta pruning.
import time
from collections import namedtuple
from typing import List, Tuple, Optional
@f0lie
f0lie / shortest_path.py
Last active August 18, 2021 09:11
Python 3: Clean implementation of Heapq Dijsktra, Bellman-Ford, and SPFA
from collections import defaultdict, deque
import heapq
from typing import OrderedDict
def create_graph(matrix):
graph = defaultdict(list)
for row in range(len(matrix)):
for col in range(len(matrix[0])):
if matrix[row][col] > 0:
@f0lie
f0lie / node.py
Created December 13, 2020 05:46
Python Recursive __repr__ for Linked Lists
class Node:
def __init__(self, val=0, next=None):
self.next = next
self.val = val
def __repr__(self):
# Note that __repr__ is meant to be able "copy and paste" into the console and create itself
return f"Node({self.val}, {self.next})"
if __name__ == "__main__":
@f0lie
f0lie / avl.cpp
Last active October 30, 2022 22:59
C++ 11 AVL Tree with Unique Pointers
#include <iostream>
#include <vector>
#include <exception>
#include <memory>
#include <algorithm>
//https://gist.github.com/f0lie/fc213e1047c4ca9f829b58c41f91091f
template<typename Key, typename T>
//class bst {
private:
@f0lie
f0lie / trie.py
Last active November 12, 2020 13:54
Python 3 Trie Prefix Searching with Recursive Printing
# https://albertauyeung.github.io/2020/06/15/python-trie.html
# Mainly this but I added some neat features and simplified some parts
class Trie:
class Node:
def __init__(self, char):
self.char = char
self.children = {}
# If true it marks the end of a string
# If this isn't here, then we couldn't have overlapping nodes like "change" and "cha"
@f0lie
f0lie / deque.h
Last active March 5, 2021 19:34
C++11 Linked List implemented with smart pointers
// https://gist.github.com/f0lie/ae376ff51834479e3a139b68c3a4cbea
#include <memory>
#include <exception>
template <typename T>
//class deque {
struct node {
T data;
// Do not use a shared pointer when a plain pointer can be used.
// Unique ptrs represent ownership. You can't just add them everywhere.
@f0lie
f0lie / mode.py
Last active November 1, 2020 10:22
Find the mode of a list recursively in Python 3
"""
I did this for a discrete math class problem.
Basically find the mode of an array recursively. Fun little problem.
Use this to understand a basic example of recursion
"""
def find_mode(array):
if len(array) == 1:
return array[0]
num = array.pop()
@f0lie
f0lie / heap.py
Last active November 1, 2020 10:23
Python 3 Heap Implementation
""
I basically implemented MIT 6.0006 Heap lesson.
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-notes/
I stole the index bits from wikipedia.
Mit used arrays starting at 1.
Stole the printing function from:
https://hbfs.wordpress.com/2016/12/06/pretty-printing-a-tree-in-a-terminal/
"""
from collections import deque