aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-17 13:39:36 +0000
committerGitHub <noreply@github.com>2024-03-17 13:39:36 +0000
commitbe9ae7459b347030a14bf444ebdecda1cb5a3983 (patch)
treeffe1e12bc00bb39d3bb58672ac11f5cb3e3f7287
parent62e7fc3bb85a74125663f4fbd0a5911f6f30c81f (diff)
parent630135a3c453711b03bfe44976daf1a53cf8a57f (diff)
downloadperlweeklychallenge-club-be9ae7459b347030a14bf444ebdecda1cb5a3983.tar.gz
perlweeklychallenge-club-be9ae7459b347030a14bf444ebdecda1cb5a3983.tar.bz2
perlweeklychallenge-club-be9ae7459b347030a14bf444ebdecda1cb5a3983.zip
Merge pull request #9750 from LubosKolouch/master
feat(challenge-260/lubos-kolouch/perl,python/): Code and style cleanups for challenge 260
-rw-r--r--challenge-260/lubos-kolouch/perl/ch-1.pl36
-rw-r--r--challenge-260/lubos-kolouch/perl/ch-2.pl39
-rw-r--r--challenge-260/lubos-kolouch/python/ch-1.py36
-rw-r--r--challenge-260/lubos-kolouch/python/ch-2.py54
-rw-r--r--challenge-260/lubos-kolouch/raku/ch-1.raku29
-rw-r--r--challenge-260/lubos-kolouch/raku/ch-2.raku26
6 files changed, 220 insertions, 0 deletions
diff --git a/challenge-260/lubos-kolouch/perl/ch-1.pl b/challenge-260/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..0d8d759c79
--- /dev/null
+++ b/challenge-260/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,36 @@
+package ch1;
+use strict;
+use warnings;
+
+use Data::Dumper;
+
+sub unique_occurrences {
+ my @ints = @_;
+ my %count;
+
+ # Count occurrences of each value in the array
+ $count{$_}++ for @ints;
+
+ # Check if the number of occurrences is unique
+ my %occurrences_count;
+ foreach my $value ( values %count ) {
+ $occurrences_count{$value}++;
+ }
+
+ # Return 1 if all occurrences are unique, 0 otherwise
+ return ( scalar keys %occurrences_count == scalar values %count ) ? 1 : 0;
+}
+
+# Example 1
+my @ints1 = ( 1, 2, 2, 1, 1, 3 );
+print "Example 1: " . unique_occurrences(@ints1) . "\n";
+
+# Example 2
+my @ints2 = ( 1, 2, 3 );
+print "Example 2: " . unique_occurrences(@ints2) . "\n";
+
+# Example 3
+my @ints3 = ( -2, 0, 1, -2, 1, 1, 0, 1, -2, 9 );
+print "Example 3: " . unique_occurrences(@ints3) . "\n";
+
+1;
diff --git a/challenge-260/lubos-kolouch/perl/ch-2.pl b/challenge-260/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..fc13687a33
--- /dev/null
+++ b/challenge-260/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,39 @@
+package ch2;
+use strict;
+use warnings;
+use List::Util qw(reduce);
+
+sub factorial {
+ my ($n) = @_;
+ return reduce { $a * $b } 1, 1 .. $n;
+}
+
+sub dictionary_rank {
+ my ($word) = @_;
+ my $length = length($word);
+ my %char_counts;
+ $char_counts{$_}++ for split //, $word;
+ my $rank = 1;
+
+ for my $i ( 0 .. $length - 1 ) {
+ my $char = substr( $word, $i, 1 );
+ my $smaller_chars = 0;
+ $smaller_chars += $char_counts{$_}
+ for grep { $_ lt $char } keys %char_counts;
+
+ $rank +=
+ $smaller_chars * factorial( $length - $i - 1 ) / reduce { $a * $b } 1,
+ map { factorial($_) } values %char_counts;
+
+ $char_counts{$char}--;
+ delete $char_counts{$char} if $char_counts{$char} == 0;
+ }
+
+ return $rank;
+}
+
+print dictionary_rank('CAT'), "\\n";
+print dictionary_rank('GOOGLE'), "\\n";
+print dictionary_rank('SECRET'), "\\n";
+
+1;
diff --git a/challenge-260/lubos-kolouch/python/ch-1.py b/challenge-260/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..7313d79c13
--- /dev/null
+++ b/challenge-260/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,36 @@
+from typing import List
+
+
+def unique_occurrences(ints: list[int]) -> int:
+ """
+ Checks if the number of occurrences of each value
+ in the given array is unique.
+
+ Parameters:
+ - ints (List[int]): A list of integers.
+
+ Returns:
+ - int: 1 if all occurrences are unique, 0 otherwise.
+ """
+ # Count the occurrences of each integer
+ occurrence_counts: dict[int, int] = {}
+ for num in ints:
+ if num in occurrence_counts:
+ occurrence_counts[num] += 1
+ else:
+ occurrence_counts[num] = 1
+
+ # Check if all occurrence counts are unique
+ counts = list(occurrence_counts.values())
+ return 1 if len(counts) == len(set(counts)) else 0
+
+
+# Test cases
+def test_unique_occurrences():
+ assert unique_occurrences([1, 2, 2, 1, 1, 3]) == 1
+ assert unique_occurrences([1, 2, 3]) == 0
+ assert unique_occurrences([-2, 0, 1, -2, 1, 1, 0, 1, -2, 9]) == 1
+
+
+# Run tests
+test_unique_occurrences()
diff --git a/challenge-260/lubos-kolouch/python/ch-2.py b/challenge-260/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..08e5c1fce9
--- /dev/null
+++ b/challenge-260/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,54 @@
+from functools import reduce
+from math import factorial
+from operator import mul
+from typing import Dict
+
+
+def calculate_factorials(n: int) -> dict[int, int]:
+ """Calculate factorials from 0 to n."""
+ factorials = {0: 1}
+ for i in range(1, n + 1):
+ factorials[i] = factorials[i - 1] * i
+ return factorials
+
+
+def count_char_occurrences(word: str) -> dict[str, int]:
+ """Count occurrences of each character in a word."""
+ counts: dict[str, int] = {}
+ for char in word:
+ counts[char] = counts.get(char, 0) + 1
+ return counts
+
+
+def product(iterable):
+ """Compute the product of an iterable of numbers."""
+ return reduce(mul, iterable, 1)
+
+
+def dictionary_rank(word: str) -> int:
+ """Compute the dictionary rank of the given word."""
+ length = len(word)
+ factorials = calculate_factorials(length)
+ char_counts = count_char_occurrences(word)
+ rank = 1
+
+ for i, char in enumerate(word):
+ smaller_chars = sum(count for c, count in char_counts.items() if c < char)
+ rank += (
+ smaller_chars
+ * factorials[length - i - 1]
+ // product(factorials[c] for c in char_counts.values())
+ )
+
+ # Decrement the count for the current character
+ char_counts[char] -= 1
+ if char_counts[char] == 0:
+ del char_counts[char]
+
+ return rank
+
+
+# Test cases
+assert dictionary_rank("CAT") == 3
+assert dictionary_rank("GOOGLE") == 88
+assert dictionary_rank("SECRET") == 255
diff --git a/challenge-260/lubos-kolouch/raku/ch-1.raku b/challenge-260/lubos-kolouch/raku/ch-1.raku
new file mode 100644
index 0000000000..68b11572a1
--- /dev/null
+++ b/challenge-260/lubos-kolouch/raku/ch-1.raku
@@ -0,0 +1,29 @@
+sub unique_occurrences(@ints) {
+ my %count;
+
+ # Count occurrences of each value in the array
+ for @ints -> $num {
+ %count{$num}++;
+ }
+
+ # Check if the number of occurrences is unique
+ my %occurrences_count;
+ for %count.values -> $value {
+ %occurrences_count{$value}++;
+ }
+
+ # Return 1 if all occurrences are unique, 0 otherwise
+ return %occurrences_count.keys == %count.values.elems ?? 1 !! 0;
+}
+
+# Example 1
+my @ints1 = (1,2,2,1,1,3);
+say "Example 1: ", unique_occurrences(@ints1);
+
+# Example 2
+my @ints2 = (1,2,3);
+say "Example 2: ", unique_occurrences(@ints2);
+
+# Example 3
+my @ints3 = (-2,0,1,-2,1,1,0,1,-2,9);
+say "Example 3: ", unique_occurrences(@ints3);
diff --git a/challenge-260/lubos-kolouch/raku/ch-2.raku b/challenge-260/lubos-kolouch/raku/ch-2.raku
new file mode 100644
index 0000000000..213f0538a6
--- /dev/null
+++ b/challenge-260/lubos-kolouch/raku/ch-2.raku
@@ -0,0 +1,26 @@
+sub factorial(Int $n) {
+ return [*] 1..$n;
+}
+
+sub dictionary-rank(Str $word) {
+ my $length = $word.chars;
+ my %char-counts = $word.comb.Bag;
+ my $rank = 1;
+
+ for 0..$length-1 -> $i {
+ my $char = $word.substr($i, 1);
+ my $smaller-chars = [+] %char-counts.pairs.grep({ .key lt $char }).map(*.value);
+
+ $rank += $smaller-chars * factorial($length - $i - 1)
+ / [*] %char-counts.values.map({ factorial($_) });
+
+ %char-counts{$char}--;
+ %char-counts{$char}:delete if %char-counts{$char} == 0;
+ }
+
+ return $rank;
+}
+
+say dictionary-rank('CAT');
+say dictionary-rank('GOOGLE');
+say dictionary-rank('SECRET');