Skip to content

Instantly share code, notes, and snippets.

View goldsborough's full-sized avatar
🔨
Fixing things

Peter Goldsborough goldsborough

🔨
Fixing things
View GitHub Profile
@goldsborough
goldsborough / zip.cpp
Last active November 12, 2022 06:48
Python built-in zip() method implemented in C++
template<typename T>
class Zip
{
public:
typedef std::vector<T> container_t;
template<typename... Args>
Zip(const T& head, const Args&... args)
@goldsborough
goldsborough / main.cpp
Created March 8, 2015 15:01
PCR in C++
#include "pcr.hpp"
#include <iostream>
int main(int argc, char * argv [])
{
DNA dna({DNA::Base::A, DNA::Base::C, DNA::Base::G, DNA::Base::T, DNA::Base::C, DNA::Base::T});
DNA::container_t ret = pcr(dna, 1, 3, 10);
@goldsborough
goldsborough / range.cpp
Created April 2, 2015 01:10
Python range() in C++
class range
{
public:
typedef std::vector<long>::iterator iterator;
typedef std::vector<long>::const_iterator const_iterator;
range(long end)
@goldsborough
goldsborough / weight.cpp
Last active January 8, 2024 21:33
Weight Literals in C++
namespace Weight
{
/*
// A Unit of weight is defined by a type T to represent the
// weight, e.g. std::size_t or double (latter required for
// proper conversion between units), as well as a ratio for
// conversion between the Unit and our base unit: grams.
// The ratio defaults to a ratio of 1/1 for grams.
*/
template<typename T, typename R = std::ratio<1>>
@goldsborough
goldsborough / calculate.py
Created August 15, 2015 22:36
Djikstra's two-stack algorithm for equation parsing
#include <map>
#include <stack>
#include <string>
#include <iostream>
#include <functional>
double calculate(const std::string& equation)
{
static std::string chars("+-*/");
@goldsborough
goldsborough / polynomial.py
Created August 18, 2015 18:28
Polynomial parser and calculator using Horner's rule
import re
from collections import namedtuple
def split_equation(equation):
match = re.match(r'^\s*\w+\((\w+)\)\s*=\s*(.*)$', equation)
return match.groups()
def find_terms(equation, var='x'):
@goldsborough
goldsborough / three-way-partition.py
Last active August 29, 2015 14:27
Three-way-partitioning and solution to Dutch National Flag Problem.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Dutch national flag.
Given an array of N buckets, each containing a Color.Red, Color.White, or
Color.Blue pebble, sort them by color. The allowed operations are:
swap(i,j): swap the pebble in bucket i with the pebble in bucket j.
@goldsborough
goldsborough / rabin-karpy.py
Created August 31, 2015 20:41
Rabin-Karp string search algorithm
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
import random
from collections import namedtuple
def is_prime(n):
"""Primality test"""
@goldsborough
goldsborough / RedBlack.cpp
Created September 22, 2015 00:56
A red-black-tree implementation.
template<typename Key, typename Value>
class Tree
{
public:
struct Pair
{
Pair(const Key& k = Key(), const Value& v = Value())
: key(k)
, value(v)
@goldsborough
goldsborough / keybase.md
Created September 22, 2015 15:45
Keybase verification

Keybase proof

I hereby claim:

  • I am goldsborough on github.
  • I am goldsborough (https://keybase.io/goldsborough) on keybase.
  • I have a public key whose fingerprint is 59DC FC71 710B 6695 B3D5 F5E9 5157 ADCF 0557 F2E4

To claim this, I am signing this object: