diff options
| -rwxr-xr-x | challenge-250/barroff/d/ch_1.d | 25 | ||||
| -rwxr-xr-x | challenge-250/barroff/d/ch_2.d | 19 | ||||
| -rw-r--r-- | challenge-250/barroff/julia/ch-1.jl | 14 | ||||
| -rw-r--r-- | challenge-250/barroff/julia/ch-2.jl | 14 | ||||
| -rw-r--r-- | challenge-250/barroff/nim/ch_1.nim | 29 | ||||
| -rw-r--r-- | challenge-250/barroff/nim/ch_2.nim | 28 | ||||
| -rw-r--r-- | challenge-250/barroff/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-250/barroff/perl/ch-2.pl | 29 | ||||
| -rw-r--r-- | challenge-250/barroff/raku/ch-1.p6 | 22 | ||||
| -rw-r--r-- | challenge-250/barroff/raku/ch-2.p6 | 23 | ||||
| -rwxr-xr-x | challenge-250/barroff/v/ch-1.v | 18 |
11 files changed, 243 insertions, 0 deletions
diff --git a/challenge-250/barroff/d/ch_1.d b/challenge-250/barroff/d/ch_1.d new file mode 100755 index 0000000000..6ae6e591ab --- /dev/null +++ b/challenge-250/barroff/d/ch_1.d @@ -0,0 +1,25 @@ +#!/usr/bin/env -S rdmd -unittest + +auto smallest_integer(int[] ints) +{ + foreach (i, x; ints) + { + if (x == i % 10) + { + return i; + } + } + + return -1; +} + +unittest +{ + assert(smallest_integer([0, 1, 2]) == 0); + assert(smallest_integer([4, 3, 2, 1]) == 2); + assert(smallest_integer([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == -1); +} + +void main() +{ +} diff --git a/challenge-250/barroff/d/ch_2.d b/challenge-250/barroff/d/ch_2.d new file mode 100755 index 0000000000..47c9cff674 --- /dev/null +++ b/challenge-250/barroff/d/ch_2.d @@ -0,0 +1,19 @@ +#!/usr/bin/env -S rdmd -unittest +import std.algorithm : map, maxElement; +import std.conv : to; +import std.string : isNumeric; + +auto alphanumeric_string_value(string[] alphanumstr) +{ + return alphanumstr.map!(a => isNumeric(a) ? a.to!int : a.length).maxElement; +} + +unittest +{ + assert(alphanumeric_string_value(["perl", "2", "000", "python", "r4ku"]) == 6); + assert(alphanumeric_string_value(["001", "1", "000", "0001"]) == 1); +} + +void main() +{ +} diff --git a/challenge-250/barroff/julia/ch-1.jl b/challenge-250/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..a6a892cd0e --- /dev/null +++ b/challenge-250/barroff/julia/ch-1.jl @@ -0,0 +1,14 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function smallest_index(ints::Vector{T}) where {T<:Integer} + indices = filter(x -> ints[x] % 10 == x - 1, 1:length(ints)) + return length(indices) > 0 ? indices[1] - 1 : -1 +end + +@testset "smallest index" begin + @test smallest_index([0, 1, 2]) == 0 + @test smallest_index([4, 3, 2, 1]) == 2 + @test smallest_index([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == -1 +end diff --git a/challenge-250/barroff/julia/ch-2.jl b/challenge-250/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..bff5607ecf --- /dev/null +++ b/challenge-250/barroff/julia/ch-2.jl @@ -0,0 +1,14 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function alphanumeric_string_value(alphanumstr::Vector{T}) where {T<:AbstractString} + return maximum( + map(x -> occursin(r"[a-zA-Z]", x) ? lastindex(x) : parse(Int, x), alphanumstr), + ) +end + +@testset "alphanumeric string value" begin + @test alphanumeric_string_value(["perl", "2", "000", "python", "r4ku"]) == 6 + @test alphanumeric_string_value(["001", "1", "000", "0001"]) == 1 +end diff --git a/challenge-250/barroff/nim/ch_1.nim b/challenge-250/barroff/nim/ch_1.nim new file mode 100644 index 0000000000..6e0a0deece --- /dev/null +++ b/challenge-250/barroff/nim/ch_1.nim @@ -0,0 +1,29 @@ +import std/unittest + +# run tests with following command: +# nim c -r ch_1.nim + +proc smallest_index[T: SomeInteger](ints: openArray[T]): int = + for i, elem in ints: + if elem == i mod 10: + return i + result = -1 + +suite "smallest index": + test "(0, 1, 2)": + let + l1 = [0, 1, 2] + + check(smallest_index(l1) == 0) + + test "(4, 3, 2, 1)": + let + l1 = [4, 3, 2, 1] + + check(smallest_index(l1) == 2) + + test "(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)": + let + l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] + + check(smallest_index(l1) == -1) diff --git a/challenge-250/barroff/nim/ch_2.nim b/challenge-250/barroff/nim/ch_2.nim new file mode 100644 index 0000000000..997ce82ab0 --- /dev/null +++ b/challenge-250/barroff/nim/ch_2.nim @@ -0,0 +1,28 @@ +import std/unittest + +from std/sequtils import map +from std/strutils import Letters, contains, parseInt + +# run tests with following command: +# nim c -r ch_2.nim + +proc alphanumeric_string_value(alphanumstr: openArray[string]): int = + result = max(map(alphanumstr, proc(x: string): int = + if contains(x, Letters): + len(x) + else: + parseInt(x) + )) + +suite "alphanumeric string value": + test """("perl", "2", "000", "python", "r4ku")""": + let + als = ["perl", "2", "000", "python", "r4ku"] + + check(alphanumeric_string_value(als) == 6) + + test """("001", "1", "000", "0001")""": + let + als = ["001", "1", "000", "0001"] + + check(alphanumeric_string_value(als) == 1) diff --git a/challenge-250/barroff/perl/ch-1.pl b/challenge-250/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..755334dea9 --- /dev/null +++ b/challenge-250/barroff/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env raku + +sub smallest-index(@ints --> Int:D) { + my Int:D @indices = grep({ @ints[$_] % 10 == $_}, 0..@ints.elems - 1); + return @indices ?? @indices[0] !! -1; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is smallest-index([0, 1, 2]), 0, 'Works for (0, 1, 2)'; + is smallest-index([4, 3, 2, 1]), 2, 'Works for (4, 3, 2, 1)'; + is smallest-index([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), -1, + 'Works for (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(*@ints) { + say smallest-index(@ints); +} diff --git a/challenge-250/barroff/perl/ch-2.pl b/challenge-250/barroff/perl/ch-2.pl new file mode 100644 index 0000000000..2f77109701 --- /dev/null +++ b/challenge-250/barroff/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use v5.38; + +sub alphanumeric_string_value (@alphanumstring) { + use List::Util qw( max ); + max( map( { $_ =~ /[a-zA-Z]/ ? length($_) : $_ + 0 } @alphanumstring ) ); +} + +sub MAIN() { + if (@ARGV) { + + #| Run on command line argument + say alphanumeric_string_value(@ARGV); + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 2; + + is alphanumeric_string_value( + ( "perl", "2", "000", "python", "r4ku" ) ), 6, + 'works for ("perl", "2", "000", "python", "r4ku")'; + is alphanumeric_string_value( ( "001", "1", "000", "0001" ) ), 1, + 'works for ("001", "1", "000", "0001")'; + } +} + +MAIN(); diff --git a/challenge-250/barroff/raku/ch-1.p6 b/challenge-250/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..755334dea9 --- /dev/null +++ b/challenge-250/barroff/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#!/usr/bin/env raku + +sub smallest-index(@ints --> Int:D) { + my Int:D @indices = grep({ @ints[$_] % 10 == $_}, 0..@ints.elems - 1); + return @indices ?? @indices[0] !! -1; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is smallest-index([0, 1, 2]), 0, 'Works for (0, 1, 2)'; + is smallest-index([4, 3, 2, 1]), 2, 'Works for (4, 3, 2, 1)'; + is smallest-index([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), -1, + 'Works for (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(*@ints) { + say smallest-index(@ints); +} diff --git a/challenge-250/barroff/raku/ch-2.p6 b/challenge-250/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..a6dad8a633 --- /dev/null +++ b/challenge-250/barroff/raku/ch-2.p6 @@ -0,0 +1,23 @@ +#!/usr/bin/env raku + +use v6.d; + +sub alphanumeric-string-value(@alphanumstr --> Int:D) { + max(map({ $_ ~~ /<[a..zA..Z]>/ ?? chars($_) !! Int($_)}, @alphanumstr)); +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is alphanumeric-string-value(("perl", "2", "000", "python", "r4ku")), 6, + 'works for ("perl", "2", "000", "python", "r4ku")'; + is alphanumeric-string-value(("001", "1", "000", "0001")), 1, + 'works for ("001", "1", "000", "0001")'; +} + +#| Take user provided list like aba aabb abcd bac aabc +multi sub MAIN(*@s) { + say alphanumeric-string-value(@s); +} diff --git a/challenge-250/barroff/v/ch-1.v b/challenge-250/barroff/v/ch-1.v new file mode 100755 index 0000000000..a987bf1df5 --- /dev/null +++ b/challenge-250/barroff/v/ch-1.v @@ -0,0 +1,18 @@ +#!/usr/bin/env -S v run + +module main + +fn smallest_integer(ints []int) int { + for i, x in ints { + if x == i % 10 { + return i + } + } + return -1 +} + +fn main() { + assert smallest_integer([0, 1, 2]) == 0 + assert smallest_integer([4, 3, 2, 1]) == 2 + assert smallest_integer([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == -1 +} |
