From 68d851f5173609afdb1ae132ebca62e0f3cca358 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Tue, 12 Mar 2024 01:21:39 -0400 Subject: Challenge 260 solutions by Packy Anderson * Raku * Perl * Python 1 Blog post --- challenge-260/packy-anderson/README.md | 2 +- challenge-260/packy-anderson/blog.txt | 1 + challenge-260/packy-anderson/perl/ch-1.pl | 32 ++++++++++++++++++++++++ challenge-260/packy-anderson/perl/ch-2.pl | 38 +++++++++++++++++++++++++++++ challenge-260/packy-anderson/python/ch-1.py | 32 ++++++++++++++++++++++++ challenge-260/packy-anderson/python/ch-2.py | 31 +++++++++++++++++++++++ challenge-260/packy-anderson/raku/ch-1.raku | 32 ++++++++++++++++++++++++ challenge-260/packy-anderson/raku/ch-2.raku | 35 ++++++++++++++++++++++++++ 8 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 challenge-260/packy-anderson/blog.txt create mode 100755 challenge-260/packy-anderson/perl/ch-1.pl create mode 100755 challenge-260/packy-anderson/perl/ch-2.pl create mode 100755 challenge-260/packy-anderson/python/ch-1.py create mode 100755 challenge-260/packy-anderson/python/ch-2.py create mode 100755 challenge-260/packy-anderson/raku/ch-1.raku create mode 100755 challenge-260/packy-anderson/raku/ch-2.raku diff --git a/challenge-260/packy-anderson/README.md b/challenge-260/packy-anderson/README.md index fb3836f0bb..2c6ea1d7ca 100644 --- a/challenge-260/packy-anderson/README.md +++ b/challenge-260/packy-anderson/README.md @@ -16,4 +16,4 @@ ## Blog Post -[Ba-a-nking Day! Ba-a-nking Day! That's all I really wanted to say...](https://packy.dardan.com/b/JC) +[Unique Dictionary Occurrences are Rank](https://packy.dardan.com/b/J_) diff --git a/challenge-260/packy-anderson/blog.txt b/challenge-260/packy-anderson/blog.txt new file mode 100644 index 0000000000..1b997751dc --- /dev/null +++ b/challenge-260/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/J_ \ No newline at end of file diff --git a/challenge-260/packy-anderson/perl/ch-1.pl b/challenge-260/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..18ae2e21e0 --- /dev/null +++ b/challenge-260/packy-anderson/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +use v5.38; + +sub uniqueOccurrences(@ints) { + my %counts; + foreach my $i ( @ints ) { + # count how many time each int occurs + $counts{$i}++; + } + my %seen; + while ( my($i, $c) = each %counts ) { + # if we've seen this count before, return 0 + return 0 if exists $seen{$c}; + $seen{$c} = $i; + } + # each count was unique + return 1; +} + +sub solution(@ints) { + say 'Input: @ints = (' . join(', ', @ints) . ')'; + say 'Output: ' . uniqueOccurrences(@ints); +} + +say "Example 1:"; +solution(1,2,2,1,1,3); + +say "\nExample 2:"; +solution(1,2,3); + +say "\nExample 3:"; +solution(-2,0,1,-2,1,1,0,1,-2,9); \ No newline at end of file diff --git a/challenge-260/packy-anderson/perl/ch-2.pl b/challenge-260/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..d7dec56134 --- /dev/null +++ b/challenge-260/packy-anderson/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +use v5.38; + +use Algorithm::Combinatorics qw( permutations ); +use List::Util qw( uniq ); + +sub dictionaryRank($word) { + # split the string into an array of characters + my @letters = split //, $word; + + # find the permutations of the letters + my @perms; + foreach my $l ( permutations(\@letters) ) { + push @perms, join('', @$l); + } + + # find where in the sorted list of + # UNIQUE permutations the word is + my $rank = 1; + foreach my $p ( sort { $a cmp $b } uniq @perms ) { + return $rank if $p eq $word; + $rank++; + } +} + +sub solution($word) { + say q{Input: $word = '} . $word . q{'}; + say 'Output: ' . dictionaryRank($word); +} + +say "Example 1:"; +solution('CAT'); + +say "\nExample 2:"; +solution('GOOGLE'); + +say "\nExample 3:"; +solution('SECRET'); \ No newline at end of file diff --git a/challenge-260/packy-anderson/python/ch-1.py b/challenge-260/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..8f185f05bf --- /dev/null +++ b/challenge-260/packy-anderson/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +from collections import Counter + +def uniqueOccurrences(ints): + counts = Counter() + for i in ints: + # count how many time each int occurs + counts[i] += 1 + seen = {} + for i, c in counts.items(): + # if we've seen this count before, return 0 + if c in seen: return 0 + seen[c] = i + # each count was unique + return 1 + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + print(f'Output: {uniqueOccurrences(ints)}') + +print('Example 1:') +solution([1,2,2,1,1,3]) + +print('\nExample 2:') +solution([1,2,3]) + +print('\nExample 3:') +solution([-2,0,1,-2,1,1,0,1,-2,9]) \ No newline at end of file diff --git a/challenge-260/packy-anderson/python/ch-2.py b/challenge-260/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..4502f78a3c --- /dev/null +++ b/challenge-260/packy-anderson/python/ch-2.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +from itertools import permutations + +def dictionaryRank(word): + # we don't need to split the string, because + # permutations() will accept a string as a parameter + # + # set() produces a set of unique elements + # + # sorted() returns a sorted list + perms = sorted( + set([ ''.join(l) for l in permutations(word) ]) + ) + rank = 1 + for p in perms: + if p == word: return rank + rank += 1 + +def solution(word): + print(f"Input: $word = '{word}'") + print(f'Output: {dictionaryRank(word)}') + +print('Example 1:') +solution('CAT') + +print('\nExample 2:') +solution('GOOGLE') + +print('\nExample 3:') +solution('SECRET') \ No newline at end of file diff --git a/challenge-260/packy-anderson/raku/ch-1.raku b/challenge-260/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..bee938de90 --- /dev/null +++ b/challenge-260/packy-anderson/raku/ch-1.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku +use v6; + +sub uniqueOccurrences(@ints) { + my %counts; + for @ints -> $i { + # count how many time each int occurs + %counts{$i}++; + } + my %seen; + for %counts.kv -> $i, $c { + # if we've seen this count before, return 0 + return 0 if %seen{$c}:exists; + %seen{$c} = $i; + } + # each count was unique + return 1; +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + say 'Output: ' ~ uniqueOccurrences(@ints); +} + +say "Example 1:"; +solution([1,2,2,1,1,3]); + +say "\nExample 2:"; +solution([1,2,3]); + +say "\nExample 3:"; +solution([-2,0,1,-2,1,1,0,1,-2,9]); \ No newline at end of file diff --git a/challenge-260/packy-anderson/raku/ch-2.raku b/challenge-260/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..b3e854e5e1 --- /dev/null +++ b/challenge-260/packy-anderson/raku/ch-2.raku @@ -0,0 +1,35 @@ +#!/usr/bin/env raku +use v6; + +sub dictionaryRank($word) { + # split the string into an array of characters + my @letters = $word.split('', :skip-empty); + + # find the permutations of the letters + my @perms; + for @letters.permutations -> @l { + @perms.append(@l.join('')); + } + + # find where in the sorted list of + # UNIQUE permutations the word is + my $rank = 1; + for @perms.unique.sort -> $p { + return $rank if $p eq $word; + $rank++; + } +} + +sub solution($word) { + say q{Input: $word = '} ~ $word ~ q{'}; + say 'Output: ' ~ dictionaryRank($word); +} + +say "Example 1:"; +solution('CAT'); + +say "\nExample 2:"; +solution('GOOGLE'); + +say "\nExample 3:"; +solution('SECRET'); \ No newline at end of file -- cgit