diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2024-09-02 15:40:05 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2024-09-02 15:40:05 +0800 |
| commit | 0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8 (patch) | |
| tree | d963a7fe57782d1d25580ea05c96f969768e8067 /challenge-283 | |
| parent | 30a42138e1d8bdd2f174e06fb18515ced0b2b241 (diff) | |
| parent | 0512711fccf91c731495f1dd901349cc900ec299 (diff) | |
| download | perlweeklychallenge-club-0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8.tar.gz perlweeklychallenge-club-0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8.tar.bz2 perlweeklychallenge-club-0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-283')
| -rwxr-xr-x | challenge-283/andrezgz/perl/ch-1.pl | 36 | ||||
| -rwxr-xr-x | challenge-283/andrezgz/perl/ch-2.pl | 46 | ||||
| -rw-r--r-- | challenge-283/conor-hoekstra/bqn/ch-1.bqn | 10 | ||||
| -rw-r--r-- | challenge-283/conor-hoekstra/bqn/ch-2.bqn | 8 | ||||
| -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 |
11 files changed, 240 insertions, 2 deletions
diff --git a/challenge-283/andrezgz/perl/ch-1.pl b/challenge-283/andrezgz/perl/ch-1.pl new file mode 100755 index 0000000000..9576d5f48e --- /dev/null +++ b/challenge-283/andrezgz/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/ +# TASK #1 > Unique Number +# +# You are given an array of integers, @ints, where every elements appears more than once except one element. +# +# Write a script to find the one element that appears exactly one time. +# +# Example 1 +# Input: @ints = (3, 3, 1) +# Output: 1 +# Example 2 +# Input: @ints = (3, 2, 4, 2, 4) +# Output: 3 +# Example 3 +# Input: @ints = (1) +# Output: 1 +# Example 4 +# Input: @ints = (4, 3, 1, 1, 1, 4) +# Output: 3 + +use strict; +use warnings; +use feature 'say'; + +my @ints = @ARGV; +my %seen; +$seen{$_}++ for @ints; + +say grep { $seen{$_} == 1 } @ints; + +__END__ + +$ ./ch-1.pl 3 1 1 3 5 2 4 2 4 +5 diff --git a/challenge-283/andrezgz/perl/ch-2.pl b/challenge-283/andrezgz/perl/ch-2.pl new file mode 100755 index 0000000000..17b744d3e9 --- /dev/null +++ b/challenge-283/andrezgz/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/ +# Task #2 > Digit Count Value +# +# You are given an array of positive integers, @ints. +# Write a script to return true if for every index i in the range 0 <= i < size of array, +# the digit i occurs exactly the $ints[$i] times in the given array otherwise return false. +# +# Example 1 +# Input: @ints = (1, 2, 1, 0) +# Ouput: true +# +# $ints[0] = 1, the digit 0 occurs exactly 1 time. +# $ints[1] = 2, the digit 1 occurs exactly 2 times. +# $ints[2] = 1, the digit 2 occurs exactly 1 time. +# $ints[3] = 0, the digit 3 occurs 0 time. +# +# Example 2 +# Input: @ints = (0, 3, 0) +# Ouput: false +# +# $ints[0] = 0, the digit 0 occurs 2 times rather than 0 time. +# $ints[1] = 3, the digit 1 occurs 0 time rather than 3 times. +# $ints[2] = 0, the digit 2 occurs exactly 0 time. + +use strict; +use warnings; +use feature 'say'; + +my @ints = @ARGV; +say digit_count_match(@ints); +exit 0; + +sub digit_count_match { + my @ints = @_; + + my %seen; + $seen{$_}++ for @ints; + + for my $i (0..$#ints) { + next if $ints[$i] == ($seen{$i} // 0); + return 'false'; + } + return 'true'; +} diff --git a/challenge-283/conor-hoekstra/bqn/ch-1.bqn b/challenge-283/conor-hoekstra/bqn/ch-1.bqn new file mode 100644 index 0000000000..b2d34a6efe --- /dev/null +++ b/challenge-283/conor-hoekstra/bqn/ch-1.bqn @@ -0,0 +1,10 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/283-1.bqn + +UniqueNumber ← ⊑·/1=·≠¨⊔ + +# Tests +•Show UniqueNumber ⟨3, 3, 1⟩ # 1 +•Show UniqueNumber ⟨3, 2, 4, 2, 4⟩ # 3 +•Show UniqueNumber ⟨1⟩ # 1 +•Show UniqueNumber ⟨4, 3, 1, 1, 1, 4⟩ # 3 diff --git a/challenge-283/conor-hoekstra/bqn/ch-2.bqn b/challenge-283/conor-hoekstra/bqn/ch-2.bqn new file mode 100644 index 0000000000..492b67033b --- /dev/null +++ b/challenge-283/conor-hoekstra/bqn/ch-2.bqn @@ -0,0 +1,8 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/283-2.bqn + +DigitCountValue ← (+˝(=⌜⟜↕≠))⊸≡ + +# Tests +•Show DigitCountValue ⟨1, 2, 1, 0⟩ # 1 +•Show DigitCountValue ⟨0, 3, 0⟩ # 0 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() |
