aboutsummaryrefslogtreecommitdiff
path: root/challenge-122
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-07-25 23:20:26 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-07-25 23:20:26 +0100
commit9b8f1f2164117210fe85516a845e5c27af0a58d4 (patch)
treee3a128c9e32f0de22e24fbd1b3edde8b59847e70 /challenge-122
parent22d65cce27aed9593113becf5c66e27e1b864c2d (diff)
downloadperlweeklychallenge-club-9b8f1f2164117210fe85516a845e5c27af0a58d4.tar.gz
perlweeklychallenge-club-9b8f1f2164117210fe85516a845e5c27af0a58d4.tar.bz2
perlweeklychallenge-club-9b8f1f2164117210fe85516a845e5c27af0a58d4.zip
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-122')
-rw-r--r--challenge-122/pete-houston/awk/ch-1.awk17
-rw-r--r--challenge-122/pete-houston/perl/ch-1.pl24
-rw-r--r--challenge-122/pete-houston/perl/ch-2.pl75
3 files changed, 116 insertions, 0 deletions
diff --git a/challenge-122/pete-houston/awk/ch-1.awk b/challenge-122/pete-houston/awk/ch-1.awk
new file mode 100644
index 0000000000..2d4ecf2fff
--- /dev/null
+++ b/challenge-122/pete-houston/awk/ch-1.awk
@@ -0,0 +1,17 @@
+#!/usr/bin/gawk -f
+#===============================================================================
+#
+# FILE: 12201.awk
+#
+# USAGE: ./12201.awk < INFILE
+#
+# DESCRIPTION: Stream average - output the average of all numbers so
+# far as a list.
+#
+# NOTES: Expects each number as the first field on the line
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 19/07/21
+#===============================================================================
+{ sum += $1; num++; print sum / num }
diff --git a/challenge-122/pete-houston/perl/ch-1.pl b/challenge-122/pete-houston/perl/ch-1.pl
new file mode 100644
index 0000000000..6c01e31795
--- /dev/null
+++ b/challenge-122/pete-houston/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 12201.pl
+#
+# USAGE: ./12201.pl N N [ N ] ...
+#
+# DESCRIPTION: Stream average - output the average of all numbers so
+# far as a list.
+#
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 19/07/21
+#===============================================================================
+
+use strict;
+use warnings;
+
+my $sum = 0;
+my $num = 1;
+
+my @avg = map { $sum += $_; $sum/$num++ } @ARGV;
+print "@avg\n";
diff --git a/challenge-122/pete-houston/perl/ch-2.pl b/challenge-122/pete-houston/perl/ch-2.pl
new file mode 100644
index 0000000000..ce94b44fb6
--- /dev/null
+++ b/challenge-122/pete-houston/perl/ch-2.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 12202.pl
+#
+# USAGE: ./12202.pl N
+#
+# DESCRIPTION: Print all permutations of 1, 2, 3 to sum to N.
+#
+# REQUIREMENTS: Algorithm::Knapsack, List::Util, Math::Combinatorics
+# NOTES: Although this works, it is inelegant.
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 19/07/21
+#===============================================================================
+
+use strict;
+use warnings;
+use Algorithm::Knapsack;
+use List::Util 'sum';
+use Math::Combinatorics;
+
+my $n = shift;
+
+my @aoa = combos ($n, 0);
+@aoa = permos (@aoa);
+for my $set (@aoa) {
+ print "@$set\n";
+}
+
+sub combos {
+ my ($tot, $level) = @_;
+ my @solutions;
+ my @scores;
+ my @fs;
+ push @scores, ($_) x ($tot / $_) for 1 .. 3;
+ my $sack = Algorithm::Knapsack->new (
+ capacity => $tot,
+ weights => \@scores,
+ );
+ $sack->compute;
+ my $combos = 0;
+ my %seen;
+ for my $fit ($sack->solutions) {
+ next unless sum (@scores[@$fit]) == $tot;
+ my $res = join (' + ', @scores[@$fit]) . " = $tot\n";
+ next if $seen{$res};
+ $seen{$res} = 1;
+ # Count frequencies
+ my %freqs;
+ $freqs{$_}++ for @scores[@$fit];
+ push @fs, [map { $freqs{$_} || 0 } 1 .. 3];
+ push @solutions, [@scores[@$fit]];
+ $combos++;
+ }
+ return @fs;
+}
+
+sub permos {
+ my @sets = @_;
+ my @perms;
+ for my $combo (@sets) {
+ # Permute these indistinguishably
+ my $permer = Math::Combinatorics->new (
+ count => sum (@$combo),
+ data => [1 .. 3],
+ frequency => $combo
+ );
+ while (my @x = $permer->next_string) {
+ push @perms, [@x];
+ }
+ }
+ return @perms;
+}