Skip to content

Instantly share code, notes, and snippets.

View aks's full-sized avatar
💭
Working

Alan Stebbens aks

💭
Working
View GitHub Profile
@aks
aks / coins.rb
Last active December 17, 2015 06:08
Ruby program to simulate coin tosses until a selected pattern is matched. Two distinct patterns are examined and compared statistically.
#!/usr/bin/env ruby
# coins.rb
# count average flips until HTH is reached
#
def flip ; rand(2) == 1 ? 'H' : 'T' ; end
# 'HTH'.count_flips
class String
@aks
aks / Test-prime-pair-sums.ijs
Created May 12, 2013 09:46
Sum of Two Prime Numbers: If p, q > 2 are consecutive in set of primes. Since p,q can only be odd number, (p+q) is an even number. Can (p+q)/2 be prime? It appears not, as confirmed for the pairs of consecutive primes in first million primes. See the J program below.
i. 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
NB. generate the first 20 primes
p: i. 20
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
NB. box up consecutive pairs of those primes
(2 <\ ]) p: i. 20
┌───┬───┬───┬────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
@aks
aks / coin-change1 output
Created May 21, 2013 16:21
coin-change -- count sets of coins (pennies, nickles, dimes, quarters) that add to a specific total
$ ./coin-change1.rb
For total = 40
There are 31 sets of coins
Set $.25 $.10 $.05 $.01
1: 0 0 0 40
2: 0 0 1 35
3: 0 0 2 30
4: 0 0 3 25
5: 0 0 4 20
@aks
aks / coin-change2.rb output
Created May 21, 2013 16:43
find the smallest number of coins (pennies, nickles, dimes, quarters) that add to a specific total
$ ./coin-change2.rb
For total = 40
There are 31 sets of coins
Set $.25 $.10 $.05 $.01 Coins
1: 1 1 1 0 3
2: 1 0 3 0 4
3: 0 4 0 0 4
4: 0 3 2 0 5
5: 0 2 4 0 6
6: 1 1 0 5 7
@aks
aks / polygon.rb
Created December 31, 2013 06:09
Sample demonstration ruby class and methods. Three files: polygon.rb, which defines simple polygons: square, rectangle, triangle, circle; test-polygon.rb, a set of simple unit tests of the Polygon class and methods, and finally, the test run output.
# polygon.rb
include Math
TAU = PI * 2
class Polygon
# o = Polygon.new :KIND, PARAMS
# o.kind
@aks
aks / test-bash-aarrays.sh
Created January 23, 2014 06:50
Show how bash associative arrays work.
#!/usr/local/bin/bash
declare -A aa
aa[foo]=bar
aa[fee]=baz
aa[fie]=tar
for key in "${!aa[@]}" ; do
printf "key: '%s' val: '%s'\n" $key "${aa[$key]}"
done
echo "${aa[@]}"
@aks
aks / nodes.py
Created September 16, 2014 05:51
Python script to read a list of node pairs and output the list of node neighbors.
#!/usr/bin/env python3
# nodes <infile
#
# Read in lists of node neighbors, and output complete list of all neighbors
#
# Input:
#
# 0 1
# 1 2
# 2 3
@aks
aks / date-parse.sh
Created May 15, 2015 23:08
Date Parser in Bash
# date_parse_str DATESTRING
#
# parse the DATESTRING. It can be in one of several formats: YYYY-MM-DD,
# YYYY.MM.DD, YYYY/MM/DD, YYYY MM DD, DD MMM YYYY, MMM DD, YYYY, and DD/MM/YYYY
# (if EUROPEAN_DATES is set).
#
# Sets the variables: year, month, day --- unless there was a parser failure
date_parse_str() {
local date="${1:-`date +%F`}"
@aks
aks / my_div.rb
Created June 30, 2015 07:03
division by bitshifting
class Fixnum
def my_div divisor
return nil if divisor == 0
return 0 if self == 0
quotient, mask = 0, 1
dividend = self
while divisor < dividend
divisor <<= 1
mask <<= 1
@aks
aks / no-div.rb
Last active August 29, 2015 14:23
no-div.rb
#!/usr/bin/env ruby
#
#
class FixedNum
attr_accessor :num, :rem
def initialize num, rem=0
@num = num ; @rem = rem ; self
end