diff --git a/challenge-341/sgreen/README.md b/challenge-341/sgreen/README.md index 6fbbc69ddc..89934768da 100644 --- a/challenge-341/sgreen/README.md +++ b/challenge-341/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 340 +# The Weekly Challenge 341 -Blog: [Ascending Regex to remove the Duplicates](https://dev.to/simongreennet/weekly-challenge-ascending-regex-to-remove-the-duplicates-2bd) +Blog: [Reversing my broken keys](https://dev.to/simongreennet/weekly-challenge-reversing-my-broken-keys-313l) diff --git a/challenge-341/sgreen/blog.txt b/challenge-341/sgreen/blog.txt new file mode 100644 index 0000000000..f3afe673a9 --- /dev/null +++ b/challenge-341/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-reversing-my-broken-keys-313l \ No newline at end of file diff --git a/challenge-341/sgreen/perl/ch-1.pl b/challenge-341/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..43211ddcf1 --- /dev/null +++ b/challenge-341/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 (@array) { + my $input_string = lc shift @array; + my @broken_keys = map { lc } @array; + + my $completed_words = 0; + for my $word ( split ' ', $input_string ) { + if ( not grep { index( $word, $_ ) != -1 } @broken_keys ) { + $completed_words++; + } + } + + say $completed_words; +} + +main(@ARGV); diff --git a/challenge-341/sgreen/perl/ch-2.pl b/challenge-341/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..07895f5744 --- /dev/null +++ b/challenge-341/sgreen/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@array) { + my ( $input_string, $char ) = @array; + + # Find the position of the first occurrence of char in input_string + my $pos = index( $input_string, $char ); + + # If char is not found, raise an error + if ( $pos == -1 ) { + die "Character '$char' not found in input string.\n"; + } + + # Reverse the first part of the string + say reverse( substr( $input_string, 0, $pos + 1 ) ) + . substr( $input_string, $pos + 1 ); +} + +main(@ARGV); diff --git a/challenge-341/sgreen/python/ch-1.py b/challenge-341/sgreen/python/ch-1.py new file mode 100755 index 0000000000..abb2a7ef59 --- /dev/null +++ b/challenge-341/sgreen/python/ch-1.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import sys + + +def broken_keyboard(input_string: str, broken_keys: list[str]) -> int: + """ + Count the number of words that can be completely typed on a broken keyboard. + + Args: + input_string (str): The input string containing words separated by spaces. + broken_keys (list[str]): A list of broken keys (characters). + + Returns: + int: The number of words that can be fully typed without using any broken keys. + """ + + # Convert everything to lower case + input_string = input_string.lower() + broken_keys = [key.lower() for key in broken_keys] + + # Count words that DON'T contain any broken keys + completed_words = 0 + for word in input_string.split(): + if not any(char in broken_keys for char in word): + completed_words += 1 + + return completed_words + +def main(): + # The first argument is the input string, the rest are broken keys + input_string = sys.argv[1] + broken_keys = sys.argv[2:] + result = broken_keyboard(input_string, broken_keys) + print(result) + + +if __name__ == "__main__": + main() diff --git a/challenge-341/sgreen/python/ch-2.py b/challenge-341/sgreen/python/ch-2.py new file mode 100755 index 0000000000..ed851bc34c --- /dev/null +++ b/challenge-341/sgreen/python/ch-2.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +import sys + + +def reverse_prefix(input_string: str, char: str) -> str: + """ + Reverse the prefix of input_string up to and including the first occurrence of char. + + Args: + input_string (str): The input string. + char (str): The character to find in the input string. + + Returns: + str: The modified string with the prefix reversed. + """ + + # Find the position of the first occurrence of char in input_string + pos = input_string.find(char) + + # If char is not found, raise an error + if pos == -1: + raise ValueError(f"Character '{char}' not found in input string.") + + # Reverse the first part of the string + return input_string[pos::-1] + input_string[pos+1:] + + +def main(): + result = reverse_prefix(sys.argv[1], sys.argv[2]) + print(result) + + +if __name__ == "__main__": + main() diff --git a/challenge-341/sgreen/python/test.py b/challenge-341/sgreen/python/test.py new file mode 100755 index 0000000000..459c1e0f66 --- /dev/null +++ b/challenge-341/sgreen/python/test.py @@ -0,0 +1,25 @@ +#!/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_keyboard("Hello World", ["d"]), 1) + self.assertEqual(ch_1.broken_keyboard("apple banana cherry", ["a", "e"]), 0) + self.assertEqual(ch_1.broken_keyboard("Coding is fun", []), 3) + self.assertEqual(ch_1.broken_keyboard("The Weekly Challenge", ["a","b"]), 2) + self.assertEqual(ch_1.broken_keyboard("Perl and Python", ["p"]), 1) + + def test_ch_2(self): + self.assertEqual(ch_2.reverse_prefix("programming", "g"), "gorpramming") + self.assertEqual(ch_2.reverse_prefix("hello", "h"), "hello") + self.assertEqual(ch_2.reverse_prefix("abcdefghij", "h"), "hgfedcbaij") + self.assertEqual(ch_2.reverse_prefix("reverse", "s"), "srevere") + self.assertEqual(ch_2.reverse_prefix("perl", "r"), "repl") + + +if __name__ == "__main__": + unittest.main()