From f8ffba5d2256ee11e2a9ff667cc042b12c4b307e Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 25 May 2025 22:02:38 +1000 Subject: sgreen solutions to challenge 322 --- challenge-322/sgreen/README.md | 4 ++-- challenge-322/sgreen/blog.txt | 1 + challenge-322/sgreen/perl/ch-1.pl | 30 ++++++++++++++++++++++++++++++ challenge-322/sgreen/perl/ch-2.pl | 25 +++++++++++++++++++++++++ challenge-322/sgreen/python/ch-1.py | 32 ++++++++++++++++++++++++++++++++ challenge-322/sgreen/python/ch-2.py | 30 ++++++++++++++++++++++++++++++ challenge-322/sgreen/python/test.py | 22 ++++++++++++++++++++++ 7 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 challenge-322/sgreen/blog.txt create mode 100755 challenge-322/sgreen/perl/ch-1.pl create mode 100755 challenge-322/sgreen/perl/ch-2.pl create mode 100755 challenge-322/sgreen/python/ch-1.py create mode 100755 challenge-322/sgreen/python/ch-2.py create mode 100755 challenge-322/sgreen/python/test.py diff --git a/challenge-322/sgreen/README.md b/challenge-322/sgreen/README.md index 10e597d6db..34165c0406 100644 --- a/challenge-322/sgreen/README.md +++ b/challenge-322/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 321 +# The Weekly Challenge 322 -Blog: [Compare the Average](https://dev.to/simongreennet/weekly-challenge-compare-the-average-1907) +Blog: [Strings and Arrays](https://dev.to/simongreennet/weekly-challenge-strings-and-arrays-4a8h) diff --git a/challenge-322/sgreen/blog.txt b/challenge-322/sgreen/blog.txt new file mode 100644 index 0000000000..b2f30e53b0 --- /dev/null +++ b/challenge-322/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-strings-and-arrays-4a8h \ No newline at end of file diff --git a/challenge-322/sgreen/perl/ch-1.pl b/challenge-322/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..5c678ba3af --- /dev/null +++ b/challenge-322/sgreen/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ( $input_string, $size ) { + # Remove existing dashes + $input_string =~ s/-//g; + + # The start value is the position of the first characters, so the remaining + # characters are a multiple of 'size' + my $start = length($input_string) % $size; + my @parts = (); + + if ($start) { + # The first group will be smaller than the rest + push @parts, substr( $input_string, 0, $start ); + } + + for ( my $pos = $start ; $pos < length($input_string) ; $pos += $size ) { + # Group the remain characters by size. + push @parts, substr( $input_string, $pos, $size ); + } + + say join( '-', @parts ); +} + +main( $ARGV[0], $ARGV[1] ); diff --git a/challenge-322/sgreen/perl/ch-2.pl b/challenge-322/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..45eab061f0 --- /dev/null +++ b/challenge-322/sgreen/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::MoreUtils 'first_index'; +use List::Util 'uniq'; + +sub main (@ints) { + # Generate a list of the sorted unique values. + my @sorted_array = sort { $a <=> $b } uniq(@ints); + my @solution = (); + + foreach my $int (@ints) { + # Return the position (1-based) of the each value. + my $idx = first_index { $_ == $int } @sorted_array; + push @solution, $idx + 1; + } + + say '(', join( ', ', @solution ), ')'; +} + +main(@ARGV); diff --git a/challenge-322/sgreen/python/ch-1.py b/challenge-322/sgreen/python/ch-1.py new file mode 100755 index 0000000000..7d58247b47 --- /dev/null +++ b/challenge-322/sgreen/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +import sys + + +def string_format(input_string: str, size: int) -> str: + # Remove existing dashes + input_string = input_string.replace("-", "") + + # The start value is the position of the first characters, so the remaining + # characters are a multiple of 'size' + start = len(input_string) % size + parts = [] + + + if start: + # The first group will be smaller than the rest + parts.append(input_string[0:start]) + + for pos in range(start, len(input_string), size): + # Group the remain characters by size. + parts.append(input_string[pos:pos+size]) + + return '-'.join(parts) + +def main(): + result = string_format(sys.argv[1], int(sys.argv[2])) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-322/sgreen/python/ch-2.py b/challenge-322/sgreen/python/ch-2.py new file mode 100755 index 0000000000..152fcf9e6f --- /dev/null +++ b/challenge-322/sgreen/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +import sys + + +def rank_array(ints: list) -> list: + """Return an array of the ranks of each element in a list + + Args: + ints (list): The input list + + Returns: + list: The position (1-based) of each integer + """ + # Generate a list of the sorted unique values. + sorted_list = sorted(set(ints)) + + # Return the position (1-based) of the each value. + return [sorted_list.index(i)+1 for i in ints] + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = rank_array(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-322/sgreen/python/test.py b/challenge-322/sgreen/python/test.py new file mode 100755 index 0000000000..a8277d6345 --- /dev/null +++ b/challenge-322/sgreen/python/test.py @@ -0,0 +1,22 @@ +#!/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.string_format("ABC-D-E-F", 3), "ABC-DEF") + self.assertEqual(ch_1.string_format("A-BC-D-E", 2), "A-BC-DE") + self.assertEqual(ch_1.string_format("A-B-CD-E", 4), "A-BCDE") + self.assertEqual(ch_1.string_format("AB-C-D-EF", 2), "AB-CD-EF") + + def test_ch_2(self): + self.assertEqual(ch_2.rank_array([55, 22, 44, 33]), [4, 1, 3, 2]) + self.assertEqual(ch_2.rank_array([10, 10, 10]), [1, 1, 1]) + self.assertEqual(ch_2.rank_array([5, 1, 1, 4, 3]), [4, 1, 1, 3, 2]) + + +if __name__ == "__main__": + unittest.main() -- cgit