Skip to content

Instantly share code, notes, and snippets.

View trygvea's full-sized avatar

Trygve Matland Amundsen trygvea

View GitHub Profile
#!/usr/bin/env python3
"""
Very simple HTTP server in python for logging requests
Usage::
./server.py [<port>]
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
import json
#!/bin/bash
# Smcn
# =============
if [ $# -eq 0 ]; then
echo "Enter date and module, ex 20-05-2021-modul-6"
exit
fi
@trygvea
trygvea / permutations.kt
Created February 10, 2021 10:52
Kotlin Permutations
package foo
fun <T> permutations(list: List<T>): List<List<T>> = when {
list.size > 10 -> throw Exception("You probably dont have enough memory to keep all those permutations")
list.size <= 1 -> listOf(list)
else ->
permute(list.drop(1)).map { perm ->
(list.indices).map { i ->
perm.subList(0, i) + list.first() + perm.drop(i)
}
@trygvea
trygvea / ShortestPathAlgorithm.kt
Created January 13, 2021 11:53
Djikstras shortest path algorithm in kotlin
package algorithms.shortestpath
interface Node
data class Edge(val node1: Node, val node2: Node, val distance: Int)
/**
* See https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
*/
fun findShortestPath(edges: List<Edge>, source: Node, target: Node): ShortestPathResult {
@trygvea
trygvea / learn_to_add.ts
Last active February 26, 2019 21:22
Machine learning - learn to add
import * as tf from '@tensorflow/tfjs'
/*
* Experiment with
* - multiplication, combined (ie x1+3*x2). May have to train 500 epochs to get satisfactory results.
*/
const randomInt = (max: number) =>
Math.floor(Math.random() * Math.floor(max))
@trygvea
trygvea / ml.ts
Created February 26, 2019 19:46
Machine learning - pedagogical
type Data = number[]
type Observation = number
type Prediction = Observation
function learn(xs: Data[], ys: Observation[]) {
return (x: Data): Prediction => {
// use xs and ys to predict outcome of x
return 1
}
}
// Objects with only numeric properties that can be treated as a n-dimensional vector
export type Vector<T> = {
[P in keyof T]: number
};
const vextend = <T>(obj: Vector<T>, key: string, val: number): Vector<T> => Object.assign(obj, { [key]: val });
export const vreduce = <T, R>(obj: Vector<T>, reducer: (agg: R, [key, val]: [string, number]) => R, init: R): R =>
@trygvea
trygvea / debouncePromise.ts
Last active November 23, 2018 09:02
debouncePromise
import {debounce} from 'lodash'; // or whatever
const debouncePromise = <T>(fn: (...args) => Promise<T>, wait: number, options = {}): ((...args) => Promise<T>) => {
return (...args) =>
new Promise((resolve, reject) => {
const promisedFn = (...args) =>
fn(...args)
.then(resolve)
.catch(reject);
const debouncedPromisedFn = debounce(promisedFn, wait, options);
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T];
type Minus<T, U> = {[P in Diff<keyof T, keyof U>]: T[P]};
interface Eo {type: string}
interface Meo extends Eo{ meoStuff: any}
const isMeo = (eo: Eo): eo is Meo => eo.type === 'MEO type'
const aMethod = (eo: Eo) => {
if (isMeo(eo)) {
eo.meoStuff
}
@trygvea
trygvea / gist:31125217cd7220359e8f0ea392712bef
Last active October 11, 2017 10:23
Face detection using dlib
# Cut and paste from:
# http://dlib.net/face_recognition.py.html
# https://github.com/ageitgey/face_recognition/blob/master/face_recognition/api.py
# https://medium.com/towards-data-science/facial-recognition-using-deep-learning-a74e9059a150
#
# Install dlib: See https://www.pyimagesearch.com/2017/03/27/how-to-install-dlib/
# Download dlib models: http://dlib.net/files/
import os
import dlib