This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
┌───┬───┬───┬────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# polygon.rb | |
include Math | |
TAU = PI * 2 | |
class Polygon | |
# o = Polygon.new :KIND, PARAMS | |
# o.kind |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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[@]}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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`}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# | |
class FixedNum | |
attr_accessor :num, :rem | |
def initialize num, rem=0 | |
@num = num ; @rem = rem ; self | |
end |
OlderNewer