aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-11-20 22:13:27 +0000
committerGitHub <noreply@github.com>2022-11-20 22:13:27 +0000
commitbf7cd7b82049899cd8673c4bc791ed6013a50121 (patch)
tree7702686b0a2d2ba72b3f06071fe4512551ab0a26
parent31bf3bb7b00e1c59dec64f00ac530345d890ed05 (diff)
parent30d14ea3a03767adf5594b3aa162a512c67d8e74 (diff)
downloadperlweeklychallenge-club-bf7cd7b82049899cd8673c4bc791ed6013a50121.tar.gz
perlweeklychallenge-club-bf7cd7b82049899cd8673c4bc791ed6013a50121.tar.bz2
perlweeklychallenge-club-bf7cd7b82049899cd8673c4bc791ed6013a50121.zip
Merge pull request #7112 from simongreen-net/master
Simon's solution to challenge 191
-rw-r--r--challenge-191/sgreen/README.md4
-rw-r--r--challenge-191/sgreen/blog.txt1
-rwxr-xr-xchallenge-191/sgreen/perl/ch-1.pl15
-rwxr-xr-xchallenge-191/sgreen/perl/ch-2.pl43
-rwxr-xr-xchallenge-191/sgreen/python/ch-1.py15
-rwxr-xr-xchallenge-191/sgreen/python/ch-2.py32
6 files changed, 108 insertions, 2 deletions
diff --git a/challenge-191/sgreen/README.md b/challenge-191/sgreen/README.md
index d9610d7312..ef0288441e 100644
--- a/challenge-191/sgreen/README.md
+++ b/challenge-191/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 190
+# The Weekly Challenge 191
-Blog [Capital letters decoded!](https://dev.to/simongreennet/capital-letters-decoded-315f)
+Blog [The cute recursive function](https://dev.to/simongreennet/the-cute-recursive-function-2gdo)
diff --git a/challenge-191/sgreen/blog.txt b/challenge-191/sgreen/blog.txt
new file mode 100644
index 0000000000..933290931a
--- /dev/null
+++ b/challenge-191/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/the-cute-recursive-function-2gdo \ No newline at end of file
diff --git a/challenge-191/sgreen/perl/ch-1.pl b/challenge-191/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..387ebbf45b
--- /dev/null
+++ b/challenge-191/sgreen/perl/ch-1.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@nums) {
+ # Sort the list numerically
+ @nums = sort { $a <=> $b } @nums;
+
+ say $nums[-1] >+ 2 * $nums[-2] ? 1 : -1;
+}
+
+main(@ARGV);
diff --git a/challenge-191/sgreen/perl/ch-2.pl b/challenge-191/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..69e9686bea
--- /dev/null
+++ b/challenge-191/sgreen/perl/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'none';
+
+sub find_solutions ( $front, $back ) {
+ # Recursive function to find possible solutions. Front is the numbers we
+ # have processed, back is what remains
+ if ( scalar(@$back) == 0 ) {
+ return 1;
+ }
+
+ # Iterate over the next value
+ my $count = 0;
+
+ foreach my $i ( @{ $back->[0] } ) {
+ # Call the recursive function if we haven't used this number already
+ if ( none { $_ == $i } @$front ) {
+ $count +=
+ find_solutions( [ @$front, $i ], [ @{$back}[ 1 .. $#$back ] ] );
+ }
+ }
+
+ # Return the number of matches up the recursive function
+ return $count;
+}
+
+sub main ($n) {
+ # Generate a list of list with a possible value for each number
+ my @possible = ();
+
+ foreach my $i ( 1 .. $n ) {
+ push @possible, [ grep { $_ % $i == 0 or $i % $_ == 0 } ( 1 .. $n ) ];
+ }
+
+ say find_solutions( [], \@possible );
+}
+
+main( $ARGV[0] )
diff --git a/challenge-191/sgreen/python/ch-1.py b/challenge-191/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..f121df2fba
--- /dev/null
+++ b/challenge-191/sgreen/python/ch-1.py
@@ -0,0 +1,15 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(s):
+ # Sort the list numerically
+ nums = [int(x) for x in s]
+ nums.sort()
+
+ print(1 if nums[-1] >= 2 * nums[-2] else -1)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-191/sgreen/python/ch-2.py b/challenge-191/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..6c610e6d88
--- /dev/null
+++ b/challenge-191/sgreen/python/ch-2.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+import sys
+
+def find_solutions(front, back):
+ '''Recursive function to find possible solutions. Front is the numbers we
+ have processed, back is what remains'''
+ if len(back) == 0:
+ return 1
+
+ # Iterate over the next value
+ count = 0
+ for i in back[0]:
+ # Call the recursive function if we haven't used this number already
+ if i not in front:
+ count += find_solutions([*front, i], back[1:])
+
+ # Return the number of matches up the recursive function
+ return count
+
+def main(n):
+ '''Main function'''
+ # Generate a list of list with a possible value for each number
+ possible = []
+ for i in range(1, n+1):
+ possible.append([j for j in range(1, n+1) if j % i == 0 or i % j == 0])
+
+ print(find_solutions([], possible))
+
+
+if __name__ == '__main__':
+ main(int(sys.argv[1]))