Skip to content

Instantly share code, notes, and snippets.

View neilmayhew's full-sized avatar

Neil Mayhew neilmayhew

View GitHub Profile
@neilmayhew
neilmayhew / close-branches.sh
Last active February 27, 2020 19:16 — forked from matiasgarciaisaia/hg-to-git.sh
Script to convert from hg to git that wraps hg-fast-export and performs some additional fixups
#!/bin/bash
USAGE="Usage: $(basename "$0" .sh) SOURCE-HG DEST-GIT"
SRC=${1?$USAGE} || exit 1
DST=${2?$USAGE} || exit 1
SANITIZE=$(dirname "$0")/sanitize.py
(cd "$SRC" &&
@neilmayhew
neilmayhew / Queens.hs
Last active August 29, 2015 14:07
Calculate solutions to the 8 queens on a 5x5 board problem
{-# LANGUAGE TupleSections #-}
import Control.Arrow (first, second)
import Control.Monad (forM_)
import Data.List (intersperse, intersect, (\\))
import System.IO (hFlush, stdout)
-- Generate the list of possible choices of n items from a list
-- Each choice is a pair of the chosen and not-chosen items
choices 0 xs = [([], xs)]
@neilmayhew
neilmayhew / Queens.out
Created October 17, 2014 22:14
Output from Queens.hs
W W - - -
- - - B B
- - - - B
W - - - -
- - B B -
W - - W -
W - - - -
- - - - B
- B - - B
@neilmayhew
neilmayhew / digraphs.py
Last active June 5, 2021 22:10
Python script to print a counted list of all digraphs in a set of files
#!/usr/bin/env python
import fileinput, re
counts = {}
for l in fileinput.input():
l = l.lower()
for m in re.finditer(r'[a-z](?=[a-z])', l):
graph = l[m.start():m.end()+1]
@neilmayhew
neilmayhew / HighProduct.cs
Last active August 29, 2015 14:20
An example of using C# and LINQ to program in a functional style
// Use the digits 0-9 (once each) to create two numbers by concatenation. (e.g. 152 and 3476980).
// What's the highest product you can achieve when these two numbers are multiplied together?
using System;
using System.Linq;
using System.Numerics;
using System.Collections.Generic;
namespace HighProduct
{
@neilmayhew
neilmayhew / MergeSort.fs
Created May 8, 2015 18:24
MergeSort algorithm in F#
let mergeSort xs =
let wrap x = [x]
let rec merge = function
| x::xs, y::ys -> if x < y then x :: merge (xs, y::ys) else y :: merge (x::xs, ys)
| l, [] -> l
| [], m -> m
let rec mergePairs = function
| (l::m::ns) -> merge (l, m) :: mergePairs ns
| ls -> ls
let rec mergeAll = function
@neilmayhew
neilmayhew / FactorTree.fs
Created June 5, 2015 04:44
Print trees of factors of integers
// Display trees of factors of integers
// Tree type
type 'a Tree =
| Branch of 'a * 'a Tree * 'a Tree
| Leaf of 'a
// Computation of Trees of factors
@neilmayhew
neilmayhew / 85restrict-applications
Last active December 2, 2015 17:02
Restrict applications in a desktop session
#!/bin/sh
# This file is sourced by Xsession(5), not executed.
# Conditionally restrict the set of applications visible to the user
if [ -e ~/.config/restrict-applications.conf -a -n "$XDG_DATA_DIRS" ]
then
XDG_DATA_DIRS=$(restrict-applications)
fi
@neilmayhew
neilmayhew / .gitignore
Last active March 10, 2016 17:03
A script for testing delays on ssh connections
*.log
@neilmayhew
neilmayhew / readfile.cc
Created February 17, 2017 17:07
Read a file into a vector using C++11
#include <cstring> // For strerror
#include <list>
#include <memory>
#include <vector>
#include <stdexcept>
#include <unistd.h> // For read
std::vector<unsigned char> readfile(int fd)
{
struct Chunk