diff options
Diffstat (limited to 'challenge-255')
| -rw-r--r-- | challenge-255/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-255/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-255/sgreen/perl/ch-1.pl | 37 | ||||
| -rwxr-xr-x | challenge-255/sgreen/perl/ch-2.pl | 27 | ||||
| -rwxr-xr-x | challenge-255/sgreen/python/ch-1.py | 50 | ||||
| -rwxr-xr-x | challenge-255/sgreen/python/ch-2.py | 38 | ||||
| -rwxr-xr-x | challenge-255/sgreen/python/test.py | 20 |
7 files changed, 175 insertions, 2 deletions
diff --git a/challenge-255/sgreen/README.md b/challenge-255/sgreen/README.md index 8963a142f3..dc722a565e 100644 --- a/challenge-255/sgreen/README.md +++ b/challenge-255/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 254 +# The Weekly Challenge 255 -Blog: [Power to the vowels](https://dev.to/simongreennet/power-to-the-vowels-5cdp) +Blog: [The most odd thing](https://dev.to/simongreennet/the-most-odd-thing-4cjo) diff --git a/challenge-255/sgreen/blog.txt b/challenge-255/sgreen/blog.txt new file mode 100644 index 0000000000..7f1db9da17 --- /dev/null +++ b/challenge-255/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/the-most-odd-thing-4cjo
\ No newline at end of file diff --git a/challenge-255/sgreen/perl/ch-1.pl b/challenge-255/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..d6a693eefa --- /dev/null +++ b/challenge-255/sgreen/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub frequency_count ($s) { + # Counts the frequency of each letter + + my %freq = (); + + foreach my $c ( split //, $s ) { + $freq{$c}++; + } + + return \%freq; +} + +sub main ( $s, $t ) { + # Finds extra character in second string + + my $first_freq = frequency_count($s); + my $second_freq = frequency_count($t); + + foreach my $c ( keys %$second_freq ) { + if ( $second_freq->{$c} > ( $first_freq->{$c} // 0 ) ) { + # We have found the extra character + say $c; + return; + } + } + + return 'No extra characters!'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-255/sgreen/perl/ch-2.pl b/challenge-255/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..186fd50544 --- /dev/null +++ b/challenge-255/sgreen/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util qw(first max); + +sub main ($p, $b) { + # Find the most frequent word that isn't banned + + # Calculate the frequency of all the words + my %freq = (); + foreach my $word ($p =~ /\w+(?:'\w+)?/g) { + $freq{lc $word}++; + } + + # Remove the bad word (if it exists) + delete $freq{lc $b}; + + # Find the maximum occurrence of any word, and the word that has this + my $max_count = max(values(%freq)); + say first { $freq{$_} == $max_count } keys %freq; +} + +main($ARGV[0], $ARGV[1]);
\ No newline at end of file diff --git a/challenge-255/sgreen/python/ch-1.py b/challenge-255/sgreen/python/ch-1.py new file mode 100755 index 0000000000..dc81356e84 --- /dev/null +++ b/challenge-255/sgreen/python/ch-1.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import sys + + +def frequency_count(s: str) -> dict: + """Counts the frequency of each letter + + Args: + s (str): The string to use + + Returns: + dict: Key is the letter, values is the number of occurrences. + """ + freq = {} + + for c in s: + freq[c] = freq.get(c, 0) + 1 + + return freq + + +def odd_character(s: str, t: str) -> str: + """Finds extra character in second string + + Args: + s (str): The first string + t(str): The second string + + Returns: + str: The extra character + """ + first_freq = frequency_count(s) + second_freq = frequency_count(t) + + for c in second_freq: + if second_freq[c] > first_freq.get(c, 0): + # We have found the extra character + return c + + return 'No extra characters!' + + +def main(): + result = odd_character(sys.argv[1], sys.argv[2]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-255/sgreen/python/ch-2.py b/challenge-255/sgreen/python/ch-2.py new file mode 100755 index 0000000000..1a04788c22 --- /dev/null +++ b/challenge-255/sgreen/python/ch-2.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def most_frequent_word(p: str, b: str) -> str: + """Find the most frequent word that isn't banned + + Args: + p (str): The paragraph + b (str): The banned word + + Returns: + str: The most frequent word + """ + + # Calculate the frequency of all the words + freq = {} + for word in re.findall(r"\w+(?:'\w+)?", p.lower()): + freq[word] = freq.get(word, 0) + 1 + + # Remove the bad word (if it exists) + freq.pop(b, None) + + # Find the maximum occurrence of any word, and the word that has this + max_count = max(freq.values()) + words = [k for k in freq if freq[k] == max_count] + return words[0] + + +def main(): + result = most_frequent_word(sys.argv[1], sys.argv[2]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-255/sgreen/python/test.py b/challenge-255/sgreen/python/test.py new file mode 100755 index 0000000000..af2c369e5f --- /dev/null +++ b/challenge-255/sgreen/python/test.py @@ -0,0 +1,20 @@ +#!/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.odd_character('Perl', 'Preel'), 'e') + self.assertEqual(ch_1.odd_character('Weekly', 'Weeakly'), 'a') + self.assertEqual(ch_1.odd_character('Box', 'Boxy'), 'y') + + def test_ch_2(self): + self.assertEqual(ch_2.most_frequent_word('Joe hit a ball, the hit ball flew far after it was hit.', 'hit'), 'ball') + self.assertEqual(ch_2.most_frequent_word('Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.', 'the'), 'perl') + + +if __name__ == '__main__': + unittest.main() |
