Skip to content

Instantly share code, notes, and snippets.

View kpsychas's full-sized avatar

Konstantinos Psychas kpsychas

View GitHub Profile
@kpsychas
kpsychas / mastermind.py
Created November 22, 2019 07:04
Command Line Mastemind in Python
import sys
import random
SLOTS = 4
class Colors:
G = 'g' # green
R = 'r' # red
B = 'b' # blue
@kpsychas
kpsychas / pyCTMC.py
Last active February 17, 2020 08:28
Stationary Distribution of a Continuous Time Markov Chain in Python
"""
Simple class that finds the stationary distribution of a
Continuous Time Markov Chain with two examples
"""
from collections import defaultdict
import numpy as np
@kpsychas
kpsychas / counter_cookbook.py
Last active April 18, 2018 19:05
Counter Cookbook
"""
The following is an attempt to gather various useful usecases of
collections.Counter in python. Some were once contributed to
Stackoverflow Documentation Experiment but since its shut down
I could not recover them.
"""
# imports
from collections import Counter
import random
@kpsychas
kpsychas / LatexCommands.md
Last active November 22, 2019 06:59
Latex Useful Command Collection
  • bold math

    \mathbf{a}

    \bm{\alpha} // requires: \usepackage{bm}

  • double line font (expectation, probability, real numbers)

\mathds{E},\mathds{P} // requires: \usepackage{dsfont}

@kpsychas
kpsychas / experiment.py
Last active October 29, 2019 06:04
Generic Experiment with plot and save option
"""
Generic example of experiment that compares two methods
COS, SIN in two different experiments EXPERIMENT1, EXPERIMENT2
Experiments can be lengthy so one can choose to SAVE the
files so that the plots can be generated again when the
appropriate RUNTAG is set and the PLOT program option is chosen.
"""
import os
import matplotlib.pyplot as plt
@kpsychas
kpsychas / fancy_plot.py
Last active March 21, 2020 17:19
Fancy Plot
import matplotlib.pyplot as plt
import numpy as np
def myplot(q, t, label, color=None, mean_in_legend=False, linewidth=0.5):
if mean_in_legend:
mean_total_q = np.dot(t, q) / t.sum()
plt.plot(t.cumsum(), q, color=color,
label='{} {:.2f}'.format(label, mean_total_q),
linewidth=linewidth)
@kpsychas
kpsychas / git_loc_stats.py
Last active May 16, 2017 18:58
Python script for plotting LoC statistics
#!/usr/bin/python
"""
Display the per-commit size of the current git branch.
source: http://stackoverflow.com/questions/23907/how-can-i-graph-the-lines-of-code-history-for-git-repo
"""
import subprocess
import re
import sys
@kpsychas
kpsychas / bridge_pattern.py
Last active November 22, 2019 07:01
The bridge design pattern retrieved from stackoverflow archived documentation
class Spellbook:
def cast(self, enemy):
raise NotImplementedError
class SpellbookFire(Spellbook):
def cast(self, enemy):
print("I've lit the {} on fire.".format(enemy))
@kpsychas
kpsychas / equality_override.py
Last active August 3, 2016 19:39
Example of overriding object equality in python
from collections import Counter
class A(object):
def __init__(self, name, val):
self.name = name
self.val = val
def __repr__(self):
return 'A(val={})'.format(self.val)
@kpsychas
kpsychas / strategy_recipe.py
Last active November 22, 2019 06:56
Strategy pattern example in python with two different ways of providing a strategy
import types
from functools import partial
class Resource1(object):
def __init__(self, value, update_strategy):
self.value = value
self.update = types.MethodType(update_strategy, self)
class Resource2(object):