aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-06 10:44:54 +0000
committerGitHub <noreply@github.com>2023-12-06 10:44:54 +0000
commit2ed9f7503a4c95780801a467173731576347ad75 (patch)
tree18e20e8ba453e6aba9f655ef9ab5168d2015f348
parentcc5ee9640ade927a8013c8435ec7163fef34b16b (diff)
parent522ed3277bfc40a4d6e3bbcc61ab6f9273b5c625 (diff)
downloadperlweeklychallenge-club-2ed9f7503a4c95780801a467173731576347ad75.tar.gz
perlweeklychallenge-club-2ed9f7503a4c95780801a467173731576347ad75.tar.bz2
perlweeklychallenge-club-2ed9f7503a4c95780801a467173731576347ad75.zip
Merge pull request #9203 from jacoby/master
Fixed and blogged 246
-rw-r--r--challenge-246/dave-jacoby/blog.txt1
-rw-r--r--challenge-246/dave-jacoby/perl/ch-1.pl32
-rw-r--r--challenge-246/dave-jacoby/perl/ch-2.pl18
3 files changed, 33 insertions, 18 deletions
diff --git a/challenge-246/dave-jacoby/blog.txt b/challenge-246/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..dad54dfbf8
--- /dev/null
+++ b/challenge-246/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2023/12/05/make-it-unique-weekly-challenge-246.html
diff --git a/challenge-246/dave-jacoby/perl/ch-1.pl b/challenge-246/dave-jacoby/perl/ch-1.pl
index 6b58ba3912..6b670775a8 100644
--- a/challenge-246/dave-jacoby/perl/ch-1.pl
+++ b/challenge-246/dave-jacoby/perl/ch-1.pl
@@ -4,11 +4,31 @@ use strict;
use warnings;
use experimental qw{ say postderef signatures state };
-say join "\n",
- sort { $a <=> $b } # and the example is sorted numerically, so we will
- map { 1 + int rand 49 } # which are random and between 1 and 49
- # but int rand ( n ) will give a number between 0 and n-1
- # so adding 1 will put it at the right range
- 1 .. 6; # we want six numbers
+my %hash;
+# Write a script that outputs six unique random integers
+# from the range 1 to 49.
+
+# I had missed 'unique', which means we have to be sure we
+# deal with duplicates. Use the random number as keys to a hash
+# and you won't get duplicates.
+
+# Adding the numeric sort just makes it pretty.
+
+while ( scalar keys %hash < 6 ) {
+ my $n = 1 + int rand 49;
+ $hash{$n} = 1;
+}
+
+say join "\n", sort { $a <=> $b } keys %hash;
+
+# And here is my first pass, which ignored "unique".
+# Because of that, it was very simple.
+
+# say join "\n",
+# sort { $a <=> $b } # and the example is sorted numerically, so we will
+# map { 1 + int rand 49 } # which are random and between 1 and 49
+# # but int rand ( n ) will give a number between 0 and n-1
+# # so adding 1 will put it at the right range
+# 1 .. 6; # we want six numbers
diff --git a/challenge-246/dave-jacoby/perl/ch-2.pl b/challenge-246/dave-jacoby/perl/ch-2.pl
index 2cf6806510..9ff50ee947 100644
--- a/challenge-246/dave-jacoby/perl/ch-2.pl
+++ b/challenge-246/dave-jacoby/perl/ch-2.pl
@@ -4,8 +4,6 @@ use strict;
use warnings;
use experimental qw{ say postderef signatures state };
-use Algorithm::Combinatorics qw{ variations };
-
my @examples = (
[ 1, 1, 2, 3, 5 ],
@@ -25,16 +23,12 @@ for my $e (@examples) {
sub lrso (@input) {
OUTER: for my $n ( 2 .. -1 + scalar @input ) {
- for my $p ( 1 .. 100 ) {
- for my $pp ( 1, -1 ) {
- my $ppp = ( $p * $pp ) * $input[ $n - 2 ];
- for my $q ( 1 .. 100 ) {
- for my $qq ( 1, -1 ) {
- my $qqq = ( $q * $qq ) * $input[ $n - 1 ];
- my $rrr = $ppp + $qqq;
- next OUTER if $rrr == $input[$n];
- }
- }
+ for my $p ( -100 .. 100 ) {
+ my $pp = $p * $input[ $n - 2 ];
+ for my $q ( -100 .. 100 ) {
+ my $qq = $q * $input[ $n - 1 ];
+ my $rr = $pp + $qq;
+ next OUTER if $rr == $input[$n];
}
}
return 'false';