aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-342/sgreen/README.md4
-rw-r--r--challenge-342/sgreen/blog.txt1
-rwxr-xr-xchallenge-342/sgreen/perl/ch-1.pl35
-rwxr-xr-xchallenge-342/sgreen/perl/ch-2.pl41
-rwxr-xr-xchallenge-342/sgreen/python/ch-1.py39
-rwxr-xr-xchallenge-342/sgreen/python/ch-2.py33
-rwxr-xr-xchallenge-342/sgreen/python/test.py25
7 files changed, 176 insertions, 2 deletions
diff --git a/challenge-342/sgreen/README.md b/challenge-342/sgreen/README.md
index 89934768da..33d3a4920d 100644
--- a/challenge-342/sgreen/README.md
+++ b/challenge-342/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 341
+# The Weekly Challenge 342
-Blog: [Reversing my broken keys](https://dev.to/simongreennet/weekly-challenge-reversing-my-broken-keys-313l)
+Blog: [Balancing the Score](https://dev.to/simongreennet/weekly-challenge-balancing-the-score-38kd)
diff --git a/challenge-342/sgreen/blog.txt b/challenge-342/sgreen/blog.txt
new file mode 100644
index 0000000000..f77d5dc54e
--- /dev/null
+++ b/challenge-342/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-balancing-the-score-38kd \ No newline at end of file
diff --git a/challenge-342/sgreen/perl/ch-1.pl b/challenge-342/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..5c79dbd8e7
--- /dev/null
+++ b/challenge-342/sgreen/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main ($input_string) {
+ # Separate letters and digits
+ my @digits = sort { $a <=> $b } grep { /[0-9]/ } split //, $input_string;
+ my @letters = sort { lc($a) cmp lc($b) } grep { /[A-Za-z]/ } split //, $input_string;
+
+ # If the difference in length is more than 1, there is no solution
+ if (abs(scalar(@digits) - scalar(@letters)) > 1) {
+ say '""';
+ return;
+ }
+
+ # If the lengths are unequal, add a blank to the shorter list
+ if (scalar(@digits) < scalar(@letters)) {
+ unshift @digits, '';
+ } elsif (scalar(@letters) < scalar(@digits)) {
+ push @letters, '';
+ }
+
+ # Interleave the two lists, digits first
+ my $solution = "";
+ foreach my $i (0 .. $#digits) {
+ $solution .= $digits[$i] . $letters[$i];
+ }
+
+ say '"', $solution, '"';
+}
+
+main($ARGV[0]); \ No newline at end of file
diff --git a/challenge-342/sgreen/perl/ch-2.pl b/challenge-342/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..ae895ff605
--- /dev/null
+++ b/challenge-342/sgreen/perl/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub calculate_score ($input_string, $split_index) {
+ # Split the string at each position and count the 0s on the left and 1s
+ # on the right
+ my $left = substr($input_string, 0, $split_index);
+ my $right = substr($input_string, $split_index);
+
+ my $count_0 = ($left =~ tr/0//);
+ my $count_1 = ($right =~ tr/1//);
+
+ return $count_0 + $count_1;
+}
+
+sub main ($input_string) {
+ # Check that the input contains only ones and zeros
+ if ($input_string !~ /^[01]+$/) {
+ die "Input must contain only '0' and '1' characters\n";
+ }
+
+ my $current_max = 0;
+
+ # Calculate the score for each possible split
+ foreach my $i (1 .. length($input_string)-1) {
+ my $score = calculate_score($input_string, $i);
+
+ # If the score is higher than the current max, update it
+ if ($score > $current_max) {
+ $current_max = $score;
+ }
+ }
+
+ say $current_max;
+}
+
+main($ARGV[0]); \ No newline at end of file
diff --git a/challenge-342/sgreen/python/ch-1.py b/challenge-342/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..db96019de8
--- /dev/null
+++ b/challenge-342/sgreen/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+
+
+def balance_string(input_string: str) -> str:
+ # Separate letters and digits
+ digits = sorted(re.findall(r"\d", input_string))
+ letters = sorted(
+ re.findall(r"[A-Za-z]", input_string),
+ key=str.lower
+ )
+
+ # If the difference in length is more than 1, there is no solution
+ if abs(len(digits) - len(letters)) > 1:
+ return ""
+
+ # If the lengths are unequal, add a blank to the shorter list
+ if len(digits) < len(letters):
+ digits.insert(0, "")
+ elif len(letters) < len(digits):
+ letters.append("")
+
+ # Interleave the two lists, digits first
+ solution = ""
+ for i in range(len(digits)):
+ solution += digits[i] + letters[i]
+
+ return solution
+
+
+def main():
+ result = balance_string(sys.argv[1])
+ print('"' + result + '"')
+
+
+if __name__ == "__main__":
+ main()
diff --git a/challenge-342/sgreen/python/ch-2.py b/challenge-342/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..add5c37753
--- /dev/null
+++ b/challenge-342/sgreen/python/ch-2.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+
+
+def max_score(input_string: str) -> int:
+ # Check that the input contains only ones and zeros
+ if not re.match(r'^[01]+$', input_string):
+ raise ValueError("Input must contain only '0' and '1' characters")
+
+ current_max = 0
+
+ # Split the string at each position and count the 0s on the left and 1s
+ # on the right
+ for i in range(1, len(input_string)):
+ left = input_string[:i].count('0')
+ right = input_string[i:].count('1')
+
+ # If the score is higher than the current max, update it
+ if left + right > current_max:
+ current_max = left + right
+
+ return current_max
+
+
+def main():
+ result = max_score(sys.argv[1])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-342/sgreen/python/test.py b/challenge-342/sgreen/python/test.py
new file mode 100755
index 0000000000..debb821112
--- /dev/null
+++ b/challenge-342/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.assertEqual(ch_1.balance_string("a0b1c2"), "0a1b2c")
+ self.assertEqual(ch_1.balance_string("abc12"), "a1b2c")
+ self.assertEqual(ch_1.balance_string("0a2b1c3"), "0a1b2c3")
+ self.assertEqual(ch_1.balance_string("1a23"), "")
+ self.assertEqual(ch_1.balance_string("ab123"), "1a2b3")
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.max_score("0011"), 4)
+ self.assertEqual(ch_2.max_score("0000"), 3)
+ self.assertEqual(ch_2.max_score("1111"), 3)
+ self.assertEqual(ch_2.max_score("0101"), 3)
+ self.assertEqual(ch_2.max_score("011101"), 5)
+
+
+if __name__ == '__main__':
+ unittest.main()