Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions challenge-341/sgreen/README.md
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions challenge-341/sgreen/blog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://dev.to/simongreennet/weekly-challenge-reversing-my-broken-keys-313l
22 changes: 22 additions & 0 deletions challenge-341/sgreen/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -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);
24 changes: 24 additions & 0 deletions challenge-341/sgreen/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -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);
39 changes: 39 additions & 0 deletions challenge-341/sgreen/python/ch-1.py
Original file line number Diff line number Diff line change
@@ -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()
35 changes: 35 additions & 0 deletions challenge-341/sgreen/python/ch-2.py
Original file line number Diff line number Diff line change
@@ -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()
25 changes: 25 additions & 0 deletions challenge-341/sgreen/python/test.py
Original file line number Diff line number Diff line change
@@ -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()