diff options
| author | Simon Green <mail@simon.green> | 2025-08-31 21:45:29 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2025-08-31 21:45:29 +1000 |
| commit | aae335bccca20a06aa5a45e5cafd50cca347cff3 (patch) | |
| tree | b6d624e56150a4349f2652abae9a70e1ee889323 | |
| parent | 923952d5af66503e9cd8d065a329f304796eab80 (diff) | |
| download | perlweeklychallenge-club-aae335bccca20a06aa5a45e5cafd50cca347cff3.tar.gz perlweeklychallenge-club-aae335bccca20a06aa5a45e5cafd50cca347cff3.tar.bz2 perlweeklychallenge-club-aae335bccca20a06aa5a45e5cafd50cca347cff3.zip | |
sgreen solutions to challenge 336
| -rw-r--r-- | challenge-336/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-336/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-336/sgreen/perl/ch-1.pl | 33 | ||||
| -rwxr-xr-x | challenge-336/sgreen/perl/ch-2.pl | 48 | ||||
| -rwxr-xr-x | challenge-336/sgreen/python/ch-1.py | 30 | ||||
| -rwxr-xr-x | challenge-336/sgreen/python/ch-2.py | 42 | ||||
| -rwxr-xr-x | challenge-336/sgreen/python/test.py | 25 |
7 files changed, 181 insertions, 2 deletions
diff --git a/challenge-336/sgreen/README.md b/challenge-336/sgreen/README.md index 261fc0ba68..77aa3b753d 100644 --- a/challenge-336/sgreen/README.md +++ b/challenge-336/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 335 +# The Weekly Challenge 336 -Blog: [The Common Winner](https://dev.to/simongreennet/weekly-challenge-the-common-winner-57ka) +Blog: [Equalling the score](https://dev.to/simongreennet/weekly-challenge-equalling-the-score-5f2n) diff --git a/challenge-336/sgreen/blog.txt b/challenge-336/sgreen/blog.txt new file mode 100644 index 0000000000..40f81a14ce --- /dev/null +++ b/challenge-336/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-equalling-the-score-5f2n
\ No newline at end of file diff --git a/challenge-336/sgreen/perl/ch-1.pl b/challenge-336/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..c260df9e44 --- /dev/null +++ b/challenge-336/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 qw(all min max); + +sub main (@ints) { + # Calculate the frequency of each integer in the list + my %freq = (); + $freq{$_}++ foreach @ints; + my @values = values %freq; + + # If any integer appears only once, it is always false. + if ( min(@values) == 1 ) { + say 'false'; + return; + } + + # Check if all frequencies are evenly divisible by an integer. + foreach my $i ( 2 .. max(@values) ) { + if ( all { $_ % $i == 0 } @values ) { + say 'true'; + return; + } + } + + say 'false'; +} + +main(@ARGV); diff --git a/challenge-336/sgreen/perl/ch-2.pl b/challenge-336/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..6e00ea34d1 --- /dev/null +++ b/challenge-336/sgreen/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'sum'; + +sub main (@scores) { + my @score_stack = (); + + foreach my $score (@scores) { + if ( $score eq "C" ) { + # Clear the previous score + if ( $#score_stack == -1 ) { + die "No scores to remove for 'C' operation\n"; + } + pop @score_stack; + } + elsif ( $score eq "D" ) { + # Double the previous score + if ( $#score_stack == -1 ) { + die "No scores to double for 'D' operation\n"; + } + push @score_stack, 2 * $score_stack[-1]; + } + elsif ( $score eq "+" ) { + # Sum the previous two scores + if ( scalar(@score_stack) < 2 ) { + die "Not enough scores to sum for '+' operation\n"; + } + push @score_stack, $score_stack[-1] + $score_stack[-2]; + } + elsif ( $score =~ /^-?\d+$/ ) { + # It's a valid integer score + push @score_stack, $score; + } + else { + # We don't know what score this is + die "Invalid score entry: '$score'\n"; + } + } + + say sum(@score_stack); +} + +main(@ARGV); diff --git a/challenge-336/sgreen/python/ch-1.py b/challenge-336/sgreen/python/ch-1.py new file mode 100755 index 0000000000..d6bd5aa0df --- /dev/null +++ b/challenge-336/sgreen/python/ch-1.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +import sys +from collections import Counter + +def equal_group(ints: list) -> bool: + # Calculate the frequency of each integer in the list + freq = Counter(ints).values() + + # If any integer appears only once, it is always false. + if min(freq) == 1: + return False + + # Check if all frequencies are evenly divisible by an integer. + for i in range(2, max(freq) + 1): + if all(f % i == 0 for f in freq): + return True + + return False + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = equal_group(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-336/sgreen/python/ch-2.py b/challenge-336/sgreen/python/ch-2.py new file mode 100755 index 0000000000..ce381c551e --- /dev/null +++ b/challenge-336/sgreen/python/ch-2.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def final_score(scores: list[str]) -> int: + score_stack = [] + + for score in scores: + if score == "C": + # Clear the previous score + if not score_stack: + raise ValueError("No scores to remove for 'C' operation") + score_stack.pop() + elif score == "D": + # Double the previous score + if not score_stack: + raise ValueError("No scores to double for 'D' operation") + score_stack.append(2 * score_stack[-1]) + elif score == "+": + # Sum the previous two scores + if len(score_stack) < 2: + raise ValueError("Not enough scores to sum for '+' operation") + score_stack.append(score_stack[-1] + score_stack[-2]) + elif re.match(r"^-?\d+$", score): + # It's a valid integer score + score_stack.append(int(score)) + else: + # We don't know what score this is + raise ValueError(f"Invalid score entry: {score}") + + return sum(score_stack) + + +def main(): + result = final_score(sys.argv[1:]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-336/sgreen/python/test.py b/challenge-336/sgreen/python/test.py new file mode 100755 index 0000000000..4255288078 --- /dev/null +++ b/challenge-336/sgreen/python/test.py @@ -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.assertTrue(ch_1.equal_group([1,1,2,2,2,2])) + self.assertFalse(ch_1.equal_group([1,1,1,2,2,2,3,3])) + self.assertTrue(ch_1.equal_group([5,5,5,5,5,5,7,7,7,7,7,7])) + self.assertFalse(ch_1.equal_group([1,2,3,4])) + self.assertTrue(ch_1.equal_group([8,8,9,9,10,10,11,11])) + + def test_ch_2(self): + self.assertEqual(ch_2.final_score(["5","2","C","D","+"]), 30) + self.assertEqual(ch_2.final_score(["5","-2","4","C","D","9","+","+"]), 27) + self.assertEqual(ch_2.final_score(["7","D","D","C","+","3"]), 45) + self.assertEqual(ch_2.final_score(["-5","-10","+","D","C","+"]), -55) + self.assertEqual(ch_2.final_score(["3","6","+","D","C","8","+","D","-2","C","+"]), 128) + + +if __name__ == '__main__': + unittest.main() |
