Skip to content

Instantly share code, notes, and snippets.

View margnus1's full-sized avatar

Magnus Lång margnus1

  • Uppsala, Sweden
View GitHub Profile
@margnus1
margnus1 / gist:7215226
Created October 29, 2013 14:01
Box Packing ASCII art illustration
int n = this->x.size() + 1; // + 1 is used if the 1x1 box is excluded
int s = this->s.val();
std::vector<std::vector<char> > buffer(s, std::vector<char>(s, ' '));
for (int i = 0; i < n - 1; i++) {
int size = this->size(n, i); // Size of box i
int x = this->x[i].val();
int y = this->y[i].val();
if (size > 1) {
for (int xl = x; xl < x + size; xl++) {
@margnus1
margnus1 / schema.ps1
Created September 7, 2013 17:24
PowerShell script to download and present a TimeEdit timetable.
$wc = new-object System.Net.WebClient
# Note: Change the extension of the subscription link to csv
$link = "https://se.timeedit.net/web/uu/db1/schema/ri63061Y655010QQ65ZY653909550863X06Y09X6055X57660609Y07Q7ZyQ8.csv"
[System.Text.Encoding]::UTF8.GetString($wc.DownloadData($link)).Split("`n") | `
select -skip 3 | ConvertFrom-Csv | `
Sort-Object startdatum,starttid | `
where {$_.Startdatum -ge [System.DateTime]::Now.ToShortDateString()} | `
select startdatum,starttid,kurs,moment,lokal,lärare -First 10 | `
Format-Table -AutoSize
@margnus1
margnus1 / YT-ChanRip.ps1
Created August 20, 2013 11:33
PowerShell script to download the contents of a YouTube channel using youtube-dl.
Function YT-ChanRip($channel) {
$wc = New-Object System.Net.WebClient
for ($index = 1;; $index = $index + 50) {
$url = "http://gdata.youtube.com/feeds/api/users/$channel/uploads?start-index=$index&max-results=50"
$page = $wc.DownloadString($url)
$matches = [System.Text.RegularExpressions.Regex]::Matches($page, "watch\?v=[^ `"`'\\]{11}")
if ($matches.Count -eq 0) { return }
$uniquematches = $matches | Sort-Object | Get-Unique -AsString
foreach ($match in $uniquematches) {
iex "youtube-dl.exe --title --no-overwrites http://youtube.com/$match"
@margnus1
margnus1 / min_difference_test.py
Last active December 12, 2015 09:49
Tester för AD2.Ass2.1. Now with randomly generated tests!
#!/usr/bin/env python2.7
from __future__ import print_function
from min_difference import *
from string import ascii_lowercase
import random
def defvalf(a, b):
return 0 if a==b else 1
def makeR(firstAlpha, secondAlpha, valf=defvalf):
@margnus1
margnus1 / option_pipe.sml
Created January 29, 2013 13:46
Piping operators for operations that can fail
infix 1 |> !> !?>
fun v |> f = f v
fun NONE !> _ = NONE
| (SOME v) !> f = f v
fun NONE !?> _ = NONE
| (SOME v) !?> f = SOME (f v)
(* Example usage: *)
fun isqrt n =
let val r = n |> real |> Math.sqrt |> round
@margnus1
margnus1 / state_monad.sml
Last active December 11, 2015 15:29
A Haskell-like state monad in Standard ML
datatype ('a, 'b) state = State of ('a -> 'b * 'a)
infix >>= >>
fun runState (State f) s =
f s
fun op>>= (act1, fact2) =
State (fn s =>
let
val (iv, is) = runState act1 s
val act2 = fact2 iv
in