aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2024-03-17 18:20:49 +1100
committerSimon Green <mail@simon.green>2024-03-17 18:20:49 +1100
commit07699eac47d83850fec02e87c69cffff471236ac (patch)
treecd9acbdbfd5183c3d037fc5e306c4880b02edc2e
parentaad9041674e6575f6c24925854d6353f2098a117 (diff)
downloadperlweeklychallenge-club-07699eac47d83850fec02e87c69cffff471236ac.tar.gz
perlweeklychallenge-club-07699eac47d83850fec02e87c69cffff471236ac.tar.bz2
perlweeklychallenge-club-07699eac47d83850fec02e87c69cffff471236ac.zip
Simon's solution to challenge 260
-rw-r--r--challenge-260/sgreen/README.md4
-rw-r--r--challenge-260/sgreen/blog.txt1
-rwxr-xr-xchallenge-260/sgreen/perl/ch-1.pl30
-rwxr-xr-xchallenge-260/sgreen/perl/ch-2.pl31
-rwxr-xr-xchallenge-260/sgreen/python/ch-1.py41
-rwxr-xr-xchallenge-260/sgreen/python/ch-2.py41
-rwxr-xr-xchallenge-260/sgreen/python/test.py21
7 files changed, 167 insertions, 2 deletions
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()