Skip to content

Instantly share code, notes, and snippets.

@redwrasse
redwrasse / base_to_decimal.py
Last active May 19, 2020 18:17
base to decimal and vice versa conversions
# base_to_decimal.py
"""
a base n to decimal converter and vice versa,
as well as base to base converter
Not all decimals can be expressed in a certain base as a
finite sequence; these are handled with an exception after
exceeding TERMINATION_LENGTH
characters allowed are 0-9 followed by A-Z
@redwrasse
redwrasse / patternmatchautomata.c
Last active July 16, 2020 03:16
String pattern matching automaton in c
/*----------------------------
Collection of states {s_i}. Sequence of inputs (say characters) {c_j}. State transition function T: (s_i, c_j) |-> s_k
. For pattern matching we would say one such state corresponds to a ‘match’. For string matching allowed state transitions
are increments until match found or ‘back to the beginning’ aka start over.
-----------------------------*/
#include <stdio.h>
#include "string.h"
@redwrasse
redwrasse / stack_computations.py
Created May 19, 2020 19:36
simple stack-based computations
# stack_computations.py
"""
Simple example stack computations
"""
"""
Stack diagram
f(n)
f(n-1)
@redwrasse
redwrasse / minimax.c
Created May 20, 2020 23:30
example minimax implementation
/*---------------------------------
minimax example
/ | \
/ \ / \ / | \
/\ /\ /\ /\ /\ /\ /\
2 7 8 1 4 5 9 0 3 6 5 1 2 1
becomes
7
@redwrasse
redwrasse / tictactoe_minimax.py
Last active May 23, 2020 02:35
tictactoe with minimax algorithm
import random
EMPTY = ' '
PLAYER1 = 'x'
PLAYER2 = 'o'
DIM = 3
def player_wins(state, player):
anycol = any(all(state[i][j] == player for i in range(DIM)) for j in range(DIM))
anyrow = any(all(state[j][i] == player for i in range(DIM)) for j in range(DIM))
@redwrasse
redwrasse / tictactoe.c
Created May 26, 2020 19:22
tictactoe minimax in c
#include <iostream>
#define SIZE 3
#define EMPTY 'e'
#define BIGNUM 100
#define MAXNUMSTATES 10000
enum Player {Player1 = 'x', Player2 = 'o'};
struct state {
@redwrasse
redwrasse / linear_ae_pca.py
Created May 28, 2020 19:23
A single-layer linear autoencoder is equivalent to principal component projection
"""
Program demonstration that a single-layer linear autoencoder
is equivalent to principal component projection
"""
import numpy as np
import tensorflow as tf
class A1(object):
@redwrasse
redwrasse / redundant_enc.py
Created June 4, 2020 02:01
a redundant bits encoder
"""
a basic redundant bits encoder-decoder
"""
import json
import random
import sys
SAMPLE_PAYLOAD = {"foo": 1, "bar": 2}
FLIP_PROB = 0.05
@redwrasse
redwrasse / fd_harmonic.py
Created June 12, 2020 23:07
finite difference for harmonic oscillator
import matplotlib.pyplot as plt
def hom1(f, x0, dx0, delta, n):
sol = [x0]
x1, dx = x0 + dx0 * delta, dx0
for _ in range(n-1):
x0, x1, dx = x1, f(x0) * delta + x0, 1.0 * (x1 - x0) / delta
sol.append(x0)
return sol
@redwrasse
redwrasse / insecure_deserialization.py
Created June 17, 2020 18:41
example deserialization attack
# insecure_deserialization.py
"""
as stated in the docs (https://docs.python.org/3/library/pickle.html):
'Warning The pickle module is not secure. Only unpickle data you trust.'
"""
import os