diff options
| -rw-r--r-- | challenge-277/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-277/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-277/sgreen/perl/ch-1.pl | 33 | ||||
| -rwxr-xr-x | challenge-277/sgreen/perl/ch-2.pl | 26 | ||||
| -rwxr-xr-x | challenge-277/sgreen/python/ch-1.py | 32 | ||||
| -rwxr-xr-x | challenge-277/sgreen/python/ch-2.py | 27 | ||||
| -rwxr-xr-x | challenge-277/sgreen/python/test.py | 32 |
7 files changed, 153 insertions, 2 deletions
diff --git a/challenge-277/sgreen/README.md b/challenge-277/sgreen/README.md index 621adc8383..8bd32e6a72 100644 --- a/challenge-277/sgreen/README.md +++ b/challenge-277/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 276 +# The Weekly Challenge 277 -Blog: [Complete frequency](https://dev.to/simongreennet/complete-frequency-2fke) +Blog: [Strong counting](https://dev.to/simongreennet/strong-counting-10on) diff --git a/challenge-277/sgreen/blog.txt b/challenge-277/sgreen/blog.txt new file mode 100644 index 0000000000..0fae17fa75 --- /dev/null +++ b/challenge-277/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/strong-counting-10on
\ No newline at end of file diff --git a/challenge-277/sgreen/perl/ch-1.pl b/challenge-277/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..52a759c9b5 --- /dev/null +++ b/challenge-277/sgreen/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'uniq'; + +sub main (@words) { + my @words_list = map { [split / / ]} @words; + my $count = 0; + + # Take a list of unique words in the first list + O: foreach my $word ( uniq( @{ $words_list[0] } ) ) { + # Check that it occurs once in each list + foreach my $words (@words_list) { + my $c = grep { $_ eq $word } @$words; + if ( $c != 1 ) { + # It doesn't, go to the next word. + next O; + } + } + + # It does, add to the count + $count++; + } + + # Count the number of words that appear in all lists + say $count; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-277/sgreen/perl/ch-2.pl b/challenge-277/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..a8d366207b --- /dev/null +++ b/challenge-277/sgreen/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util qw(min uniq); + +sub main (@ints) { + # Remove duplicates + @ints = uniq(@ints); + my $count = 0; + + foreach my $i ( 0 .. $#ints - 1 ) { + foreach my $j ( $i + 1 .. $#ints ) { + if ( abs( $ints[$i] - $ints[$j] ) < min( $ints[$i], $ints[$j] ) ) { + $count++; + } + } + } + + say $count; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-277/sgreen/python/ch-1.py b/challenge-277/sgreen/python/ch-1.py new file mode 100755 index 0000000000..0338922088 --- /dev/null +++ b/challenge-277/sgreen/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +from collections import Counter +import sys + + +def count_common(*words_list: list[list]) -> int: + count = 0 + + # Take a list of unique words in the first list + for word in set(words_list[0]): + # Check that it occurs once in each list + for words in words_list: + if words.count(word) != 1: + # It doesn't, go to the next word. + break + else: + # It does, add to the count + count+=1 + + # Count the number of words that appear in all lists + return count + +def main(): + # Convert two space-separated strings into muliple lists + word_list = (words.split(' ')for words in sys.argv[1:]) + result = count_common(*word_list) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-277/sgreen/python/ch-2.py b/challenge-277/sgreen/python/ch-2.py new file mode 100755 index 0000000000..7896268051 --- /dev/null +++ b/challenge-277/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import sys + + +def strong_pair(ints: list) -> int: + # Remove duplicates + ints = list(set(ints)) + count = 0 + + for i in range(len(ints)-1): + for j in range(i+1, len(ints)): + if abs(ints[i]-ints[j]) < min(ints[i], ints[j]): + count += 1 + + return count + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = strong_pair(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-277/sgreen/python/test.py b/challenge-277/sgreen/python/test.py new file mode 100755 index 0000000000..fd5e90dbf2 --- /dev/null +++ b/challenge-277/sgreen/python/test.py @@ -0,0 +1,32 @@ +#!/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.count_common( + ["Perl", "is", "my", "friend"], + ["Perl", "and", "Raku", "are", "friend"]), + 2) + self.assertEqual( + ch_1.count_common( + ["Perl", "and", "Python", "are", "very", "similar"], + ["Python", "is", "top", "in", "guest", "languages"]), + 1) + self.assertEqual( + ch_1.count_common( + ["Perl", "is", "imperative", "Lisp", "is", "functional"], + ["Crystal", "is", "similar", "to", "Ruby"]), + 0) + + def test_ch_2(self): + self.assertEqual(ch_2.strong_pair([1, 2, 3, 4, 5]), 4) + self.assertEqual(ch_2.strong_pair([5, 7, 1]), 1) + + +if __name__ == '__main__': + unittest.main() |
