diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-30 13:50:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-30 13:50:07 +0100 |
| commit | 7edec2d1810751b703297f27426a9629eb3076d0 (patch) | |
| tree | 24faf33fbb55c59fa74b9d5a88656dc8c03374fe | |
| parent | 6f401642435ccfb226356a1079f44ccdfc7cc00a (diff) | |
| parent | b7df036a3f3988e2ae38af4f43b8d53f4cf5e0f3 (diff) | |
| download | perlweeklychallenge-club-7edec2d1810751b703297f27426a9629eb3076d0.tar.gz perlweeklychallenge-club-7edec2d1810751b703297f27426a9629eb3076d0.tar.bz2 perlweeklychallenge-club-7edec2d1810751b703297f27426a9629eb3076d0.zip | |
Merge pull request #10341 from simongreen-net/master
sgreen solutions to challenge 275
| -rw-r--r-- | challenge-275/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-275/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-275/sgreen/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-275/sgreen/perl/ch-2.pl | 32 | ||||
| -rwxr-xr-x | challenge-275/sgreen/python/ch-1.py | 40 | ||||
| -rwxr-xr-x | challenge-275/sgreen/python/ch-2.py | 41 | ||||
| -rwxr-xr-x | challenge-275/sgreen/python/test.py | 23 |
7 files changed, 167 insertions, 2 deletions
diff --git a/challenge-275/sgreen/README.md b/challenge-275/sgreen/README.md index 772d402709..7b9d0fd9d5 100644 --- a/challenge-275/sgreen/README.md +++ b/challenge-275/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 274 +# The Weekly Challenge 275 -Blog: [Speaking Goat Latin on the fastest bus to town](https://dev.to/simongreennet/speaking-goat-latin-on-the-fastest-bus-to-town-59b2) +Blog: [Broken digits](https://dev.to/simongreennet/broken-digits-e7k) diff --git a/challenge-275/sgreen/blog.txt b/challenge-275/sgreen/blog.txt new file mode 100644 index 0000000000..1aaedc6609 --- /dev/null +++ b/challenge-275/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/broken-digits-e7k
\ No newline at end of file diff --git a/challenge-275/sgreen/perl/ch-1.pl b/challenge-275/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..1cd14af183 --- /dev/null +++ b/challenge-275/sgreen/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'all'; + +sub main ($str, @keys) { + # Convert values to lowercase + my @words = split ' ', lc $str; + @keys = map { lc } @keys; + + # Initialize the counter + my $count = 0; + + # Loop through the strings + foreach my $word (@words) { + if (all { index($word, $_) ==-1 } @keys) { + $count++; + } + } + + say $count; +} + +main($ARGV[0], @ARGV[1..$#ARGV]);
\ No newline at end of file diff --git a/challenge-275/sgreen/perl/ch-2.pl b/challenge-275/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..e135f018a0 --- /dev/null +++ b/challenge-275/sgreen/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::MoreUtils 'first_index'; + +sub main ($str) { + my $current_letter_pos = undef; + my $solution = ''; + my @alphabet = ('a'..'z'); + + foreach my $char (split //, $str) { + if ($char =~ /[0-9]/) { + if (not defined $current_letter_pos) { + die "The first number must follow a letter\n"; + } + + $solution .= $alphabet[($current_letter_pos + $char) % 26]; + } + else { + $solution .= $char; + $current_letter_pos = first_index { $_ eq $char } @alphabet; + } + } + + say $solution; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-275/sgreen/python/ch-1.py b/challenge-275/sgreen/python/ch-1.py new file mode 100755 index 0000000000..e84c66de13 --- /dev/null +++ b/challenge-275/sgreen/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +import sys + + +def broken_keys(s: str, keys: list) -> int: + """ + Counts the number of words in a string that do not contain any of the specified keys. + + Args: + s (str): The input string. + keys (list): A list of keys to check for in the words. + + Returns: + int: The count of words that do not contain any of the specified keys. + """ + + # Convert values to lowercase + words = s.lower().split(' ') + keys = [key.lower() for key in keys] + + # Initialize the counter + count = 0 + + # Loop through the word. + for word in words: + if not any(key in word for key in keys): + count += 1 + + return count + + +def main(): + # The first value is the string and the rest are the keys + result = broken_keys(sys.argv[1], sys.argv[2:]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-275/sgreen/python/ch-2.py b/challenge-275/sgreen/python/ch-2.py new file mode 100755 index 0000000000..ec4fb291d8 --- /dev/null +++ b/challenge-275/sgreen/python/ch-2.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import string +import sys + + +def replace_digits(s: str) -> str: + """ + Replaces digits in a string with corresponding letters from the alphabet. + + Args: + s (str): The input string. + + Returns: + str: The modified string with digits replaced by letters. + """ + + current_letter = None + solution = '' + alphabet = string.ascii_lowercase + + for char in s: + if char.isdigit(): + if current_letter is None: + raise ValueError('The first number must follow a letter.') + solution += alphabet[(alphabet.index(current_letter) + + int(char)) % 26] + else: + solution += char + current_letter = char + + return solution + + +def main(): + result = replace_digits(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-275/sgreen/python/test.py b/challenge-275/sgreen/python/test.py new file mode 100755 index 0000000000..96f643528f --- /dev/null +++ b/challenge-275/sgreen/python/test.py @@ -0,0 +1,23 @@ +#!/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.broken_keys('Perl Weekly Challenge', ['l', 'a']), 0) + self.assertEqual(ch_1.broken_keys('Perl and Raku', ['a']), 1) + self.assertEqual(ch_1.broken_keys('Well done Team PWC', ['l', 'o']), 2) + self.assertEqual(ch_1.broken_keys('The joys of polyglottism', ['T']), 2) + + def test_ch_2(self): + self.assertEqual(ch_2.replace_digits('a1c1e1'), 'abcdef') + self.assertEqual(ch_2.replace_digits('a1b2c3d4'), 'abbdcfdh') + self.assertEqual(ch_2.replace_digits('b2b'), 'bdb') + self.assertEqual(ch_2.replace_digits('a16z'), 'abgz') + + +if __name__ == '__main__': + unittest.main() |
