diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-17 14:11:41 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-17 14:11:41 +0000 |
| commit | 7177fae93dcaab8a8838b3af1c450e95b70ff4af (patch) | |
| tree | 1d285cabd2285b5b43fa512cc085c5d566558755 | |
| parent | 0292dfba8214f337b04484366cd007cd26f0b4e5 (diff) | |
| parent | 07699eac47d83850fec02e87c69cffff471236ac (diff) | |
| download | perlweeklychallenge-club-7177fae93dcaab8a8838b3af1c450e95b70ff4af.tar.gz perlweeklychallenge-club-7177fae93dcaab8a8838b3af1c450e95b70ff4af.tar.bz2 perlweeklychallenge-club-7177fae93dcaab8a8838b3af1c450e95b70ff4af.zip | |
Merge pull request #9754 from simongreen-net/master
Simon's solution to challenge 260
| -rw-r--r-- | challenge-259/sgreen/README.md | 3 | ||||
| -rw-r--r-- | challenge-260/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-260/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-260/sgreen/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-260/sgreen/perl/ch-2.pl | 31 | ||||
| -rwxr-xr-x | challenge-260/sgreen/python/ch-1.py | 41 | ||||
| -rwxr-xr-x | challenge-260/sgreen/python/ch-2.py | 41 | ||||
| -rwxr-xr-x | challenge-260/sgreen/python/test.py | 21 |
8 files changed, 167 insertions, 5 deletions
diff --git a/challenge-259/sgreen/README.md b/challenge-259/sgreen/README.md deleted file mode 100644 index 72cd7411bb..0000000000 --- a/challenge-259/sgreen/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# The Weekly Challenge 258 - -Blog: [Weekly Challenge 258](https://dev.to/simongreennet/weekly-challenge-258-1mg) diff --git a/challenge-260/sgreen/README.md b/challenge-260/sgreen/README.md index 72cd7411bb..068010e562 100644 --- a/challenge-260/sgreen/README.md +++ b/challenge-260/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 258 +# The Weekly Challenge 260 -Blog: [Weekly Challenge 258](https://dev.to/simongreennet/weekly-challenge-258-1mg) +Blog: [Weekly Challenge 260](https://dev.to/simongreennet/counting-and-ranking-3jjb) diff --git a/challenge-260/sgreen/blog.txt b/challenge-260/sgreen/blog.txt new file mode 100644 index 0000000000..bac17ec0a6 --- /dev/null +++ b/challenge-260/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/counting-and-ranking-3jjb
\ No newline at end of file diff --git a/challenge-260/sgreen/perl/ch-1.pl b/challenge-260/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..7985afc49e --- /dev/null +++ b/challenge-260/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 (@ints) { + # Calculate the frequency of each integer + my %freq = (); + foreach my $i (@ints) { + $freq{$i}++; + } + + # Return if we have seen a frequency already + my %seen = (); + foreach my $i ( values(%freq) ) { + if ( exists $seen{$i} ) { + say '0'; + return; + } + + $seen{$i} = 1; + } + + # We have a unique orrurrence list + say '1'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-260/sgreen/perl/ch-2.pl b/challenge-260/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..dcdbfcfc61 --- /dev/null +++ b/challenge-260/sgreen/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use Math::Combinatorics; + +sub main ($word) { + # Convert to lower case, and sort the letters alphabetically + my @letters = sort ( split //, lc($word) ); + my $count = 0; + + # Go through each sorted unique permutation and count where <= $word + my $c = Math::Combinatorics->new( + data => \@letters, + frequency => [ (1) x length($word) ] + ); + + while ( my @perm = $c->next_string() ) { + if ( join( '', @perm ) le $word ) { + $count++; + } + } + + # Return the rank + say $count; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-260/sgreen/python/ch-1.py b/challenge-260/sgreen/python/ch-1.py new file mode 100755 index 0000000000..90ba9f5b09 --- /dev/null +++ b/challenge-260/sgreen/python/ch-1.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +from collections import defaultdict +import sys + + +def uniq_occurrences(ints: list) -> bool: + """Check the frequency of integers are unique + + Args: + ints (list): The integers + + Returns: + bool: Whether the frequency of the list is unique + """ + + # Calculate the frequency of each integer + freq = defaultdict(int) + for i in ints: + freq[i] += 1 + + # Return if we have seen a frequency already + seen = {} + for i in freq.values(): + if i in seen: + return False + seen[i] = 1 + + # We have a unique orrurrence list + return True + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = uniq_occurrences(array) + print('1' if result else '0') + + +if __name__ == '__main__': + main() diff --git a/challenge-260/sgreen/python/ch-2.py b/challenge-260/sgreen/python/ch-2.py new file mode 100755 index 0000000000..fa649c6b28 --- /dev/null +++ b/challenge-260/sgreen/python/ch-2.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +from more_itertools import distinct_permutations +import sys + + +def dictionary_rank(word: str) -> int: + """Calculate the position on the dictionary if all permutations of letters + in the given word were valid. + + Args: + word (str): The input word + + Returns: + int: The position + """ + # Convert to lower case, and sort the letters alphabetically + tuple_word = tuple(word.lower()) + letters = sorted(tuple_word) + count = 0 + + # Go through each sorted unique permutation until we find the word we want + for perm in distinct_permutations(letters): + count += 1 + if perm == tuple_word: + break + else: + raise ValueError('Cannot find position') + + # Return the rank + return count + + +def main(): + # Convert input into integers + result = dictionary_rank(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-260/sgreen/python/test.py b/challenge-260/sgreen/python/test.py new file mode 100755 index 0000000000..1a7ed7de1d --- /dev/null +++ b/challenge-260/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.assertTrue(ch_1.uniq_occurrences([1, 2, 2, 1, 1, 3])) + self.assertFalse(ch_1.uniq_occurrences([1, 2, 3])) + self.assertTrue(ch_1.uniq_occurrences([-2, 0, 1, -2, 1, 1, 0, 1, -2, 9])) + + def test_ch_2(self): + self.assertEqual(ch_2.dictionary_rank('CAT'), 3) + self.assertEqual(ch_2.dictionary_rank('GOOGLE'), 88) + self.assertEqual(ch_2.dictionary_rank('SECRET'), 255) + + +if __name__ == '__main__': + unittest.main() |
