Skip to content

Instantly share code, notes, and snippets.

@idavis
idavis / gist:4046433
Created November 9, 2012 15:49
Given the values x1,...,xn of the n different scores, and a number k, determine whether k is a possible sum of any combination of scores
internal class Program
{
private static void Main( string[] args )
{
var source = new long[] { 8, 4, 2 };
Console.WriteLine( SolveMemoizedWithUtil( source, 23427 ) );
Console.ReadLine();
}
public static bool Solve( IEnumerable<long> items, long target )
@idavis
idavis / Example Output (grouped)
Created November 8, 2012 21:19 — forked from bradwilson/Example Output (grouped)
find-string.ps1: A PowerShell script to find strings
src\xunit\Sdk\Executor.cs
15: /// ExecutorWrapper instead.
src\xunit.runner.msbuild\Utility\XmlTestRunner.cs
8: public XmlTestRunner(IExecutorWrapper executorWrapper, IRunnerLogger logger)
10: testRunner = new TestRunner(executorWrapper, logger);
src\xunit.runner.msbuild\xunit.cs
98: using (ExecutorWrapper wrapper = new ExecutorWrapper(assemblyFilename, configFilename, ShadowCopy))
src\xunit.runner.msbuild\xunitproject.cs
48: using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.AssemblyFilename, assembly.ConfigFilename, assembly.ShadowCopy))
src\xunit.runner.tdnet\TdNetRunner.cs
function Test-TypeLoaded {
param([string]$typeName)
return @(try{Invoke-Expression "[$typeName]"}catch{}).Length -ne 0
}
Test-TypeLoaded uri
Test-TypeLoaded Uri
Test-TypeLoaded System.uri
Test-TypeLoaded System.uris
\documentclass[12pt]{article}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{amsfonts}
\newtheorem{thm}{Theorem}[section]
\theoremstyle{definition}
\newtheorem{dfn}{Definition}[section]
\theoremstyle{remark}
@idavis
idavis / OpenClasses.ps1
Created October 11, 2012 20:35
Mimicking ruby's open classes with PowerShell
# new and def are used for lack of good PowerShell compatible API names. I want simple and easy to understand for this example.
function def {
param([string]$name, [scriptblock] $initializer)
# Create the static instance registry to mimic the CTS's single class instance per type
if($Global:__CTS__ -eq $null) {
$dictionary = (New-Object -TypeName 'System.Collections.Generic.Dictionary[string,object]' -ArgumentList @([StringComparer]::OrdinalIgnoreCase))
$value = [PSObject]::AsPSObject($dictionary)
$Global:__CTS__ = $value
@idavis
idavis / parameters.ps1
Created September 20, 2012 19:54
Prototypes with Modules
function New-SapiVoice {
return new-prototype "SapiVoice" {
# Define properties
$Speaker = New-Object -ComObject SAPI.SPVoice
$Message = 'Hello, World!'
# Define methods
function Say {
param([string]$Message)
[void]$Speaker.Speak($Message, 1)
}
using System;
namespace ConsoleApplication1
{
interface ITest
{
void Method();
}
class Parent : ITest
@idavis
idavis / Get-Distance.ps1
Created August 10, 2012 15:03
Parametric Polymorphism in PowerShell, but in a single method o.O
$point = new-object psobject
$point | Add-Member NoteProperty -Name X -Value 4
$point | Add-Member NoteProperty -Name Y -Value 0
$point2 = new-object psobject
$point2 | Add-Member NoteProperty -Name X -Value 0
$point2 | Add-Member NoteProperty -Name Y -Value 0
function Get-Distance {
params([string]$message)
Write-Host $message
function New-Prototype {
param($baseObject = (new-object object))
$prototype = [PSObject]::AsPSObject($baseObject)
$prototype.PSObject.TypeNames.Insert(0,"Prototype")
$prototype
}
filter New-Property {
param(
[string]$name,