aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2024-01-21 21:43:03 +1100
committerSimon Green <mail@simon.green>2024-01-21 21:43:03 +1100
commit50431906f587a0d8622ac4714edb64e612facc11 (patch)
tree8cd837b23558d98378ebd5219df6028b4f0b6a23
parent7efb373bb9adffa79f84825217015835805298b5 (diff)
downloadperlweeklychallenge-club-50431906f587a0d8622ac4714edb64e612facc11.tar.gz
perlweeklychallenge-club-50431906f587a0d8622ac4714edb64e612facc11.tar.bz2
perlweeklychallenge-club-50431906f587a0d8622ac4714edb64e612facc11.zip
Simon's solution to challenge 252
-rw-r--r--challenge-252/sgreen/README.md4
-rw-r--r--challenge-252/sgreen/blog.txt1
-rwxr-xr-xchallenge-252/sgreen/perl/ch-1.pl22
-rwxr-xr-xchallenge-252/sgreen/perl/ch-2.pl26
-rwxr-xr-xchallenge-252/sgreen/python/ch-1.py21
-rwxr-xr-xchallenge-252/sgreen/python/ch-2.py24
6 files changed, 96 insertions, 2 deletions
diff --git a/challenge-252/sgreen/README.md b/challenge-252/sgreen/README.md
index 8323c1d923..b1dcdb1358 100644
--- a/challenge-252/sgreen/README.md
+++ b/challenge-252/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 251
+# The Weekly Challenge 252
-Blog: [Lucky values](https://dev.to/simongreennet/lucky-values-4bh5)
+Blog: [Special zeros](https://dev.to/simongreennet/special-zeros-17i5)
diff --git a/challenge-252/sgreen/blog.txt b/challenge-252/sgreen/blog.txt
new file mode 100644
index 0000000000..6a8f075935
--- /dev/null
+++ b/challenge-252/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/special-zeros-17i5 \ No newline at end of file
diff --git a/challenge-252/sgreen/perl/ch-1.pl b/challenge-252/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..6ac3415a2d
--- /dev/null
+++ b/challenge-252/sgreen/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@ints) {
+ my $solution = 0;
+ my $length = scalar(@ints);
+
+ foreach my $pos ( 0 .. $#ints ) {
+ if ( $length % ( $pos + 1 ) == 0 ) {
+ # This is a special number. Add its square to the solution
+ $solution += $ints[$pos]**2;
+ }
+ }
+
+ say $solution;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-252/sgreen/perl/ch-2.pl b/challenge-252/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..5c37726810
--- /dev/null
+++ b/challenge-252/sgreen/perl/ch-2.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main ($n) {
+ my @solution = ();
+ my $half = int($n / 2);
+
+ # If we have an odd number, add a zero
+ if ($n % 2 == 1) {
+ push @solution, 0;
+ }
+
+ foreach my $i (1 .. $half) {
+ # Add a pair of numbers
+ unshift @solution, -$i;
+ push @solution, $i;
+ }
+
+ say join ', ', @solution;
+}
+
+main($ARGV[0]); \ No newline at end of file
diff --git a/challenge-252/sgreen/python/ch-1.py b/challenge-252/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..68b87cbc81
--- /dev/null
+++ b/challenge-252/sgreen/python/ch-1.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(ints):
+ solution = 0
+ length = len(ints)
+
+ for pos in range(length):
+ if length % (pos+1) == 0:
+ # This is a special number. Add its square to the solution
+ solution += ints[pos] ** 2
+
+ print(solution)
+
+
+if __name__ == '__main__':
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ main(array)
diff --git a/challenge-252/sgreen/python/ch-2.py b/challenge-252/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..745338701a
--- /dev/null
+++ b/challenge-252/sgreen/python/ch-2.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(n):
+ solution = []
+ half = n // 2
+
+ # If we have an odd number, add a zero
+ if n % 2 == 1:
+ solution = [0]
+
+ for i in range(half):
+ # Add a pair of numbers
+ solution.insert(0, -i-1)
+ solution.append(i+1)
+
+ print(*solution, sep=', ')
+
+
+if __name__ == '__main__':
+ # Convert input into integers
+ main(int(sys.argv[1]))