diff options
| author | Simon Green <mail@simon.green> | 2024-09-01 18:59:00 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2024-09-01 18:59:00 +1000 |
| commit | 55c3d22707c0a0d6f119d16b6024a78bd1d901d2 (patch) | |
| tree | 5919d5ef4df8f45c21bb34585fdb69ef729e63b3 /challenge-284 | |
| parent | 3726f5d22a659f31a090dd40e9e888f440850596 (diff) | |
| download | perlweeklychallenge-club-55c3d22707c0a0d6f119d16b6024a78bd1d901d2.tar.gz perlweeklychallenge-club-55c3d22707c0a0d6f119d16b6024a78bd1d901d2.tar.bz2 perlweeklychallenge-club-55c3d22707c0a0d6f119d16b6024a78bd1d901d2.zip | |
sgreen solutions to challenge 284
Diffstat (limited to 'challenge-284')
| -rw-r--r-- | challenge-284/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-284/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-284/sgreen/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-284/sgreen/perl/ch-2.pl | 31 | ||||
| -rwxr-xr-x | challenge-284/sgreen/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-284/sgreen/python/ch-2.py | 28 | ||||
| -rwxr-xr-x | challenge-284/sgreen/python/test.py | 31 |
7 files changed, 150 insertions, 2 deletions
diff --git a/challenge-284/sgreen/README.md b/challenge-284/sgreen/README.md index eff69133e3..e5f731d3d7 100644 --- a/challenge-284/sgreen/README.md +++ b/challenge-284/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 282 +# The Weekly Challenge 284 -Blog: [Good keys](https://dev.to/simongreennet/good-keys-2elk) +Blog: [The lucky sort](https://dev.to/simongreennet/the-lucky-sort-1648) diff --git a/challenge-284/sgreen/blog.txt b/challenge-284/sgreen/blog.txt new file mode 100644 index 0000000000..31f26aa46c --- /dev/null +++ b/challenge-284/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/the-lucky-sort-1648
\ No newline at end of file diff --git a/challenge-284/sgreen/perl/ch-1.pl b/challenge-284/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..bd5d874c88 --- /dev/null +++ b/challenge-284/sgreen/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@ints) { + # Calculate the frequency of each integer + my %freq = (); + foreach my $i (@ints) { + ++$freq{$i}; + } + + # Work through the dict, highest first + foreach my $i ( sort { $b <=> $a } keys %freq ) { + if ( $i == $freq{$i} ) { + # We have the lucky integer + say $i; + return; + } + } + + # There is no lucky integer + say -1; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-284/sgreen/perl/ch-2.pl b/challenge-284/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..7d22dafd0f --- /dev/null +++ b/challenge-284/sgreen/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use JSON 'decode_json'; +use List::MoreUtils 'first_index'; + +sub find_index( $lst, $val ) { + # Find the position of val in lst. If it does not appear, return the + # length of the list + my $idx = first_index { $_ == $val } @$lst; + return $idx == -1 ? scalar(@$lst) : $idx; +} + +sub main ($lists) { + my $list1 = $lists->[0]; + my $list2 = $lists->[1]; + + # Sort the first list, in the order they appear in the second list. If + # it doesn't appear in the second list, append to the end in numerical order. + my @solution = sort { + find_index( $list2, $a ) <=> find_index( $list2, $b ) + or $a <=> $b + } @$list1; + say '(', join( ', ', @solution ), ')'; +} + +main( decode_json( $ARGV[0] ) );
\ No newline at end of file diff --git a/challenge-284/sgreen/python/ch-1.py b/challenge-284/sgreen/python/ch-1.py new file mode 100755 index 0000000000..2b7d01598f --- /dev/null +++ b/challenge-284/sgreen/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +from collections import Counter +import sys + + +def lucky_integer(ints: list) -> str: + # Calculate the frequency of each integer + freq = Counter(ints) + + # Work through the dict, highest first + for i in sorted(freq, reverse=True): + if i == freq[i]: + # We have the lucky integer + return i + + # There is no lucky integer + return -1 + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = lucky_integer(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-284/sgreen/python/ch-2.py b/challenge-284/sgreen/python/ch-2.py new file mode 100755 index 0000000000..a236c11600 --- /dev/null +++ b/challenge-284/sgreen/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import json +import sys + + +def find_index(lst: list, val: int): + # Find the position of val in lst. If it does not appear, return the + # length of the list + return lst.index(val) if val in lst else len(lst) + + +def relative_sort(list1: list, list2: list) -> list: + # Sort the first list, in the order they appear in the second list. If + # it doesn't appear in the second list, append to the end in numerical order. + return sorted(list1, key=lambda i: (find_index(list2, i), i)) + + +def main(): + lists = json.loads(sys.argv[1]) + result = relative_sort(*lists) + + # Convert to a tuple to match expected output + print(tuple(result)) + + +if __name__ == '__main__': + main() diff --git a/challenge-284/sgreen/python/test.py b/challenge-284/sgreen/python/test.py new file mode 100755 index 0000000000..8601576245 --- /dev/null +++ b/challenge-284/sgreen/python/test.py @@ -0,0 +1,31 @@ +#!/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.lucky_integer([2, 2, 3, 4]), 2) + self.assertEqual(ch_1.lucky_integer([1, 2, 2, 3, 3, 3]), 3) + self.assertEqual(ch_1.lucky_integer([1, 1, 1, 3]), -1) + + def test_ch_2(self): + self.assertEqual( + ch_2.relative_sort([2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5], + [2, 1, 4, 3, 5, 6]), + [2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9] + ) + self.assertEqual( + ch_2.relative_sort([3, 3, 4, 6, 2, 4, 2, 1, 3], [1, 3, 2]), + [1, 3, 3, 3, 2, 2, 4, 4, 6] + ) + self.assertEqual( + ch_2.relative_sort([3, 0, 5, 0, 2, 1, 4, 1, 1], [1, 0, 3, 2]), + [1, 1, 1, 0, 0, 3, 2, 4, 5] + ) + + +if __name__ == '__main__': + unittest.main() |
