From 754a0f9cce2dc07a97609f2b8bc642dfee61930f Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Tue, 2 Jan 2024 02:29:50 -0500 Subject: TWC250 --- challenge-250/deadmarshal/blog.txt | 1 + challenge-250/deadmarshal/c/ch-1.c | 21 +++++++++++++ challenge-250/deadmarshal/c/ch-2.c | 31 ++++++++++++++++++++ challenge-250/deadmarshal/cpp/ch-1.cpp | 19 ++++++++++++ challenge-250/deadmarshal/cpp/ch-2.cpp | 30 +++++++++++++++++++ challenge-250/deadmarshal/d/ch1.d | 18 ++++++++++++ challenge-250/deadmarshal/d/ch2.d | 29 ++++++++++++++++++ challenge-250/deadmarshal/go/ch1.go | 20 +++++++++++++ challenge-250/deadmarshal/go/ch2.go | 34 ++++++++++++++++++++++ challenge-250/deadmarshal/lua/ch-1.lua | 14 +++++++++ challenge-250/deadmarshal/lua/ch-2.lua | 15 ++++++++++ challenge-250/deadmarshal/modula-3/ch1/src/Ch1.m3 | 23 +++++++++++++++ .../deadmarshal/modula-3/ch1/src/m3makefile | 4 +++ challenge-250/deadmarshal/modula-3/ch2/src/Ch2.m3 | 33 +++++++++++++++++++++ .../deadmarshal/modula-3/ch2/src/m3makefile | 4 +++ challenge-250/deadmarshal/pascal/ch1.pas | 25 ++++++++++++++++ challenge-250/deadmarshal/pascal/ch2.pas | 34 ++++++++++++++++++++++ challenge-250/deadmarshal/perl/ch-1.pl | 13 +++++++++ challenge-250/deadmarshal/perl/ch-2.pl | 12 ++++++++ challenge-250/deadmarshal/raku/ch-1.raku | 12 ++++++++ challenge-250/deadmarshal/raku/ch-2.raku | 10 +++++++ challenge-250/deadmarshal/ruby/ch1.rb | 11 +++++++ challenge-250/deadmarshal/ruby/ch2.rb | 14 +++++++++ 23 files changed, 427 insertions(+) create mode 100644 challenge-250/deadmarshal/blog.txt create mode 100644 challenge-250/deadmarshal/c/ch-1.c create mode 100644 challenge-250/deadmarshal/c/ch-2.c create mode 100644 challenge-250/deadmarshal/cpp/ch-1.cpp create mode 100644 challenge-250/deadmarshal/cpp/ch-2.cpp create mode 100644 challenge-250/deadmarshal/d/ch1.d create mode 100644 challenge-250/deadmarshal/d/ch2.d create mode 100644 challenge-250/deadmarshal/go/ch1.go create mode 100644 challenge-250/deadmarshal/go/ch2.go create mode 100644 challenge-250/deadmarshal/lua/ch-1.lua create mode 100644 challenge-250/deadmarshal/lua/ch-2.lua create mode 100644 challenge-250/deadmarshal/modula-3/ch1/src/Ch1.m3 create mode 100644 challenge-250/deadmarshal/modula-3/ch1/src/m3makefile create mode 100644 challenge-250/deadmarshal/modula-3/ch2/src/Ch2.m3 create mode 100644 challenge-250/deadmarshal/modula-3/ch2/src/m3makefile create mode 100644 challenge-250/deadmarshal/pascal/ch1.pas create mode 100644 challenge-250/deadmarshal/pascal/ch2.pas create mode 100644 challenge-250/deadmarshal/perl/ch-1.pl create mode 100644 challenge-250/deadmarshal/perl/ch-2.pl create mode 100644 challenge-250/deadmarshal/raku/ch-1.raku create mode 100644 challenge-250/deadmarshal/raku/ch-2.raku create mode 100644 challenge-250/deadmarshal/ruby/ch1.rb create mode 100644 challenge-250/deadmarshal/ruby/ch2.rb diff --git a/challenge-250/deadmarshal/blog.txt b/challenge-250/deadmarshal/blog.txt new file mode 100644 index 0000000000..464d2629c1 --- /dev/null +++ b/challenge-250/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2024/01/twc250.html diff --git a/challenge-250/deadmarshal/c/ch-1.c b/challenge-250/deadmarshal/c/ch-1.c new file mode 100644 index 0000000000..3c3c2398d9 --- /dev/null +++ b/challenge-250/deadmarshal/c/ch-1.c @@ -0,0 +1,21 @@ +#include + +int smallest_index(int *arr,size_t sz) +{ + for(size_t i = 0; i < sz; ++i) + if(i % 10 == arr[i]) return i; + return -1; +} + +int main(void) +{ + int arr1[] = {0,1,2}; + int arr2[] = {4,3,2,1}; + int arr3[] = {1,2,3,4,5,6,7,8,9,0}; + size_t sz1 = 3,sz2 = 4,sz3 = 10; + printf("%d\n",smallest_index(arr1,sz1)); + printf("%d\n",smallest_index(arr2,sz2)); + printf("%d\n",smallest_index(arr3,sz3)); + return 0; +} + diff --git a/challenge-250/deadmarshal/c/ch-2.c b/challenge-250/deadmarshal/c/ch-2.c new file mode 100644 index 0000000000..066b585bf2 --- /dev/null +++ b/challenge-250/deadmarshal/c/ch-2.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +int is_numeric(const char *str) +{ + for(size_t i = 0; str[i] != '\0'; ++i) if(!isdigit(str[i])) return 0; + return 1; +} + +size_t alphanumeric_string_value(const char **arr,size_t sz) +{ + size_t max = 0; + for(size_t i = 0; i < sz; ++i){ + size_t n = is_numeric(arr[i]) ? strtol(arr[i],NULL,10) : strlen(arr[i]); + if(n > max) max = n; + } + return max; +} + +int main(void) +{ + const char *arr1[] = {"perl","2","000","python","r4ku"}; + const char *arr2[] = {"001","1","000","0001"}; + size_t sz1 = 5,sz2 = 4; + printf("%zu\n",alphanumeric_string_value(arr1,sz1)); + printf("%zu\n",alphanumeric_string_value(arr2,sz2)); + return 0; +} + diff --git a/challenge-250/deadmarshal/cpp/ch-1.cpp b/challenge-250/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..ff43839325 --- /dev/null +++ b/challenge-250/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,19 @@ +#include +#include + +template +int smallest_index(const std::vector &vec) +{ + for(size_t i = 0; i < vec.size(); ++i) if(i % 10 == vec.at(i)) return i; + return -1; +} + +int main() +{ + std::vector vec1{0,1,2},vec2{4,3,2,1},vec3{1,2,3,4,5,6,7,8,9,0}; + std::cout << smallest_index(vec1) << '\n' + << smallest_index(vec2) << '\n' + << smallest_index(vec3) << '\n'; + return 0; +} + diff --git a/challenge-250/deadmarshal/cpp/ch-2.cpp b/challenge-250/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..0b87ec93b6 --- /dev/null +++ b/challenge-250/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,30 @@ +#include +#include + +bool is_numeric(const std::string &str) +{ + for(const auto& c : str) if(!isdigit(c)) return false; + return true; +} + +template +size_t alphanumeric_string_value(const std::vector &vec) +{ + size_t max{}; + for(auto & e : vec) + { + size_t n = is_numeric(e) ? std::stoi(e) : e.size(); + if(n > max) max = n; + } + return max; +} + +int main() +{ + std::vector vec1{"perl","2","000","python","r4ku"}, + vec2{"001","1","000","0001"}; + std::cout << alphanumeric_string_value(vec1) << '\n' + << alphanumeric_string_value(vec2) << '\n'; + return 0; +} + diff --git a/challenge-250/deadmarshal/d/ch1.d b/challenge-250/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..eb73d21e84 --- /dev/null +++ b/challenge-250/deadmarshal/d/ch1.d @@ -0,0 +1,18 @@ +import std.stdio; + +int smallest_index(int[] arr) +{ + foreach(i;0..arr.length) if(i % 10 == arr[i]) return cast(int)i; + return -1; +} + +void main() +{ + int[] arr1 = [0,1,2]; + int[] arr2 = [4,3,2,1]; + int[] arr3 = [1,2,3,4,5,6,7,8,9,0]; + writeln(smallest_index(arr1)); + writeln(smallest_index(arr2)); + writeln(smallest_index(arr3)); +} + diff --git a/challenge-250/deadmarshal/d/ch2.d b/challenge-250/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..535ebf9d15 --- /dev/null +++ b/challenge-250/deadmarshal/d/ch2.d @@ -0,0 +1,29 @@ +import std.stdio:writeln; +import std.ascii:isDigit; +import std.conv:to; + +bool is_numeric(string str) +{ + foreach(i;0..str.length) if(!isDigit(str[i])) return false; + return true; +} + +ulong alphanumeric_string_value(string[] arr) +{ + ulong max = 0; + foreach(i;0..arr.length) + { + ulong n = is_numeric(arr[i]) ? to!int(arr[i]) : arr[i].length; + if(n > max) max = n; + } + return max; +} + +void main() +{ + string[] arr1 = ["perl","2","000","python","r4ku"]; + string[] arr2 = ["001","1","000","0001"]; + writeln(alphanumeric_string_value(arr1)); + writeln(alphanumeric_string_value(arr2)); +} + diff --git a/challenge-250/deadmarshal/go/ch1.go b/challenge-250/deadmarshal/go/ch1.go new file mode 100644 index 0000000000..b6ad070a11 --- /dev/null +++ b/challenge-250/deadmarshal/go/ch1.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" +) + +func smallestIndex(arr []int) int { + for i, v := range arr { + if i%10 == v { + return i + } + } + return -1 +} + +func main() { + fmt.Println(smallestIndex([]int{0, 1, 2})) + fmt.Println(smallestIndex([]int{4, 3, 2, 1})) + fmt.Println(smallestIndex([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0})) +} diff --git a/challenge-250/deadmarshal/go/ch2.go b/challenge-250/deadmarshal/go/ch2.go new file mode 100644 index 0000000000..cdb67053e8 --- /dev/null +++ b/challenge-250/deadmarshal/go/ch2.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "strconv" +) + +func isNumeric(s string) bool { + _, err := strconv.ParseFloat(s, 64) + return err == nil +} + +func alphanumericStringValue(arr []string) int { + max := 0 + for _, v := range arr { + n := 0 + if isNumeric(v) { + n, _ = strconv.Atoi(v) + } else { + n = len(v) + } + if n > max { + max = n + } + } + return max +} + +func main() { + arr1 := []string{"perl", "2", "000", "python", "r4ku"} + arr2 := []string{"001", "1", "000", "0001"} + fmt.Println(alphanumericStringValue(arr1)) + fmt.Println(alphanumericStringValue(arr2)) +} diff --git a/challenge-250/deadmarshal/lua/ch-1.lua b/challenge-250/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..26ff895cd0 --- /dev/null +++ b/challenge-250/deadmarshal/lua/ch-1.lua @@ -0,0 +1,14 @@ +#!/usr/bin/env lua + +local function smallest_index(t) + assert(type(t) == 'table','t must be a table!') + for i=1,#t do + if i % 10 == t[i]+1 then return i-1 end + end + return -1 +end + +print(smallest_index{0,1,2}) +print(smallest_index{4,3,2,1}) +print(smallest_index{1,2,3,4,5,6,7,8,9,0}) + diff --git a/challenge-250/deadmarshal/lua/ch-2.lua b/challenge-250/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..a88eda2702 --- /dev/null +++ b/challenge-250/deadmarshal/lua/ch-2.lua @@ -0,0 +1,15 @@ +#!/usr/bin/env lua + +local function alphanumeric_string_value(t) + assert(type(t) == 'table','t must be a table!') + local max = 0 + for i=1,#t do + local n = t[i]:match('^%d+$') and tonumber(t[i]) or t[i]:len() + if n > max then max = n end + end + return max +end + +print(alphanumeric_string_value{'perl','2','000','python','r4ku'}) +print(alphanumeric_string_value{'001','1','000','0001'}) + diff --git a/challenge-250/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-250/deadmarshal/modula-3/ch1/src/Ch1.m3 new file mode 100644 index 0000000000..f532592594 --- /dev/null +++ b/challenge-250/deadmarshal/modula-3/ch1/src/Ch1.m3 @@ -0,0 +1,23 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT IO; + +VAR + A1 := ARRAY[0..2] OF CARDINAL{0,1,2}; + A2 := ARRAY[0..3] OF CARDINAL{4,3,2,1}; + A3 := ARRAY[0..9] OF CARDINAL{1,2,3,4,5,6,7,8,9,0}; + +PROCEDURE SmallestIndex(VAR A:ARRAY OF CARDINAL):INTEGER = + BEGIN + FOR I := FIRST(A) TO LAST(A) DO + IF I MOD 10 = A[I] THEN RETURN I END + END; + RETURN -1 + END SmallestIndex; + +BEGIN + IO.PutInt(SmallestIndex(A1)); IO.Put("\n"); + IO.PutInt(SmallestIndex(A2)); IO.Put("\n"); + IO.PutInt(SmallestIndex(A3)); IO.Put("\n"); +END Ch1. + diff --git a/challenge-250/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-250/deadmarshal/modula-3/ch1/src/m3makefile new file mode 100644 index 0000000000..0ee72d695b --- /dev/null +++ b/challenge-250/deadmarshal/modula-3/ch1/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +implementation("Ch1") +program("ch1") + diff --git a/challenge-250/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-250/deadmarshal/modula-3/ch2/src/Ch2.m3 new file mode 100644 index 0000000000..a56c7abe42 --- /dev/null +++ b/challenge-250/deadmarshal/modula-3/ch2/src/Ch2.m3 @@ -0,0 +1,33 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT IO,Text,Scan; + +VAR + A1 := ARRAY[0..4] OF TEXT{"perl","2","000","python","r4ku"}; + A2 := ARRAY[0..3] OF TEXT{"001","1","000","0001"}; + +PROCEDURE IsNumeric(VAR t:TEXT):BOOLEAN = + BEGIN + FOR I := 0 TO Text.Length(t)-1 DO + IF NOT Text.GetChar(t,I) IN SET OF CHAR{'0'..'9'} THEN RETURN FALSE END + END; + RETURN TRUE + END IsNumeric; + +PROCEDURE AlphanumericStringValue(VAR A:ARRAY OF TEXT):CARDINAL = + VAR Max,N:CARDINAL := 0; + BEGIN + FOR I := FIRST(A) TO LAST(A) DO + IF IsNumeric(A[I]) THEN N := Scan.Unsigned(A[I],10) + ELSE N := Text.Length(A[I]) + END; + IF N > Max THEN Max := N END + END; + RETURN Max + END AlphanumericStringValue; + +BEGIN + IO.PutInt(AlphanumericStringValue(A1)); IO.Put("\n"); + IO.PutInt(AlphanumericStringValue(A2)); IO.Put("\n"); +END Ch2. + diff --git a/challenge-250/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-250/deadmarshal/modula-3/ch2/src/m3makefile new file mode 100644 index 0000000000..5c32bbc4bb --- /dev/null +++ b/challenge-250/deadmarshal/modula-3/ch2/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +implementation("Ch2") +program("ch2") + diff --git a/challenge-250/deadmarshal/pascal/ch1.pas b/challenge-250/deadmarshal/pascal/ch1.pas new file mode 100644 index 0000000000..efc5e38db5 --- /dev/null +++ b/challenge-250/deadmarshal/pascal/ch1.pas @@ -0,0 +1,25 @@ +program Ch1; + +{$mode objfpc} +uses + Sysutils,Types; + +var + A1,A2,A3:TIntegerDynArray; + +function SmallestIndex(var Arr:TIntegerDynArray):Integer; +var I:Integer; +begin + for I := Low(Arr) to High(Arr) do if I mod 10 = Arr[I] then Exit(I); + Exit(-1) +end; + +begin + A1 := [0,1,2]; + A2 := [4,3,2,1]; + A3 := [1,2,3,4,5,6,7,8,9,0]; + WriteLn(SmallestIndex(A1)); + WriteLn(SmallestIndex(A2)); + WriteLn(SmallestIndex(A3)); +end. + diff --git a/challenge-250/deadmarshal/pascal/ch2.pas b/challenge-250/deadmarshal/pascal/ch2.pas new file mode 100644 index 0000000000..df558bba70 --- /dev/null +++ b/challenge-250/deadmarshal/pascal/ch2.pas @@ -0,0 +1,34 @@ +program Ch2; + +{$mode objfpc} +uses + SysUtils,Types,Character; + +var + A1,A2:TStringDynArray; + +function IsNumeric(var Str:AnsiString):Boolean; +var I:Integer; +begin + for I := Low(Str) to High(Str) do if not IsDigit(Str[I]) then Exit(False); + Exit(True) +end; + +function AlphanumericStringValue(var Arr:TStringArray):Integer; +var I,N:Integer; +begin + Result := 0; + for I := Low(Arr) to High(Arr) do + begin + if IsNumeric(Arr[I]) then N := StrToInt(Arr[I]) else N := Length(Arr[I]); + if N > Result then Result := N; + end; +end; + +begin + A1 := ['perl','2','000','python','r4ku']; + A2 := ['001','1','000','0001']; + WriteLn(AlphanumericStringValue(A1)); + WriteLn(AlphanumericStringValue(A2)); +end. + diff --git a/challenge-250/deadmarshal/perl/ch-1.pl b/challenge-250/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..cff994aeac --- /dev/null +++ b/challenge-250/deadmarshal/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub smallest_index{ + map{return $_ if $_ % 10 == $_[0]->[$_]}0..$#{$_[0]}; + -1 +} + +printf "%d\n",smallest_index([0,1,2]); +printf "%d\n",smallest_index([4,3,2,1]); +printf "%d\n",smallest_index([1,2,3,4,5,6,7,8,9,0]); + diff --git a/challenge-250/deadmarshal/perl/ch-2.pl b/challenge-250/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..a85bc593d1 --- /dev/null +++ b/challenge-250/deadmarshal/perl/ch-2.pl @@ -0,0 +1,12 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(max); + +sub alphanumeric_string_value{ + max map{my $n = (/^\d+$/) ? $_ : length}@{$_[0]}; +} + +printf "%d\n",alphanumeric_string_value(['perl','2','000','python','r4ku']); +printf "%d\n",alphanumeric_string_value(['001','1','000','0001']); + diff --git a/challenge-250/deadmarshal/raku/ch-1.raku b/challenge-250/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..e52acb2e8c --- /dev/null +++ b/challenge-250/deadmarshal/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/usr/bin/env raku + +sub smallest_index(@arr) +{ + (0..@arr.end).map: {return $_ if $_ % 10 == @arr[$_]} + -1 +} + +say smallest_index([0,1,2]); +say smallest_index([4,3,2,1]); +say smallest_index([1,2,3,4,5,6,7,8,9,0]); + diff --git a/challenge-250/deadmarshal/raku/ch-2.raku b/challenge-250/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..0d1c6bd6d8 --- /dev/null +++ b/challenge-250/deadmarshal/raku/ch-2.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku + +sub alphanumeric_string_value(@arr) +{ + (@arr.map: {my $n = $_.Numeric !~~ Failure ?? (0+$_) !! .chars}).max +} + +say alphanumeric_string_value(['perl','2','000','python','r4ku']); +say alphanumeric_string_value(['001','1','000','0001']); + diff --git a/challenge-250/deadmarshal/ruby/ch1.rb b/challenge-250/deadmarshal/ruby/ch1.rb new file mode 100644 index 0000000000..d21552eb85 --- /dev/null +++ b/challenge-250/deadmarshal/ruby/ch1.rb @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby + +def smallest_index(arr) + (0..arr.length).map {|i| return i if i % 10 == arr[i]} + -1 +end + +p smallest_index([0,1,2]) +p smallest_index([4,3,2,1]) +p smallest_index([1,2,3,4,5,6,7,8,9,0]) + diff --git a/challenge-250/deadmarshal/ruby/ch2.rb b/challenge-250/deadmarshal/ruby/ch2.rb new file mode 100644 index 0000000000..409b548495 --- /dev/null +++ b/challenge-250/deadmarshal/ruby/ch2.rb @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +def alphanumeric_string_value(arr) + max = 0 + arr.map do |e| + n = (true if Integer(e) rescue false) ? e.to_i : e.length + max = n if n > max + end + max +end + +p alphanumeric_string_value(['perl','2','000','python','r4ku']) +p alphanumeric_string_value(['001','1','000','0001']) + -- cgit