aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2024-02-18 22:06:17 +1100
committerSimon Green <mail@simon.green>2024-02-18 22:06:17 +1100
commitf26d5bb6358c8586add2fc520ed98651688fc2cb (patch)
tree6423761ddb359fb89ad0dcf2d7d4c625e0a96620
parent3f3e0798a68401ce1d67a5e1534f69de16856e82 (diff)
downloadperlweeklychallenge-club-f26d5bb6358c8586add2fc520ed98651688fc2cb.tar.gz
perlweeklychallenge-club-f26d5bb6358c8586add2fc520ed98651688fc2cb.tar.bz2
perlweeklychallenge-club-f26d5bb6358c8586add2fc520ed98651688fc2cb.zip
Simon's solution to challenge 256
-rw-r--r--challenge-256/sgreen/README.md4
-rw-r--r--challenge-256/sgreen/blog.txt1
-rwxr-xr-xchallenge-256/sgreen/perl/ch-1.pl27
-rwxr-xr-xchallenge-256/sgreen/perl/ch-2.pl15
-rwxr-xr-xchallenge-256/sgreen/python/ch-1.py35
-rwxr-xr-xchallenge-256/sgreen/python/ch-2.py26
-rwxr-xr-xchallenge-256/sgreen/python/test.py21
7 files changed, 127 insertions, 2 deletions
diff --git a/challenge-256/sgreen/README.md b/challenge-256/sgreen/README.md
index dc722a565e..a8ccde13ba 100644
--- a/challenge-256/sgreen/README.md
+++ b/challenge-256/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 255
+# The Weekly Challenge 256
-Blog: [The most odd thing](https://dev.to/simongreennet/the-most-odd-thing-4cjo)
+Blog: [Matching and zipping](https://dev.to/simongreennet/matching-and-zipping-3bl7)
diff --git a/challenge-256/sgreen/blog.txt b/challenge-256/sgreen/blog.txt
new file mode 100644
index 0000000000..be3718e45c
--- /dev/null
+++ b/challenge-256/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/matching-and-zipping-3bl7 \ No newline at end of file
diff --git a/challenge-256/sgreen/perl/ch-1.pl b/challenge-256/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..dfca35f33e
--- /dev/null
+++ b/challenge-256/sgreen/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'any';
+
+sub main (@words) {
+ my $count = 0;
+
+ while (@words) {
+ # Take one word from the list
+ my $word = pop(@words);
+
+ # See if the reverse of it is also in the list
+ if (any { $_ eq reverse($word) } @words) {
+ $count++;
+ }
+ }
+
+ # Return the number of pairs
+ say $count;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-256/sgreen/perl/ch-2.pl b/challenge-256/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..e9cf49917d
--- /dev/null
+++ b/challenge-256/sgreen/perl/ch-2.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'mesh';
+
+sub main (@words) {
+ my @arrays = map { [ split // ] } @words;
+ say join( '', mesh(@arrays) );
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-256/sgreen/python/ch-1.py b/challenge-256/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..e84c83e639
--- /dev/null
+++ b/challenge-256/sgreen/python/ch-1.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def count_pairs(words: list) -> int:
+ """Count the number of pairs of strings where one is reversed
+
+ Args:
+ words (list): A list of words
+
+ Returns:
+ int: The number of pairs
+ """
+ count = 0
+
+ while words:
+ # Take one word from the list
+ word = words.pop()
+
+ # See if the reverse of it is also in the list
+ if word[::-1] in words:
+ count += 1
+
+ # Return the number of pairs
+ return count
+
+
+def main():
+ result = count_pairs(sys.argv[1:])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-256/sgreen/python/ch-2.py b/challenge-256/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..051efa1d4c
--- /dev/null
+++ b/challenge-256/sgreen/python/ch-2.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+import sys
+from itertools import zip_longest
+
+
+def merge_strings(words: list) -> str:
+ """Merge strings together by taking the first character from each word,
+ then the second, and so on.
+
+ Args:
+ words (list): A list of words
+
+ Returns:
+ str: The merged string
+ """
+ return ''.join(''.join(s) for s in zip_longest(*words, fillvalue=''))
+
+
+def main():
+ result = merge_strings(sys.argv[1:])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-256/sgreen/python/test.py b/challenge-256/sgreen/python/test.py
new file mode 100755
index 0000000000..ae0ffaef4e
--- /dev/null
+++ b/challenge-256/sgreen/python/test.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__("ch-1")
+ch_2 = __import__("ch-2")
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.count_pairs(["ab", "de", "ed", "bc"]), 1)
+ self.assertEqual(ch_1.count_pairs(["aa", "ba", "cd", "ed"]), 0)
+ self.assertEqual(ch_1.count_pairs(["uv", "qp", "st", "vu", "mn", "pq"]), 2)
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.merge_strings(["abcd", "1234"]), "a1b2c3d4")
+ self.assertEqual(ch_2.merge_strings(["abc", "12345"]), "a1b2c345")
+ self.assertEqual(ch_2.merge_strings(["abcde", "123"]), "a1b2c3de")
+
+
+if __name__ == "__main__":
+ unittest.main()