Skip to content

Instantly share code, notes, and snippets.

@USM-F
USM-F / elo.py
Last active November 23, 2021 10:09
Rating Elo for Rocky franchise
SCORES = {'win': 1, 'lose': 0, 'tie': 0.5}
class Player:
INITIAL_RATING = 1000
def __init__(self, name, rating=INITIAL_RATING, total_matches=0):
self.name = name
self.rating = rating
@USM-F
USM-F / adjacency_matrix_to_adjacency_list.py
Last active November 24, 2020 13:16
Convert adjacency matrix to adjacency list
import numpy as np
def adjacency_matrix_to_adjacency_list(adjacency_matrix):
adjacency_list = []
indices = np.argwhere(adjacency_matrix==1)
for node in range(len(adjacency_matrix)):
adjacency_list.append([])
for index in indices:
if index[0] == node:
@USM-F
USM-F / SetCover.py
Last active February 15, 2020 11:47
Simple recursive solver for set cover problem
from collections import Counter
def set_cover_recursive(sets, result, element_index=0, prefix=[]):
if len(prefix)==len(sets[0]):
result.append(prefix)
return True
# Check validity of allocation
for i in range(len(sets)):
if sets[i][element_index] == 1:
set_cover_recursive(sets, result, element_index+1, prefix+[i])
@USM-F
USM-F / MCKP.py
Last active April 19, 2024 01:45
Dynamic programming solution of Multiple-Choice Knapsack Problem (MCKP) in Python
#groups is list of integers in ascending order without gaps
def getRow(lists, row):
res = []
for l in lists:
for i in range(len(l)):
if i==row:
res.append(l[i])
return res