Skip to content

Commit

Permalink
add specifications and fix examples for current rakudo
Browse files Browse the repository at this point in the history
  • Loading branch information
philandstuff committed Sep 5, 2010
1 parent bb043ee commit 90357c5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
23 changes: 20 additions & 3 deletions perlmonks/combinations-731808.pl
@@ -1,14 +1,31 @@
use v6;

# Specification:
# From http://www.perlmonks.org/?node_id=731808
# Given a list of URL prefixes, and a list of product IDs, make a list
# consisting of each URL prefix concatenated with each product ID.


my @urls = ('http://www.something.com/blah.aspx?code=',
'http://www.somethingelse.com/stuff.aspx?thing=');

my @ids = <375035304 564564774 346464646>;

my @combined = (@urls X @ids).map: {$^a ~ $^b};

# 1. Cross then map
# We use the cross operator X to make every combination of pairs from @urls
# and @ids. We then use map to stringify each pair. $^a is a "placeholder
# argument" - in this case, it refers to the only argument to the block.
my @combined = (@urls X @ids).map: { ~$^a };

.say for @combined;

# or even easier with the cross-meta operator:

# .say for @urls X~ @ids;
# 2. cross hyperoperator
# We use the cross hyperoperator X~
# This combines each element from list1 with each element from list2 using ~
# You can use any infix operator.
# Try (1,2,3) X* (1,2,3) to generate a multiplication table.


.say for @urls X~ @ids;
38 changes: 33 additions & 5 deletions perlmonks/weighted-roll-731696.pl
@@ -1,24 +1,52 @@
use v6;

# Specification:
# From http://www.perlmonks.org/?node_id=731443
# This is part of a strategy game. There are a number of ships in combat,
# and you must determine which order they get to take their actions.
# * Each ship has an initiative value.
# * At the start of the turn, each ship gets a number of ballots equal to
# their initiative. The ballots are drawn out randomly. The first time a
# ship's ballot gets drawn, it takes its turn. The rest of its ballots
# are wasted.
#
# Worked example:
# 3 ships <A B C> with initiative (1,2,4).
# We put ballots into an array: <A B B C C C C>
# We draw a ballot out at random: C. C takes its turn.
# We draw another ballot: C. do nothing.
# We draw another ballot: C. do nothing.
# We draw another ballot: B. B takes its turn.
# We draw another ballot: B. do nothing.
# We draw another ballot: A. A takes its turn.
# We draw the last ballot: C. do nothing.
# So the final order is: <C B A>
# This is not necessarily the most efficient way to perform the algorithm.


# Useful things to note:
# to generate a random number from 1 to N, use (1..N).pick
# to generate a random number from 0 to N-1, use (0..^N).pick

our $SHIPS = 4;
our $REPS = 30;

my @weights = list(1..$SHIPS).map: { 1+16.rand.int };
my @weights = (1..16).pick($SHIPS, :replace);
for @weights.kv -> $k, $v { say "$k: $v" }

my $total = [+] @weights;
say "Total Weights $total";

sub pick (@weights, $total) {
my $rand = $total.rand.int;
#my $rand = $total.rand.int;
my $rand = (0..^$total).pick;
for @weights.kv -> $i, $w {
$rand -= $w;
return $i if $rand < 0;
}
}

sub pickAll (@w,$t) {
my @weights = @w.clone; my $total = $t.clone; #a hack until "is copy" gets implemented
sub pickAll (@weights is copy, $total is copy) {
my @order;
for @weights {
my $pick = pick(@weights, $total);
Expand All @@ -29,7 +57,7 @@ (@w,$t)
return @order;
}

pickAll(@weights,$total).join.say for 1 .. $REPS;
say ~pickAll(@weights,$total) for 1 .. $REPS;



0 comments on commit 90357c5

Please sign in to comment.