Skip to content

Instantly share code, notes, and snippets.

function Roll-Dice {
param(
[Parameter(
Mandatory = $true,
#Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[string]$ndm
)
#should probably check for input not matching ndm pattern
@pohatu
pohatu / gist:5827442
Last active December 18, 2015 18:39
Simulation for Google interview question about having girls until you have a boy
# Problem:
# In a country in which people only want boys every family continues to have children until they have a boy.
# If they have a girl, they have another child. If they have a boy, they stop.
# What is the proportion of boys to girls in the country?
$global:m=0; $global:f=0;
function havebabiesuntilboy(){
while($true){
if ((Get-Random -Minimum 0 -Maximum 2) -eq 1)
@pohatu
pohatu / md2html.ps1
Created July 1, 2013 19:38
Use the GitHub markdown service to convert md to html via powershell.
[CmdletBinding()] Param (
[Parameter(Position = 0, Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
[ValidateNotNullOrEmpty()]
[Alias('FullName')]
[String]
$filePath
)
function ConvertFrom-md($mdText){
$response = Invoke-WebRequest -Uri 'https://api.github.com/markdown/raw' -Method Post -body "$mdText" -ContentType "text/plain"
@pohatu
pohatu / Get-DaysLeft
Created September 4, 2013 19:15
Get-DaysLeft will tell you how many days you have left to live for various life expectancies. 100 is a nice round number. 82 is a good long life. 76 is the current life expectancy for the US. 65 is not uncommon.
$today = (Get-Date)
$bday = $(
$DOB = Read-Host "What is your birthday?"
if ($DOB){ $(get-date ($DOB)) }else{$(get-date)} #Default to today to avoid errors when nothing is entered.
)
write-host "You were born on $($bday.ToLongDateSTring())."
write-host "You have lived $(($today - $bday).Days) days."
100,82,76,65|%{ write-host "If you live to be $_ years old, then you have"(( $bday.AddYears($_) ) - $today).Days "days left to live."}
Write-Host "Make the most of them!"
Write-Host "But remember, there is only now."
@pohatu
pohatu / freqofsums.py
Created October 15, 2013 06:27
Calculates the frequency of sums for n samples of digits from 1-n.
#!/usr/local/bin/python2.7
from collections import defaultdict
def freq_of_sums(a, b, v):
if (b == 0):
global freq
if freq.has_key(v):
freq[v]=freq[v]+1
else:
freq[v]=1
# A simple Rock, Paper Scissors Game in Powershell by pohatu
# Uses very cool Select-Item function by James O'Neill
# Presented here: http://blogs.technet.com/b/jamesone/archive/2009/06/24/how-to-get-user-input-more-nicely-in-powershell.aspx
#
# Also uses a clever trick to make remainder act like modulo for negative numbers presented by StackOverflow user polygenelubricants
# here: http://stackoverflow.com/questions/3417183/modulo-of-negative-numbers/3417598#3417598
# You can see the difference between columns labeled Excel:(b-a)%3 and PS:(b-a)%3 column
#
#
# p1 p2 a-b xls:a-b%3 result PS:(b-a)%3
@pohatu
pohatu / fizzbuzz.ps1
Created April 14, 2014 07:22
some fizz buzz solutions in powershell
function fizzbuzz(){
for ($i=1; $i -lt 101; $i++){
$db=0;
if ($i%3 -eq 0){$db = $db -bor 1}
if ($i%5 -eq 0){$db = $db -bor 2}
switch($db)
{
1 { Write-Output "Fizz"} #01
2 { Write-Output "Buzz"} #10
@pohatu
pohatu / fizzbuzz.c
Created April 14, 2014 07:46
fizzbuzz in one loop in C (bit-wise OR trick)
#include <stdio.h>
#include <string.h>
int main() {
int i=0;
for (i=1; i<101; i++){
int db = 0;
db = db ^ 1 * (!(i%3)) ^ 2*(!(i%5));
switch (db){
case (1):
@pohatu
pohatu / fizzbuzz.c
Created April 14, 2014 08:03
FizzBuzz in C (one loop) bit-wise OR trick
#include <stdio.h>
#include <string.h>
int main() {
int i=0;
for (i=1; i<101; i++){
int db = 0;
db = db ^ 1 * (!(i%3)) ^ 2*(!(i%5));
if (db&1){printf("fizz");} //db=01 or 11
if (db&2){printf("buzz");} //db=10 or 11