aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-09-11 03:29:30 +0100
committerGitHub <noreply@github.com>2023-09-11 03:29:30 +0100
commit64219e69da579fc6e25bb7d072ed517cc9ca0374 (patch)
tree605ff07e356e4b3eee834aa27a78c5e4a94c3d32
parentbe35f7cb0ebd77c643e594fde079bf4cddb27037 (diff)
parent52b326741d28482197a06007b9afbbc7a0294a6c (diff)
downloadperlweeklychallenge-club-64219e69da579fc6e25bb7d072ed517cc9ca0374.tar.gz
perlweeklychallenge-club-64219e69da579fc6e25bb7d072ed517cc9ca0374.tar.bz2
perlweeklychallenge-club-64219e69da579fc6e25bb7d072ed517cc9ca0374.zip
Merge pull request #8684 from jaldhar/challenge-233
Challenge 233 by Jaldhar H. Vyas.
-rw-r--r--challenge-233/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-233/jaldhar-h-vyas/perl/ch-1.pl37
-rwxr-xr-xchallenge-233/jaldhar-h-vyas/perl/ch-2.pl23
-rwxr-xr-xchallenge-233/jaldhar-h-vyas/raku/ch-1.sh3
-rwxr-xr-xchallenge-233/jaldhar-h-vyas/raku/ch-2.raku15
5 files changed, 79 insertions, 0 deletions
diff --git a/challenge-233/jaldhar-h-vyas/blog.txt b/challenge-233/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..3c7eaab0d1
--- /dev/null
+++ b/challenge-233/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2023/09/perl_weekly_challenge_week_233.html
diff --git a/challenge-233/jaldhar-h-vyas/perl/ch-1.pl b/challenge-233/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..e318fd3e45
--- /dev/null
+++ b/challenge-233/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+use 5.030;
+use warnings;
+
+sub combinations {
+ my @list = @{$_[0]};
+ my $length = $_[1];
+
+ if ($length <= 1) {
+ return map [$_], @list;
+ }
+
+ my @combos;
+
+ for (my $i = 0; $i + $length <= scalar @list; $i++) {
+ my $val = $list[$i];
+ my @rest = @list[$i + 1 .. $#list];
+ for my $c (combinations(\@rest, $length - 1)) {
+ push @combos, [$val, @{$c}] ;
+ }
+ }
+
+ return @combos;
+}
+
+sub unique {
+ my ($arr) = @_;
+ my %unique;
+ for my $elem (@{$arr}) {
+ $unique{$elem}++;
+ }
+
+ return keys %unique;
+}
+
+say scalar grep { $_->[0] eq $_->[1] }
+ combinations([ map { join q{}, sort (unique([ split //, $_ ])); } @ARGV], 2);
diff --git a/challenge-233/jaldhar-h-vyas/perl/ch-2.pl b/challenge-233/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..cab8bb81ba
--- /dev/null
+++ b/challenge-233/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+use 5.030;
+use warnings;
+
+sub classify {
+ my %count;
+
+ for my $n (@_) {
+ push @{$count{$n}}, $n;
+ }
+ return %count;
+}
+
+my %freq = classify(@ARGV);
+
+say q{(},
+ (
+ join q{,},
+ map { @{$freq{$_}} }
+ sort { scalar @{$freq{$a}} <=> scalar @{$freq{$b}} || $b <=> $a }
+ keys %freq
+ ),
+q{)};
diff --git a/challenge-233/jaldhar-h-vyas/raku/ch-1.sh b/challenge-233/jaldhar-h-vyas/raku/ch-1.sh
new file mode 100755
index 0000000000..6988c8a42a
--- /dev/null
+++ b/challenge-233/jaldhar-h-vyas/raku/ch-1.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+raku -e '@*ARGS.map({$_.comb.unique.sort.join}).combinations(2).grep({@$_[0]eq@$_[1]}).elems.say' "$@"
diff --git a/challenge-233/jaldhar-h-vyas/raku/ch-2.raku b/challenge-233/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..1ab6a75526
--- /dev/null
+++ b/challenge-233/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,15 @@
+#!/usr/bin/raku
+
+sub MAIN(
+ *@ints
+) {
+ my %freq = @ints.classify({$_});
+
+ say q{(},
+ %freq
+ .keys
+ .sort({ %freq{$^a}.elems <=> %freq{$^b}.elems || $^b <=> $^a })
+ .map({ | %freq{$_} })
+ .join(q{,}),
+ q{)};
+} \ No newline at end of file