Skip to content

Instantly share code, notes, and snippets.

@rossmurray
rossmurray / sixnine.go
Created July 31, 2012 06:51
Golang tour #69
package main
import "tour/tree"
import "fmt"
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
WalkRecursive(t, ch)
close(ch)
@rossmurray
rossmurray / gist:5128322
Created March 10, 2013 12:09
A binary search tree in rust 0.5. Very mutable, non-functional style. An exercise to try and learn parts of the language.
extern mod std;
use core::cmp::*;
fn main() {
let b = make_bst::<int>();
b.add(8);
b.add(3);
b.add(6);
b.add(7);
b.add(4);
@rossmurray
rossmurray / gist:5129924
Created March 10, 2013 19:06
A binary search tree in rust (trunk version, I guess 0.6).
extern mod std;
use core::cmp::*;
fn main() {
let mut b = BST::<int> { Root: @None };
b.add(8);
b.add(3);
b.add(6);
b.add(7);
b.add(4);
@rossmurray
rossmurray / alien2009
Created March 18, 2013 04:09
Naive, slow solution for google code jam 2009 qualification problem A, "Alien Language." Rust 0.6 (incoming branch)
extern mod std;
use core::io::ReaderUtil;
use core::vec;
use core::vec::*;
fn main() {
let input = get_lines();
let first_line: &str = input[0];
let toks = str::split_char(first_line, ' ');
let ldn = map(toks, |&s| uint::from_str(s).get());
@rossmurray
rossmurray / credit.rs
Last active December 15, 2015 04:39
rust 0.6 (incoming branch) solution to google code jam practice problem "Store Credit" http://code.google.com/codejam/contest/351101/dashboard
fn main() {
let mut input = get_lines();
let num_cases_str = vec::remove(&mut input, 0);
let num_cases = int::from_str(num_cases_str).get();
let mut i = 1;
while i <= num_cases {
let credit = int::from_str(vec::remove(&mut input, 0)).get();
let _num_items = int::from_str(vec::remove(&mut input, 0)).get();
let items = vec::map(str::split_char(vec::remove(&mut input, 0), ' '), |&s| int::from_str(s).get());
let (a, b) = best_buy(credit, items);
@rossmurray
rossmurray / bytes_to_uint.cs
Last active December 17, 2015 16:49
convert random bytes to uint
using(var rng = new RNGCryptoServiceProvider())
{
var bytes = new byte[4];
rng.GetBytes(bytes);
var value = BitConverter.ToUInt32(bytes, 0);
value.Dump();
}
@rossmurray
rossmurray / ConvertToBase.cs
Last active December 17, 2015 16:49
Converts a number to an arbitrary radix (base), with configurable character set.
public string ConvertToBase(uint value, char[] characterSet)
{
var radix = (uint)characterSet.Length;
var result = new List<char>();
do
{
var remainder = value % radix;
value = value / radix;
result.Add(characterSet[remainder]);
} while(value > 0);
public string ConvertToBase(uint value, char[] characterSet)
{
var radix = (uint)characterSet.Length;
var result = string.Empty;
do
{
var remainder = value % radix;
value = value / radix;
result = characterSet[remainder] + result;
} while(value > 0);
@rossmurray
rossmurray / randomKey.cs
Last active December 17, 2015 16:49
Generates a random key using an arbitrary character set
void Main()
{
var charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); //which characters can appear in the string
var length = 32; //length of the string
Console.WriteLine(GenerateKey(charset, length));
}
///Generate a random string of arbitrary length using RNGCryptoServiceProvider.
///characterSet is the set of characters that can appear in the string.
///length is the desired length of string.
@rossmurray
rossmurray / AsSlidingPairs.cs
Created August 5, 2013 19:16
Returns an IEnumerable of paired input. {1,2,3,4} -> {(1,2), (2,3), (3,4)} Useful in doing things like computing intervals between input elements, while only doing a single pass over the input.
public static class ExtensionMethods
{
//given a list {1,2,3,4}, this will return {(1,2), (2,3), (3,4)}
public static IEnumerable<Tuple<T,T>> AsSlidingPairs<T>(this IEnumerable<T> list)
{
if(list == null) throw new ArgumentNullException();
if(!list.Any())
{
yield break;
}