diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-18 23:23:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-18 23:23:21 +0100 |
| commit | 36ff7b6646e15c3a76c287ce4d4462b1d251b101 (patch) | |
| tree | 455dae4b742ce019c160038efb79c92d35abed1f | |
| parent | 8d1db242e6d28eb45e7fc8963488eee71f687d7b (diff) | |
| parent | 59548f33170863d1b540daa9fed90707a6117a01 (diff) | |
| download | perlweeklychallenge-club-36ff7b6646e15c3a76c287ce4d4462b1d251b101.tar.gz perlweeklychallenge-club-36ff7b6646e15c3a76c287ce4d4462b1d251b101.tar.bz2 perlweeklychallenge-club-36ff7b6646e15c3a76c287ce4d4462b1d251b101.zip | |
Merge pull request #12038 from simongreen-net/master
sgreen solutions to challenge 321
| -rw-r--r-- | challenge-321/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-321/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-321/sgreen/perl/ch-1.pl | 31 | ||||
| -rwxr-xr-x | challenge-321/sgreen/perl/ch-2.pl | 28 | ||||
| -rwxr-xr-x | challenge-321/sgreen/python/ch-1.py | 32 | ||||
| -rwxr-xr-x | challenge-321/sgreen/python/ch-2.py | 44 | ||||
| -rwxr-xr-x | challenge-321/sgreen/python/test.py | 21 |
7 files changed, 159 insertions, 2 deletions
diff --git a/challenge-321/sgreen/README.md b/challenge-321/sgreen/README.md index 030723f69a..10e597d6db 100644 --- a/challenge-321/sgreen/README.md +++ b/challenge-321/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 320 +# The Weekly Challenge 321 -Blog: [Words and numbers](https://dev.to/simongreennet/weekly-challenge-the-maximum-difference-2bnm) +Blog: [Compare the Average](https://dev.to/simongreennet/weekly-challenge-compare-the-average-1907) diff --git a/challenge-321/sgreen/blog.txt b/challenge-321/sgreen/blog.txt new file mode 100644 index 0000000000..dda2f7e696 --- /dev/null +++ b/challenge-321/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-compare-the-average-1907
\ No newline at end of file diff --git a/challenge-321/sgreen/perl/ch-1.pl b/challenge-321/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..4552e44178 --- /dev/null +++ b/challenge-321/sgreen/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use bignum; +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'uniq'; + +sub main (@numbers) { + my @averages = (); + + # Make sure the list is even + if ( $#numbers % 2 == 0 ) { + die "You must provide an even number of numbers\n"; + } + + # Sort the list, and convert to Decimal type. + @numbers = sort { $a <=> $b } @numbers; + + foreach my $idx ( 0 .. scalar(@numbers) / 2 - 1 ) { + # Calculate the unique sums for matching pairs + push( @averages, + ( $numbers[$idx] + $numbers[ $#numbers - $idx ] ) / 2 ); + } + + say scalar( uniq @averages ); +} + +main(@ARGV); diff --git a/challenge-321/sgreen/perl/ch-2.pl b/challenge-321/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..f6d20751fa --- /dev/null +++ b/challenge-321/sgreen/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub convert_string($s) { + my $new_string = ''; + foreach my $c (split //, $s) { + if ($c eq '#') { + # This is safe even if new_string is empty + chop($new_string); + } + else { + $new_string .= $c; + } + } + + return $new_string; +} + +sub main ($str1, $str2) { + my $same = convert_string($str1) eq convert_string($str2); + say $same ? 'true' : 'false'; +} + +main($ARGV[0], $ARGV[1]);
\ No newline at end of file diff --git a/challenge-321/sgreen/python/ch-1.py b/challenge-321/sgreen/python/ch-1.py new file mode 100755 index 0000000000..3851260f4e --- /dev/null +++ b/challenge-321/sgreen/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +from decimal import Decimal +import sys + + +def distinct_average(numbers: list) -> int: + unique_averages = set() + + # Make sure the list is even + if len(numbers) % 2 == 1: + raise ValueError("You must provide an even number of numbers") + + # Sort the list, and convert to Decimal type. + numbers = sorted(Decimal(i) for i in numbers) + + for idx in range(len(numbers)//2): + # Calculate the unique sums for matching pairs + unique_averages.add((numbers[idx] + numbers[-1-idx])/2) + + return len(unique_averages) + + +def main(): + # Convert input into integers + array = [Decimal(n) for n in sys.argv[1:]] + result = distinct_average(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-321/sgreen/python/ch-2.py b/challenge-321/sgreen/python/ch-2.py new file mode 100755 index 0000000000..81e0d9ed98 --- /dev/null +++ b/challenge-321/sgreen/python/ch-2.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +import sys + +def covert_string(s: str) -> str: + """Convert a string by treating '#' as a backspace character + + Args: + s (str): The original string + + Returns: + str: The converted string + """ + + new_string = '' + for c in s: + if c == '#': + # This is safe even if new_string is empty + new_string = new_string[:-1] + else: + new_string += c + + return new_string + +def backspace_compare(str1: str, str2: str) -> bool: + """Compare two strings are the same if '#' is a back space character + + Args: + str1 (str): The first string + str2 (str): The second string + + Returns: + bool: Whether the resulting strings are the same + """ + return covert_string(str1) == covert_string(str2) + + +def main(): + result = backspace_compare(sys.argv[1], sys.argv[2]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-321/sgreen/python/test.py b/challenge-321/sgreen/python/test.py new file mode 100755 index 0000000000..2c353d53a2 --- /dev/null +++ b/challenge-321/sgreen/python/test.py @@ -0,0 +1,21 @@ +#!/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.distinct_average([1, 2, 4, 3, 5, 6]), 1) + self.assertEqual(ch_1.distinct_average([0, 2, 4, 8, 3, 5]), 2) + self.assertEqual(ch_1.distinct_average([7, 3, 1, 0, 5, 9]), 2) + + def test_ch_2(self): + self.assertTrue(ch_2.backspace_compare("ab#c", "ad#c")) + self.assertTrue(ch_2.backspace_compare("ab##", "a#b#")) + self.assertFalse(ch_2.backspace_compare("a#b", "c")) + + +if __name__ == "__main__": + unittest.main() |
