aboutsummaryrefslogtreecommitdiff
path: root/challenge-150
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2022-02-06 21:48:47 +1100
committerSimon Green <mail@simon.green>2022-02-06 21:48:47 +1100
commitf0aae16dfbef4966dafba0f5d5bb10f527d37376 (patch)
treec82cd0d49b3f75352b91cfc4d52f58f4e520853f /challenge-150
parentebcd59a50056e1da3a3f28c98e867c2344548ac6 (diff)
downloadperlweeklychallenge-club-f0aae16dfbef4966dafba0f5d5bb10f527d37376.tar.gz
perlweeklychallenge-club-f0aae16dfbef4966dafba0f5d5bb10f527d37376.tar.bz2
perlweeklychallenge-club-f0aae16dfbef4966dafba0f5d5bb10f527d37376.zip
sgreen solutions to challenge 150
Diffstat (limited to 'challenge-150')
-rw-r--r--challenge-150/sgreen/README.md4
-rw-r--r--challenge-150/sgreen/blog.txt1
-rwxr-xr-xchallenge-150/sgreen/perl/ch-1.pl27
-rwxr-xr-xchallenge-150/sgreen/perl/ch-2.pl25
-rwxr-xr-xchallenge-150/sgreen/python/ch-1.py22
-rwxr-xr-xchallenge-150/sgreen/python/ch-2.py19
6 files changed, 96 insertions, 2 deletions
diff --git a/challenge-150/sgreen/README.md b/challenge-150/sgreen/README.md
index 08586987d4..ea1bfbf44a 100644
--- a/challenge-150/sgreen/README.md
+++ b/challenge-150/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 149
+# The Weekly Challenge 150
-Solution by Simon Green. [Weekly Challenge 149](https://dev.to/simongreennet/weekly-challenge-149-cln)
+Solution by Simon Green. [Weekly Challenge 150](https://dev.to/simongreennet/weekly-challenge-150-4fmn)
diff --git a/challenge-150/sgreen/blog.txt b/challenge-150/sgreen/blog.txt
new file mode 100644
index 0000000000..528c33a9cc
--- /dev/null
+++ b/challenge-150/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-150-4fmn
diff --git a/challenge-150/sgreen/perl/ch-1.pl b/challenge-150/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..7aecfc5b4a
--- /dev/null
+++ b/challenge-150/sgreen/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub main {
+ my @fibs = @_;
+
+ # Sanity check
+ if ( scalar(@fibs) != 2 ) {
+ die "You must specify two inputs\n";
+ }
+ if ( length( $fibs[0] ) != length( $fibs[1] ) ) {
+ die "Strings must be of the same length\n";
+ }
+
+ # Keep compounding the strings until we have at least 51 digits
+ while ( scalar(@fibs) == 2 or length( $fibs[-1] ) < 51 ) {
+ push @fibs, $fibs[-2] . $fibs[-1];
+ }
+
+ # Print the 51st character from the last string
+ say substr( $fibs[-1], 50, 1 );
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-150/sgreen/perl/ch-2.pl b/challenge-150/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..c003b04f56
--- /dev/null
+++ b/challenge-150/sgreen/perl/ch-2.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use List::Util 'none';
+
+sub main {
+ # Get all squares < 500
+ my @squares = ();
+ foreach my $i ( 2 .. 22 ) {
+ push @squares, $i * $i;
+ }
+
+ # Find all numbers 1 .. 500 that aren't divisable by a square
+ my @solutions = ();
+ foreach my $i ( 1 .. 500 ) {
+ if ( none { $i % $_ == 0 } @squares ) {
+ push @solutions, $i;
+ }
+ }
+ say join ', ', @solutions;
+}
+
+main(); \ No newline at end of file
diff --git a/challenge-150/sgreen/python/ch-1.py b/challenge-150/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..45f7f3e049
--- /dev/null
+++ b/challenge-150/sgreen/python/ch-1.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def main(fibs):
+ # Sanity check
+ if len(fibs) != 2:
+ raise ValueError('You must specify two inputs')
+ if len(fibs[0]) != len(fibs[1]):
+ raise ValueError('Strings must be of the same length')
+
+ # Keep compounding the strings until we have at least 51 digits
+ while len(fibs) == 2 or len(fibs[-1]) < 51:
+ fibs.append(fibs[-2] + fibs[-1])
+
+ # Print the 51st character from the last string
+ print(fibs[-1][50])
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-150/sgreen/python/ch-2.py b/challenge-150/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..176a60336d
--- /dev/null
+++ b/challenge-150/sgreen/python/ch-2.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+
+def main():
+ # Get all squares < 500
+ squares = []
+ for i in range(2, 23):
+ squares.append(i*i)
+
+ # Find all numbers 1 .. 500 that aren't divisable by a square
+ solutions = []
+ for i in range(1, 501):
+ if not any(i % s == 0 for s in squares):
+ solutions.append(i)
+
+ print(*solutions, sep=', ')
+
+
+if __name__ == '__main__':
+ main()