Skip to content

Commit 9b8cfcf

Browse files
authored
Merge pull request #12790 from simongreen-net/master
sgreen solutions to challenge 341
2 parents ac21ae2 + e6b81e6 commit 9b8cfcf

File tree

7 files changed

+148
-2
lines changed

7 files changed

+148
-2
lines changed

challenge-341/sgreen/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# The Weekly Challenge 340
1+
# The Weekly Challenge 341
22

3-
Blog: [Ascending Regex to remove the Duplicates](https://dev.to/simongreennet/weekly-challenge-ascending-regex-to-remove-the-duplicates-2bd)
3+
Blog: [Reversing my broken keys](https://dev.to/simongreennet/weekly-challenge-reversing-my-broken-keys-313l)

challenge-341/sgreen/blog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://dev.to/simongreennet/weekly-challenge-reversing-my-broken-keys-313l

challenge-341/sgreen/perl/ch-1.pl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
use feature 'say';
6+
use experimental 'signatures';
7+
8+
sub main (@array) {
9+
my $input_string = lc shift @array;
10+
my @broken_keys = map { lc } @array;
11+
12+
my $completed_words = 0;
13+
for my $word ( split ' ', $input_string ) {
14+
if ( not grep { index( $word, $_ ) != -1 } @broken_keys ) {
15+
$completed_words++;
16+
}
17+
}
18+
19+
say $completed_words;
20+
}
21+
22+
main(@ARGV);

challenge-341/sgreen/perl/ch-2.pl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
use feature 'say';
6+
use experimental 'signatures';
7+
8+
sub main (@array) {
9+
my ( $input_string, $char ) = @array;
10+
11+
# Find the position of the first occurrence of char in input_string
12+
my $pos = index( $input_string, $char );
13+
14+
# If char is not found, raise an error
15+
if ( $pos == -1 ) {
16+
die "Character '$char' not found in input string.\n";
17+
}
18+
19+
# Reverse the first part of the string
20+
say reverse( substr( $input_string, 0, $pos + 1 ) )
21+
. substr( $input_string, $pos + 1 );
22+
}
23+
24+
main(@ARGV);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
6+
def broken_keyboard(input_string: str, broken_keys: list[str]) -> int:
7+
"""
8+
Count the number of words that can be completely typed on a broken keyboard.
9+
10+
Args:
11+
input_string (str): The input string containing words separated by spaces.
12+
broken_keys (list[str]): A list of broken keys (characters).
13+
14+
Returns:
15+
int: The number of words that can be fully typed without using any broken keys.
16+
"""
17+
18+
# Convert everything to lower case
19+
input_string = input_string.lower()
20+
broken_keys = [key.lower() for key in broken_keys]
21+
22+
# Count words that DON'T contain any broken keys
23+
completed_words = 0
24+
for word in input_string.split():
25+
if not any(char in broken_keys for char in word):
26+
completed_words += 1
27+
28+
return completed_words
29+
30+
def main():
31+
# The first argument is the input string, the rest are broken keys
32+
input_string = sys.argv[1]
33+
broken_keys = sys.argv[2:]
34+
result = broken_keyboard(input_string, broken_keys)
35+
print(result)
36+
37+
38+
if __name__ == "__main__":
39+
main()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
6+
def reverse_prefix(input_string: str, char: str) -> str:
7+
"""
8+
Reverse the prefix of input_string up to and including the first occurrence of char.
9+
10+
Args:
11+
input_string (str): The input string.
12+
char (str): The character to find in the input string.
13+
14+
Returns:
15+
str: The modified string with the prefix reversed.
16+
"""
17+
18+
# Find the position of the first occurrence of char in input_string
19+
pos = input_string.find(char)
20+
21+
# If char is not found, raise an error
22+
if pos == -1:
23+
raise ValueError(f"Character '{char}' not found in input string.")
24+
25+
# Reverse the first part of the string
26+
return input_string[pos::-1] + input_string[pos+1:]
27+
28+
29+
def main():
30+
result = reverse_prefix(sys.argv[1], sys.argv[2])
31+
print(result)
32+
33+
34+
if __name__ == "__main__":
35+
main()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
3+
import unittest
4+
ch_1 = __import__("ch-1")
5+
ch_2 = __import__("ch-2")
6+
7+
8+
class TestClass(unittest.TestCase):
9+
def test_ch_1(self):
10+
self.assertEqual(ch_1.broken_keyboard("Hello World", ["d"]), 1)
11+
self.assertEqual(ch_1.broken_keyboard("apple banana cherry", ["a", "e"]), 0)
12+
self.assertEqual(ch_1.broken_keyboard("Coding is fun", []), 3)
13+
self.assertEqual(ch_1.broken_keyboard("The Weekly Challenge", ["a","b"]), 2)
14+
self.assertEqual(ch_1.broken_keyboard("Perl and Python", ["p"]), 1)
15+
16+
def test_ch_2(self):
17+
self.assertEqual(ch_2.reverse_prefix("programming", "g"), "gorpramming")
18+
self.assertEqual(ch_2.reverse_prefix("hello", "h"), "hello")
19+
self.assertEqual(ch_2.reverse_prefix("abcdefghij", "h"), "hgfedcbaij")
20+
self.assertEqual(ch_2.reverse_prefix("reverse", "s"), "srevere")
21+
self.assertEqual(ch_2.reverse_prefix("perl", "r"), "repl")
22+
23+
24+
if __name__ == "__main__":
25+
unittest.main()

0 commit comments

Comments
 (0)