diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-12-04 00:32:47 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-04 00:32:47 +0000 |
| commit | f7f62e96e60390ea0104649f32279d95c28f8fcf (patch) | |
| tree | 15d927e5462b37b295b76e72fcd47ec4bd5b7471 /challenge-245 | |
| parent | bf6e35199edac6b68566988e9b6cd98ab875ae35 (diff) | |
| parent | 273320e5f2358a95989aa8ddc64dadf99e7e9523 (diff) | |
| download | perlweeklychallenge-club-f7f62e96e60390ea0104649f32279d95c28f8fcf.tar.gz perlweeklychallenge-club-f7f62e96e60390ea0104649f32279d95c28f8fcf.tar.bz2 perlweeklychallenge-club-f7f62e96e60390ea0104649f32279d95c28f8fcf.zip | |
Merge pull request #9184 from BarrOff/barroff-245
feat: add solutions for challenge 245 from BarrOff
Diffstat (limited to 'challenge-245')
| -rw-r--r-- | challenge-245/barroff/julia/ch-1.jl | 13 | ||||
| -rw-r--r-- | challenge-245/barroff/nim/ch_1.nim | 32 | ||||
| -rw-r--r-- | challenge-245/barroff/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-245/barroff/raku/ch-1.p6 | 17 |
4 files changed, 88 insertions, 0 deletions
diff --git a/challenge-245/barroff/julia/ch-1.jl b/challenge-245/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..1a31a37267 --- /dev/null +++ b/challenge-245/barroff/julia/ch-1.jl @@ -0,0 +1,13 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function sort_language(lang::Vector{S}, positions::Vector{T}) where {S<: AbstractString, T<:Integer} + pos_dict = Dict(zip(positions, lang)) + [pos_dict[x] for x in sort(positions)] +end + +@testset "reverse pairs" begin + @test sort_language(["perl", "c", "python"], [ 2, 1, 3 ]) == ["c", "perl", "python"] + @test sort_language(["c++", "haskell", "java"], [ 1, 3, 2 ]) == ["c++", "java", "haskell"] +end diff --git a/challenge-245/barroff/nim/ch_1.nim b/challenge-245/barroff/nim/ch_1.nim new file mode 100644 index 0000000000..a588674f08 --- /dev/null +++ b/challenge-245/barroff/nim/ch_1.nim @@ -0,0 +1,32 @@ +import std/[algorithm, sequtils, sugar, tables, unittest] + +# run tests with following command: +# nim c -r ch_1.nim + +proc sortLanguage(lang: openArray[string], positions: openArray[int]): seq[string] = + var + posHash = initTable[int, string]() + setlen(result, len(lang)) + for x in zip(positions, lang): + posHash[x[0]] = x[1] + for i in sorted(positions): + result[i - 1] = posHash[i] + +suite "sort language": + test """"["perl", "c", "python"], [2, 1, 3]""": + let + lang = ["perl", "c", "python"] + positions = [2, 1, 3] + expected = ["c", "perl", "python"] + res = sortLanguage(lang, positions) + + check(expected == res) + + test """"["c++", "haskell", "java"], [1, 3, 2]""": + let + lang = ["c++", "haskell", "java"] + positions = [1, 3, 2] + expected = ["c++", "java", "haskell"] + res = sortLanguage(lang, positions) + + check(expected == res) diff --git a/challenge-245/barroff/perl/ch-1.pl b/challenge-245/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..23510a0f43 --- /dev/null +++ b/challenge-245/barroff/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use v5.38; + +sub sort_language ( $lang, @positions ) { + my %pos_map; + @pos_map{@positions} = @{$lang}; + my @res = @pos_map{ sort @positions }; + return \@res; +} + +sub MAIN() { + + #| Run test cases + use Test2::V0 qw( is plan ); + plan 2; + + is sort_language( [ 'perl', 'c', 'python' ], 2, 1, 3 ), + [ 'c', 'perl', 'python' ], + "works for ('c', 'perl', 'python')"; + is sort_language( [ 'c++', 'haskell', 'java' ], 1, 3, 2 ), + [ 'c++', 'java', 'haskell' ], + "works for ('c++', 'java', 'haskell')"; +} + +MAIN(); diff --git a/challenge-245/barroff/raku/ch-1.p6 b/challenge-245/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..9af1ebad54 --- /dev/null +++ b/challenge-245/barroff/raku/ch-1.p6 @@ -0,0 +1,17 @@ +#!/usr/bin/env raku + +sub sort-language(@lang, @positions --> List) { + my %pos-hash = Hash(@positions Z=> @lang); + %pos-hash{ sort(@positions) } +} + +#| Run test cases +multi sub MAIN() { + use Test; + plan 2; + + is sort-language(['perl', 'c', 'python'], [2, 1, 3]), + ('c', 'perl', 'python'), 'Works for first example'; + is sort-language(['c++', 'haskell', 'java'], [1, 3, 2]), + ('c++', 'java', 'haskell'), 'Works for second example'; +} |
