aboutsummaryrefslogtreecommitdiff
path: root/challenge-245
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-04 00:32:47 +0000
committerGitHub <noreply@github.com>2023-12-04 00:32:47 +0000
commitf7f62e96e60390ea0104649f32279d95c28f8fcf (patch)
tree15d927e5462b37b295b76e72fcd47ec4bd5b7471 /challenge-245
parentbf6e35199edac6b68566988e9b6cd98ab875ae35 (diff)
parent273320e5f2358a95989aa8ddc64dadf99e7e9523 (diff)
downloadperlweeklychallenge-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.jl13
-rw-r--r--challenge-245/barroff/nim/ch_1.nim32
-rw-r--r--challenge-245/barroff/perl/ch-1.pl26
-rw-r--r--challenge-245/barroff/raku/ch-1.p617
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';
+}