diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-14 22:15:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-14 22:15:10 +0100 |
| commit | d029bcc0e46525c110697c70b2d51be88c9b4516 (patch) | |
| tree | ce5161dd593103c3226a154f66f9cc43d206c472 | |
| parent | 8208b83aa08f2c3a8018f83a6b25fb069247c0aa (diff) | |
| parent | 758ae19f055bc4db08a98c5e28d6d9806ce1594a (diff) | |
| download | perlweeklychallenge-club-d029bcc0e46525c110697c70b2d51be88c9b4516.tar.gz perlweeklychallenge-club-d029bcc0e46525c110697c70b2d51be88c9b4516.tar.bz2 perlweeklychallenge-club-d029bcc0e46525c110697c70b2d51be88c9b4516.zip | |
Merge pull request #10425 from BarrOff/barroff-277
feat: add solutions for challenge 276 from BarrOff
| -rw-r--r-- | challenge-277/barroff/julia/ch-1.jl | 31 | ||||
| -rw-r--r-- | challenge-277/barroff/julia/ch-2.jl | 29 | ||||
| -rw-r--r-- | challenge-277/barroff/raku/ch-1.p6 | 25 | ||||
| -rw-r--r-- | challenge-277/barroff/raku/ch-2.p6 | 30 |
4 files changed, 115 insertions, 0 deletions
diff --git a/challenge-277/barroff/julia/ch-1.jl b/challenge-277/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..63ddcff5f4 --- /dev/null +++ b/challenge-277/barroff/julia/ch-1.jl @@ -0,0 +1,31 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function count_common(words1::Vector{T}, words2::Vector{T})::Int where {T<:AbstractString} + words1Dict = Dict{T,Int}() + words2Dict = Dict{T,Int}() + map(x -> words1Dict[x] = get(words1Dict, x, 0) + 1, words1) + map(x -> words2Dict[x] = get(words2Dict, x, 0) + 1, words2) + length( + filter( + x -> words1Dict[x] == 1 && get(words2Dict, x, 0) == 1, + keys(words1Dict) + ) + ) +end + +@testset "count common" begin + @test count_common( + ["Perl", "is", "my", "friend"], + ["Perl", "and", "Raku", "are", "friend"], + ) == 2 + @test count_common( + ["Perl", "and", "Python", "are", "very", "similar"], + ["Python", "is", "top", "in", "guest", "languages"], + ) == 1 + @test count_common( + ["Perl", "is", "imperative", "Lisp", "is", "functional"], + ["Crystal", "is", "similar", "to", "Ruby"], + ) == 0 +end diff --git a/challenge-277/barroff/julia/ch-2.jl b/challenge-277/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..c38c44c9f1 --- /dev/null +++ b/challenge-277/barroff/julia/ch-2.jl @@ -0,0 +1,29 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function is_strong_pair(x::S, y::T)::Bool where {S<:Real,T<:Real} + if x == y + return false + end + return abs(x - y) < min(x, y) +end + + +function strong_pair(ints::Vector{T})::Int where {T<:Real} + unique_ints = unique(ints) + length( + filter( + x -> x, + [ + is_strong_pair(unique_ints[x], unique_ints[y]) for x in 1:length(unique_ints)-1 for y in x+1:length(unique_ints) + ] + ) + ) + +end + +@testset "strong pair" begin + @test strong_pair([1, 2, 3, 4, 5]) == 4 + @test strong_pair([5, 7, 1, 7]) == 1 +end diff --git a/challenge-277/barroff/raku/ch-1.p6 b/challenge-277/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..6f3fbfff64 --- /dev/null +++ b/challenge-277/barroff/raku/ch-1.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub count-common(@words1, @words2 --> Int) { + my $words1-bag = Bag(@words1); + my $words2-bag = Bag(@words2); + grep({ $words1-bag{$_} == 1 and $words2-bag{$_} == 1 }, keys($words1-bag)).elems; +} + +#| Run test cases +sub MAIN() { + use Test; + plan 3; + + is count-common( + ("Perl", "is", "my", "friend"), ("Perl", "and", "Raku", "are", "friend") + ), 2, 'works for ("Perl", "is", "my", "friend") and ("Perl", "and", "Raku", "are", "friend")'; + is count-common( + ("Perl", "and", "Python", "are", "very", "similar"), ("Python", "is", "top", "in", "guest", "languages") + ), 1, 'works for ("Perl", "and", "Python", "are", "very", "similar") and ("Python", "is", "top", "in", "guest", "languages")'; + is count-common( + ("Perl", "is", "imperative", "Lisp", "is", "functional"), ("Crystal", "is", "similar", "to", "Ruby") + ), 0, 'works for ("Perl", "is", "imperative", "Lisp", "is", "functional") and ("Crystal", "is", "similar", "to", "Ruby")'; +} diff --git a/challenge-277/barroff/raku/ch-2.p6 b/challenge-277/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..03beb5e5b0 --- /dev/null +++ b/challenge-277/barroff/raku/ch-2.p6 @@ -0,0 +1,30 @@ +#!/usr/bin/env raku + +use v6.d; + +sub is-strong-pair(Int $x, Int $y --> Bool) { + return False if $x == $y; + return abs($x - $y) < min($x, $y); +} + +sub strong-pair(@ints --> Int) { + my @uniq_ints = unique(@ints); + grep( + { is-strong-pair($_[0], $_[1]) }, + @uniq_ints.combinations: 2 + ).elems; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is strong-pair([1, 2, 3, 4, 5]), 4, 'works for [1, 2, 3, 4, 5]'; + is strong-pair([5, 7, 1, 7]), 1, 'works for [5, 7, 1, 7]'; +} + +#| Take user provided number like 1024 512 256 128 64 +multi sub MAIN(*@ints) { + say strong-pair(@ints); +} |
