aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-04-22 16:47:33 +0100
committerGitHub <noreply@github.com>2025-04-22 16:47:33 +0100
commitd6eb79f9e37490af474da54261d924145be7bf37 (patch)
tree866c015015e9ae44415a779853bcf18d63ee78e2
parent4145b59216c074e1caeca19ec1d06c4240d389bc (diff)
parent3b958e96ccffdfc8819b3a8d85dc9cb9dd91bde4 (diff)
downloadperlweeklychallenge-club-d6eb79f9e37490af474da54261d924145be7bf37.tar.gz
perlweeklychallenge-club-d6eb79f9e37490af474da54261d924145be7bf37.tar.bz2
perlweeklychallenge-club-d6eb79f9e37490af474da54261d924145be7bf37.zip
Merge pull request #11921 from oWnOIzRi/week317
add solutions week 317 in python and perl
-rw-r--r--challenge-317/steven-wilson/perl/ch-1.pl16
-rw-r--r--challenge-317/steven-wilson/python/ch-1.py22
-rw-r--r--challenge-317/steven-wilson/python/ch-2.py26
3 files changed, 64 insertions, 0 deletions
diff --git a/challenge-317/steven-wilson/perl/ch-1.pl b/challenge-317/steven-wilson/perl/ch-1.pl
new file mode 100644
index 0000000000..29976f26b9
--- /dev/null
+++ b/challenge-317/steven-wilson/perl/ch-1.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/env perl
+
+use v5.35;
+use Test2::Bundle::More;
+
+sub acronyms{
+ my ($words, $word) = @_;
+ my $first = join "", map { substr($_, 0, 1) } @$words;
+ return $first eq $word;
+}
+
+ok(acronyms(["Perl", "Weekly", "Challenge"], "PWC"));
+ok(acronyms(["Bob", "Charlie", "Joe"], "BCJ"));
+ok(!acronyms(["Morning", "Good"], "MM"));
+
+done_testing();
diff --git a/challenge-317/steven-wilson/python/ch-1.py b/challenge-317/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..b0d7b864ac
--- /dev/null
+++ b/challenge-317/steven-wilson/python/ch-1.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+
+def acronyms(words, word):
+ """ Given an array of words and a word, return true if concatenating the
+ first letter of each word in the given array matches the given word, return
+ false otherwise.
+
+ >>> acronyms(["Perl", "Weekly", "Challenge"], "PWC")
+ True
+ >>> acronyms(["Bob", "Charlie", "Joe"], "BCJ")
+ True
+ >>> acronyms(["Morning", "Good"], "MM")
+ False
+ """
+ return word == "".join(w[0] for w in words)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)
diff --git a/challenge-317/steven-wilson/python/ch-2.py b/challenge-317/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..7fea218224
--- /dev/null
+++ b/challenge-317/steven-wilson/python/ch-2.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+from itertools import combinations
+
+
+def friendly_strings(a, b):
+ """ Given two strings, return true if swapping any two letters in one
+ string match the other string, return false otherwise.
+
+ >>> friendly_strings("desc", "dsec")
+ True
+ >>> friendly_strings("fuck", "fcuk")
+ True
+ >>> friendly_strings("poo", "eop")
+ False
+ >>> friendly_strings("stripe", "sprite")
+ True
+ """
+ swaps = combinations(range(len(a)), 2)
+ return any(a[:x] + a[y] + a[x+1:y] + a[x] + a[y+1:] == b for x, y in swaps)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)