diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-10 21:46:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-10 21:46:21 +0100 |
| commit | 5ebe97797282cc6217010bcfacbf2a06364edf48 (patch) | |
| tree | 0f55c2aa28dbe9639268f9d5f3a36f365d6a21c2 | |
| parent | c09668092e28422d67258b8c86e4c92ff3a8ad1b (diff) | |
| parent | 1701193c04567d9f969f0c3eeb21d20f29c282a1 (diff) | |
| download | perlweeklychallenge-club-5ebe97797282cc6217010bcfacbf2a06364edf48.tar.gz perlweeklychallenge-club-5ebe97797282cc6217010bcfacbf2a06364edf48.tar.bz2 perlweeklychallenge-club-5ebe97797282cc6217010bcfacbf2a06364edf48.zip | |
Merge pull request #10407 from deadmarshal/TWC277
TWC277
| -rw-r--r-- | challenge-277/deadmarshal/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-277/deadmarshal/perl/ch-1.pl | 20 | ||||
| -rw-r--r-- | challenge-277/deadmarshal/perl/ch-2.pl | 14 | ||||
| -rw-r--r-- | challenge-277/deadmarshal/raku/ch-1.raku | 14 | ||||
| -rw-r--r-- | challenge-277/deadmarshal/raku/ch-2.raku | 9 | ||||
| -rw-r--r-- | challenge-277/deadmarshal/ruby/ch-1.rb | 15 | ||||
| -rw-r--r-- | challenge-277/deadmarshal/ruby/ch-2.rb | 9 |
7 files changed, 82 insertions, 0 deletions
diff --git a/challenge-277/deadmarshal/blog.txt b/challenge-277/deadmarshal/blog.txt new file mode 100644 index 0000000000..c504f86ae5 --- /dev/null +++ b/challenge-277/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2024/07/twc277.html diff --git a/challenge-277/deadmarshal/perl/ch-1.pl b/challenge-277/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..f255551d41 --- /dev/null +++ b/challenge-277/deadmarshal/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub count_common{ + my %h; + $h{$_}++ foreach @{$_[0]},@{$_[1]}; + (grep{$_ == 2}values %h) // 0 +} + +printf "%d\n", + count_common(['Perl','is','my','friend'], + ['Perl','and','Raku','are','friend']); +printf "%d\n", + count_common(['Perl','and','Python','are','very','similar'], + ['Python','is','top','in','guest','languages']); +printf "%d\n", + count_common(['Perl','is','imperative','Lisp','is','functional'], + ['Crystal','is','similar','to','Ruby']); + diff --git a/challenge-277/deadmarshal/perl/ch-2.pl b/challenge-277/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..d48c2b1a70 --- /dev/null +++ b/challenge-277/deadmarshal/perl/ch-2.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Algorithm::Combinatorics qw(combinations); +use List::AllUtils qw(min uniq); + +sub strong_pairs{ + scalar grep{abs($$_[0] - $$_[1]) < min(@$_)} + combinations([uniq @{$_[0]}],2) +} + +printf "%d\n",strong_pairs([1,2,3,4,5]); +printf "%d\n",strong_pairs([5,7,1,7]); + diff --git a/challenge-277/deadmarshal/raku/ch-1.raku b/challenge-277/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..8aa1d02622 --- /dev/null +++ b/challenge-277/deadmarshal/raku/ch-1.raku @@ -0,0 +1,14 @@ +#!/usr/bin/env raku + +sub count-common(@arr1,@arr2) +{ + ((@arr1,@arr2).flat.Bag.values.grep: {$_ == 2}).elems +} + +say count-common(['Perl','is','my','friend'], + ['Perl','and','Raku','are','friend']); +say count-common(['Perl','and','Python','are','very','similar'], + ['Python','is','top','in','guest','languages']); +say count-common(['Perl','is','imperative','Lisp','is','functional'], + ['Crystal','is','similar','to','Ruby']); + diff --git a/challenge-277/deadmarshal/raku/ch-2.raku b/challenge-277/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..ca12eda356 --- /dev/null +++ b/challenge-277/deadmarshal/raku/ch-2.raku @@ -0,0 +1,9 @@ +#!/usr/bin/env raku + +sub strong-pairs(@arr){ + (@arr.unique.combinations(2).grep: {(abs $_[0] - $_[1]) < $_.min}).elems; +} + +say strong-pairs([1,2,3,4,5]); +say strong-pairs([5,7,1,7]); + diff --git a/challenge-277/deadmarshal/ruby/ch-1.rb b/challenge-277/deadmarshal/ruby/ch-1.rb new file mode 100644 index 0000000000..1f27e056d0 --- /dev/null +++ b/challenge-277/deadmarshal/ruby/ch-1.rb @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby + +def count_common(arr1,arr2) + h = Hash.new(0) + (arr1 + arr2).each {|e| h[e] += 1 } + (h.values.select{|e| e == 2}).size +end + +p count_common(['Perl','is','my','friend'], + ['Perl','and','Raku','are','friend']) +p count_common(['Perl','and','Python','are','very','similar'], + ['Python','is','top','in','guest','languages']) +p count_common(['Perl','is','imperative','Lisp','is','functional'], + ['Crystal','is','similar','to','Ruby']) + diff --git a/challenge-277/deadmarshal/ruby/ch-2.rb b/challenge-277/deadmarshal/ruby/ch-2.rb new file mode 100644 index 0000000000..f7137c59c7 --- /dev/null +++ b/challenge-277/deadmarshal/ruby/ch-2.rb @@ -0,0 +1,9 @@ +#!/usr/bin/env ruby + +def strong_pairs(arr) + (arr.uniq.combination(2).select{|e| (e[0] - e[1]).abs < e.min}).size +end + +p strong_pairs([1,2,3,4,5]) +p strong_pairs([5,7,1,7]) + |
