diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2024-03-16 15:31:39 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2024-03-16 15:31:39 +0100 |
| commit | c332d451e86cc8bd09c8e9746359a3085fcc3a0e (patch) | |
| tree | caa34bbdd909983ce8ade56620c500e4d8593115 | |
| parent | 5daec0294693d536d10e8ef9aea976f659a17165 (diff) | |
| download | perlweeklychallenge-club-c332d451e86cc8bd09c8e9746359a3085fcc3a0e.tar.gz perlweeklychallenge-club-c332d451e86cc8bd09c8e9746359a3085fcc3a0e.tar.bz2 perlweeklychallenge-club-c332d451e86cc8bd09c8e9746359a3085fcc3a0e.zip | |
feat(challenge-260/lubos-kolouch/perl,-python,-raku/): Challenge 260 LK Perl Python Raku
| -rw-r--r-- | challenge-260/lubos-kolouch/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-260/lubos-kolouch/perl/ch-2.pl | 36 | ||||
| -rw-r--r-- | challenge-260/lubos-kolouch/python/ch-1.py | 36 | ||||
| -rw-r--r-- | challenge-260/lubos-kolouch/python/ch-2.py | 54 | ||||
| -rw-r--r-- | challenge-260/lubos-kolouch/raku/ch-1.raku | 29 | ||||
| -rw-r--r-- | challenge-260/lubos-kolouch/raku/ch-2.raku | 26 |
6 files changed, 213 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..791a3dc77c --- /dev/null +++ b/challenge-260/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,32 @@ +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"; 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..a33f112bb3 --- /dev/null +++ b/challenge-260/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,36 @@ +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"; 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'); |
