diff options
| -rw-r--r-- | challenge-317/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-317/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-317/sgreen/perl/ch-1.pl | 19 | ||||
| -rwxr-xr-x | challenge-317/sgreen/perl/ch-2.pl | 52 | ||||
| -rwxr-xr-x | challenge-317/sgreen/python/ch-1.py | 21 | ||||
| -rwxr-xr-x | challenge-317/sgreen/python/ch-2.py | 37 | ||||
| -rwxr-xr-x | challenge-317/sgreen/python/test.py | 24 |
7 files changed, 156 insertions, 2 deletions
diff --git a/challenge-317/sgreen/README.md b/challenge-317/sgreen/README.md index aa105d2247..a861bc8e56 100644 --- a/challenge-317/sgreen/README.md +++ b/challenge-317/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 316 +# The Weekly Challenge 317 -Blog: [Sub circular](https://dev.to/simongreennet/weekly-challenge-sub-circular-hno) +Blog: [Short and friendly](https://dev.to/simongreennet/weekly-challenge-short-and-friendly-jf) diff --git a/challenge-317/sgreen/blog.txt b/challenge-317/sgreen/blog.txt new file mode 100644 index 0000000000..e88deb05d4 --- /dev/null +++ b/challenge-317/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-short-and-friendly-jf
\ No newline at end of file diff --git a/challenge-317/sgreen/perl/ch-1.pl b/challenge-317/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..7d6e0ad8b0 --- /dev/null +++ b/challenge-317/sgreen/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@array) { + # The last value is the string we are comparing + my $word = pop(@array); + + # Make up the acronym for the first letter in each word + my $acronym = join( '', map { substr( $_, 0, 1 ) } @array ); + + # Compare it to the supplied string + say lc($acronym) eq lc($word) ? 'true' : 'false'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-317/sgreen/perl/ch-2.pl b/challenge-317/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..df40a09be8 --- /dev/null +++ b/challenge-317/sgreen/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'any'; + +sub main ( $str1, $str2 ) { + # Check strings are the same length + if ( length($str1) != length($str2) ) { + say 'false'; + return; + } + + if ( $str1 eq $str2 ) { + # We can still swap two characters if there are any letters that + # appear two or more times + my %freq = (); + foreach my $i ( 0 .. length($str1) - 1 ) { + ++$freq{ substr( $str1, $i, 1 ) }; + } + if ( any { $_ > 1 } values(%freq) ) { + say 'true'; + } + else { + say 'false'; + } + return; + } + + # Find characters that are different + my @differences = + ( grep { substr( $str1, $_, 1 ) ne substr( $str2, $_, 1 ) } + ( 0 .. length($str1) - 1 ) ); + + if ( $#differences == 1 ) { + # Check that the letters at each position were switched + my ( $pos1, $pos2 ) = @differences; + if ( substr( $str1, $pos1, 1 ) eq substr( $str2, $pos2, 1 ) + and substr( $str2, $pos1, 1 ) eq substr( $str1, $pos2, 1 ) ) + { + say 'true'; + return; + } + } + + say 'false'; +} + +main( $ARGV[0], $ARGV[1] );
\ No newline at end of file diff --git a/challenge-317/sgreen/python/ch-1.py b/challenge-317/sgreen/python/ch-1.py new file mode 100755 index 0000000000..3300ed4a9d --- /dev/null +++ b/challenge-317/sgreen/python/ch-1.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +import sys + + +def is_acronyms(array: list, word: str) -> bool: + # Make up the acronym for the first letter in each word + acronym = ''.join(s[0] for s in array) + + # Compare it to the supplied string + return acronym.lower() == word.lower() + + +def main(): + # The last string is the word (acronym) + result = is_acronyms(sys.argv[1:-1], sys.argv[-1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-317/sgreen/python/ch-2.py b/challenge-317/sgreen/python/ch-2.py new file mode 100755 index 0000000000..b855287fdc --- /dev/null +++ b/challenge-317/sgreen/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +from collections import Counter +import sys + + +def friendly_strings(str1: str, str2: str) -> bool: + # Check strings are the same length + if len(str1) != len(str2): + return False + + if str1 == str2: + # We can still swap two characters if there are any letters that + # appear two or more times + freq = Counter(str1) + return any(i for i in freq.values() if i > 1) + + # Find characters that are different + differences = [ i for i in range(len(str1)) if str1[i] != str2[i]] + + if len(differences) == 2: + # Check that the letters at each position were switched + pos1, pos2 = differences + if str1[pos1] == str2[pos2] and str2[pos1] == str1[pos2]: + return True + + return False + + + +def main(): + result = friendly_strings(sys.argv[1], sys.argv[2]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-317/sgreen/python/test.py b/challenge-317/sgreen/python/test.py new file mode 100755 index 0000000000..7719735c43 --- /dev/null +++ b/challenge-317/sgreen/python/test.py @@ -0,0 +1,24 @@ +#!/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.assertTrue(ch_1.is_acronyms(["Perl", "Weekly", "Challenge"], "PWC")) + self.assertTrue(ch_1.is_acronyms(["Bob", "Charlie", "Joe"], "BCJ")) + self.assertFalse(ch_1.is_acronyms(["Morning", "Good"], "MM")) + + def test_ch_2(self): + self.assertTrue(ch_2.friendly_strings("desc", "dsec")) + self.assertTrue(ch_2.friendly_strings("fcyn", "fcny")) + self.assertFalse(ch_2.friendly_strings("poo", "eop")) + self.assertTrue(ch_2.friendly_strings("stripe", "sprite")) + self.assertTrue(ch_2.friendly_strings("bitter", "bitter")) + self.assertFalse(ch_2.friendly_strings("biter", "biter")) + + +if __name__ == '__main__': + unittest.main() |
