diff options
| author | Simon Green <mail@simon.green> | 2024-08-25 21:14:42 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2024-08-25 21:14:42 +1000 |
| commit | 65a75b949d58591e22bad80c8e3c33b77cd946f9 (patch) | |
| tree | 9c37b5ccc5335fd0cc7ba2edf4879c6570aecf9d /challenge-283 | |
| parent | 1ec5389949cde3e7cae71c1c6298759adc5b9b1b (diff) | |
| download | perlweeklychallenge-club-65a75b949d58591e22bad80c8e3c33b77cd946f9.tar.gz perlweeklychallenge-club-65a75b949d58591e22bad80c8e3c33b77cd946f9.tar.bz2 perlweeklychallenge-club-65a75b949d58591e22bad80c8e3c33b77cd946f9.zip | |
sgreen solutions to challenge 283
Diffstat (limited to 'challenge-283')
| -rw-r--r-- | challenge-283/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-283/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-283/sgreen/perl/ch-1.pl | 29 | ||||
| -rwxr-xr-x | challenge-283/sgreen/perl/ch-2.pl | 28 | ||||
| -rwxr-xr-x | challenge-283/sgreen/python/ch-1.py | 31 | ||||
| -rwxr-xr-x | challenge-283/sgreen/python/ch-2.py | 28 | ||||
| -rwxr-xr-x | challenge-283/sgreen/python/test.py | 21 |
7 files changed, 140 insertions, 2 deletions
diff --git a/challenge-283/sgreen/README.md b/challenge-283/sgreen/README.md index eff69133e3..a1791d3a25 100644 --- a/challenge-283/sgreen/README.md +++ b/challenge-283/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 282 +# The Weekly Challenge 283 -Blog: [Good keys](https://dev.to/simongreennet/good-keys-2elk) +Blog: [Weekly Challenge 283](https://dev.to/simongreennet/weekly-challenge-283-102h) diff --git a/challenge-283/sgreen/blog.txt b/challenge-283/sgreen/blog.txt new file mode 100644 index 0000000000..883e2d2ae4 --- /dev/null +++ b/challenge-283/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-283-102h
\ No newline at end of file diff --git a/challenge-283/sgreen/perl/ch-1.pl b/challenge-283/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..7a69bc8d06 --- /dev/null +++ b/challenge-283/sgreen/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/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 $int (@ints) { + $freq{$int}++; + } + + # Get the integers that appear only once + my @only_once = grep { $freq{$_} == 1 } keys %freq; + + if ( $#only_once == 0 ) { + say $only_once[0]; + } + elsif ( $#only_once == -1 ) { + say 'No values only appear once'; + } + else { + say 'More than one value appears once'; + } +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-283/sgreen/perl/ch-2.pl b/challenge-283/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..709644de65 --- /dev/null +++ b/challenge-283/sgreen/perl/ch-2.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 $int (@ints) { + $freq{$int}++; + } + + while ( my ( $idx, $value ) = each(@ints) ) { + # Check the index only appear value number of times + if ( ( $freq{$idx} // 0 ) != $value ) { + say 'false'; + return; + } + } + + # Checks pass + say 'true'; + +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-283/sgreen/python/ch-1.py b/challenge-283/sgreen/python/ch-1.py new file mode 100755 index 0000000000..7f5ed66cd7 --- /dev/null +++ b/challenge-283/sgreen/python/ch-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +from collections import Counter +import sys + + +def unique_number(ints: list) -> int: + # Calculate the frequency of each integer + freq = Counter(ints) + + # Get the integers that appear only once + only_once = [i for i in freq if freq[i] == 1] + + if len(only_once) == 1: + return only_once[0] + + if len(only_once) == 0: + raise ValueError('No values only appear once') + + raise ValueError('More than one value appears once') + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = unique_number(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-283/sgreen/python/ch-2.py b/challenge-283/sgreen/python/ch-2.py new file mode 100755 index 0000000000..978ad41f6d --- /dev/null +++ b/challenge-283/sgreen/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +from collections import Counter +import sys + + +def digit_count_value(ints: list) -> bool: + # Calculate the frequency of each integer + freq = Counter(ints) + + for idx, value in enumerate(ints): + # Check the index only appear value number of times + if freq[idx] != value: + return False + + # Checks pass + return True + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = digit_count_value(array) + print('true' if result else 'false') + + +if __name__ == '__main__': + main() diff --git a/challenge-283/sgreen/python/test.py b/challenge-283/sgreen/python/test.py new file mode 100755 index 0000000000..9073c9d954 --- /dev/null +++ b/challenge-283/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.unique_number([3, 3, 1]), 1) + self.assertEqual(ch_1.unique_number([3, 2, 4, 2, 4]), 3) + self.assertEqual(ch_1.unique_number([1]), 1) + self.assertEqual(ch_1.unique_number([4, 3, 1, 1, 1, 4]), 3) + + def test_ch_2(self): + self.assertTrue(ch_2.digit_count_value([1, 2, 1, 0])) + self.assertFalse(ch_2.digit_count_value([0, 3, 0])) + + +if __name__ == '__main__': + unittest.main() |
