Skip to content

Instantly share code, notes, and snippets.

@nwertzberger
Created October 25, 2012 02:49
Show Gist options
  • Save nwertzberger/3950170 to your computer and use it in GitHub Desktop.
Save nwertzberger/3950170 to your computer and use it in GitHub Desktop.
Free-form coding.
#!/usr/bin/perl
# Like you thought i'd use something else
my %cats = (
purring => {
# Specify default probability as well as probabilities given the previous
# action.
default => .1 / 24
},
eating => {
default => 1 / 24.0
},
sleeping => {
sleeping => .7,
purring => .5,
default => .5
},
scratching => {
default => .2/24
},
"licking itself" => {
default => 1/12.0
},
"whining about not being allowed in the basement" => {
eating => .25,
default => 1/24.
},
"using the litter box" => {
sleeping => .2,
default => .5/24
}
);
sub getNumberFollower {
my ($num) = @_;
{ 1 => "st", 2 => "nd", 3 => "rd" }->{$num} || "th";
}
my @prev;
for my $hour (1..24) {
my $follower = &getNumberFollower($hour);
print "on the $hour$follower hour of today, my cat was probably";
my @acts;
for my $action (keys %cats) {
my $act = "default";
my $probability = 1;
for (@prev) {
$probability *= 1 - $cats{$action}->{$_} if defined $cats{$action}->{$_};
}
$probability = ($probability != 1 ? 1 - $probability : $cats{$action}->{default});
if (rand() < $probability) {
if (@acts) {
print " and";
}
push @acts, $action;
print " ".$action;
}
}
if (scalar @acts == 0) {
print " just plain doing nothing!";
}
else {
print ".";
}
@prev = ();
push @prev, @acts; # i forgot how to clone data
print "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment