aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-180/deadmarshal/cpp/ch-1.cpp37
-rw-r--r--challenge-180/deadmarshal/cpp/ch-2.cpp26
-rw-r--r--challenge-180/deadmarshal/d/ch1.d32
-rw-r--r--challenge-180/deadmarshal/d/ch2.d22
-rw-r--r--challenge-180/deadmarshal/lua/ch-1.lua22
-rw-r--r--challenge-180/deadmarshal/lua/ch-2.lua13
-rw-r--r--challenge-180/deadmarshal/modula-3/ch1/src/Ch1.m334
-rw-r--r--challenge-180/deadmarshal/modula-3/ch1/src/m3makefile4
-rw-r--r--challenge-180/deadmarshal/modula-3/ch2/src/Ch2.m348
-rw-r--r--challenge-180/deadmarshal/modula-3/ch2/src/m3makefile3
-rw-r--r--challenge-180/deadmarshal/nim/ch1.nim23
-rw-r--r--challenge-180/deadmarshal/nim/ch2.nim14
-rw-r--r--challenge-180/deadmarshal/pascal/ch1.pas35
-rw-r--r--challenge-180/deadmarshal/pascal/ch2.pas41
-rw-r--r--challenge-180/deadmarshal/perl/ch-1.pl19
-rw-r--r--challenge-180/deadmarshal/perl/ch-2.pl13
-rw-r--r--challenge-180/deadmarshal/python/ch1.py20
-rw-r--r--challenge-180/deadmarshal/python/ch2.py9
-rw-r--r--challenge-180/deadmarshal/raku/ch-1.raku20
-rw-r--r--challenge-180/deadmarshal/raku/ch-2.raku8
20 files changed, 443 insertions, 0 deletions
diff --git a/challenge-180/deadmarshal/cpp/ch-1.cpp b/challenge-180/deadmarshal/cpp/ch-1.cpp
new file mode 100644
index 0000000000..389fdd96e6
--- /dev/null
+++ b/challenge-180/deadmarshal/cpp/ch-1.cpp
@@ -0,0 +1,37 @@
+#include<cstdio>
+#include<vector>
+#include<string>
+#include<unordered_map>
+
+char *first_unique_character(const std::string& str)
+{
+ char *buffer = new char[39];
+ std::vector<char> chars{str.begin(), str.end()};
+ std::unordered_map<char, unsigned> hash{};
+ for(const char& c : str) hash[c]++;
+ for(std::size_t i = 0; i < chars.size(); ++i)
+ if(hash[chars[i]] == 1)
+ {
+ std::snprintf(buffer,
+ 39,
+ "%zu as '%c' is the first unique character",
+ i,
+ chars[i]);
+ break;
+ }
+ return buffer;
+}
+
+int main(int argc, char *argv[])
+{
+ if(argc < 2)
+ {
+ fprintf(stderr, "No arg(s) provided!");
+ exit(1);
+ }
+ char *buffer = first_unique_character(argv[1]);
+ printf("%s\n", buffer);
+ delete buffer;
+
+ return 0;
+}
diff --git a/challenge-180/deadmarshal/cpp/ch-2.cpp b/challenge-180/deadmarshal/cpp/ch-2.cpp
new file mode 100644
index 0000000000..1ae978920c
--- /dev/null
+++ b/challenge-180/deadmarshal/cpp/ch-2.cpp
@@ -0,0 +1,26 @@
+#include<iostream>
+#include<vector>
+#include<algorithm>
+
+int main()
+{
+ std::vector<int> n{1,4,2,3,5};
+ std::vector<int> n2{9,0,6,2,3,8,5};
+ int i = 3, i2 = 4;
+ auto removed = std::remove_if(n.begin(),
+ n.end(),
+ [&i](int num){return num <= i;});
+ n.erase(removed, n.end());
+ for(const int& item : n)
+ std::cout << item << ' ';
+ std::cout << '\n';
+
+ auto removed2 = std::remove_if(n2.begin(),
+ n2.end(),
+ [&i2](int num){return num <= i2;});
+ n2.erase(removed2, n2.end());
+ for(const int& item : n2)
+ std::cout << item << ' ';
+
+ return 0;
+}
diff --git a/challenge-180/deadmarshal/d/ch1.d b/challenge-180/deadmarshal/d/ch1.d
new file mode 100644
index 0000000000..9e116dc557
--- /dev/null
+++ b/challenge-180/deadmarshal/d/ch1.d
@@ -0,0 +1,32 @@
+import std.stdio:writeln,stderr;
+import std.format:format;
+import core.stdc.stdlib:exit;
+
+string first_unique_character(string str)
+{
+ string ret;
+ char[] chars = str.dup;
+ int[char] hash;
+ foreach(c;chars)
+ hash[c]++;
+ for(ulong i = 0; i < chars.length; ++i)
+ if(hash[chars[i]] == 1)
+ {
+ ret = format("%u as '%c' is the first unique character",
+ i,
+ chars[i]);
+ break;
+ }
+ return ret;
+}
+
+void main(string[] args)
+{
+ if(args.length != 2)
+ {
+ stderr.writeln("no arg(s) provided!");
+ exit(1);
+ }
+ writeln(first_unique_character(args[1]));
+}
+
diff --git a/challenge-180/deadmarshal/d/ch2.d b/challenge-180/deadmarshal/d/ch2.d
new file mode 100644
index 0000000000..d93355c611
--- /dev/null
+++ b/challenge-180/deadmarshal/d/ch2.d
@@ -0,0 +1,22 @@
+import std.stdio:writeln;
+import std.format:format;
+import std.algorithm:map,filter;
+import std.array:array,join;
+import std.conv:to;
+
+void main()
+{
+ int[] n = [1,4,2,3,5];
+ int[] n2 = [9,0,6,2,3,8,5];
+ int i = 3, i2 = 4;
+ writeln(format("(%s)",
+ filter!(a => a > i)(n)
+ .array
+ .map!(a => to!string(a))
+ .join(",")));
+ writeln(format("(%s)",
+ filter!(a => a > i2)(n2)
+ .array
+ .map!(a => to!string(a))
+ .join(",")));
+}
diff --git a/challenge-180/deadmarshal/lua/ch-1.lua b/challenge-180/deadmarshal/lua/ch-1.lua
new file mode 100644
index 0000000000..0ce7829ce2
--- /dev/null
+++ b/challenge-180/deadmarshal/lua/ch-1.lua
@@ -0,0 +1,22 @@
+if #arg ~= 1 then
+ io.stderr:write('No arg(s) provided!')
+ os.exit(1)
+end
+
+local function first_unique_character(str)
+ assert(type(str) == 'string', 'str must be a string!')
+ local chars, hash = {}, {}
+ str:gsub(".", function(c) table.insert(chars, c) end)
+ setmetatable(hash, {__index = function(t, k) return 0 end})
+ for i, v in ipairs(chars) do
+ hash[v] = hash[v] + 1
+ end
+ for i=1, #chars do
+ if hash[chars[i]] == 1 then
+ return string.format("%d as '%s' is the first unique character",
+ i-1, chars[i])
+ end
+ end
+end
+
+print(first_unique_character(arg[1]))
diff --git a/challenge-180/deadmarshal/lua/ch-2.lua b/challenge-180/deadmarshal/lua/ch-2.lua
new file mode 100644
index 0000000000..1cdd6f045a
--- /dev/null
+++ b/challenge-180/deadmarshal/lua/ch-2.lua
@@ -0,0 +1,13 @@
+local function trim_list(t, i)
+ assert(type(t) == 'table', 't must be a table!')
+ local filtered = {}
+ for j=1, #t do
+ if(t[j] > i) then table.insert(filtered, t[j]) end
+ end
+ return filtered
+end
+
+local n,i = {1,4,2,3,5}, 3
+local n2,i2 = {9,0,6,2,3,8,5}, 4
+print(table.unpack(trim_list(n,i)))
+print(table.unpack(trim_list(n2,i2)))
diff --git a/challenge-180/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-180/deadmarshal/modula-3/ch1/src/Ch1.m3
new file mode 100644
index 0000000000..b10db7aec0
--- /dev/null
+++ b/challenge-180/deadmarshal/modula-3/ch1/src/Ch1.m3
@@ -0,0 +1,34 @@
+MODULE Ch1 EXPORTS Main;
+
+IMPORT IO,Params,StableError,Text,Fmt;
+
+PROCEDURE FirstUniqueCharacter(Str:TEXT):TEXT =
+ VAR
+ Ret:TEXT;
+ Chars:REF ARRAY OF CHAR;
+ Hash:ARRAY[0..255] OF INTEGER := ARRAY[0..255] OF INTEGER{0, ..};
+ BEGIN
+ Chars := NEW(REF ARRAY OF CHAR, Text.Length(Str));
+ FOR I := 0 TO Text.Length(Str)-1 DO
+ Chars[I] := Text.GetChar(Str, I);
+ END;
+ FOR I := FIRST(Chars^) TO LAST(Chars^) DO
+ Hash[ORD(Chars[I])] := Hash[ORD(Chars[I])] + 1;
+ END;
+ FOR I := FIRST(Chars^) TO LAST(Chars^) DO
+ IF Hash[ORD(Chars[I])] = 1 THEN
+ Ret := Fmt.F("%s as '%s' is the first unique character\n",
+ Fmt.Int(I),
+ Fmt.Char(Chars[I]));
+ EXIT;
+ END;
+ END;
+ RETURN Ret;
+ END FirstUniqueCharacter;
+
+BEGIN
+ IF Params.Count # 2 THEN
+ StableError.Halt("No arg(s) provided!");
+ END;
+ IO.Put(FirstUniqueCharacter(Params.Get(1)));
+END Ch1.
diff --git a/challenge-180/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-180/deadmarshal/modula-3/ch1/src/m3makefile
new file mode 100644
index 0000000000..1f4f492255
--- /dev/null
+++ b/challenge-180/deadmarshal/modula-3/ch1/src/m3makefile
@@ -0,0 +1,4 @@
+import("libm3")
+import("stable")
+implementation("Ch1")
+program("ch1")
diff --git a/challenge-180/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-180/deadmarshal/modula-3/ch2/src/Ch2.m3
new file mode 100644
index 0000000000..7a4f318569
--- /dev/null
+++ b/challenge-180/deadmarshal/modula-3/ch2/src/Ch2.m3
@@ -0,0 +1,48 @@
+MODULE Ch2 EXPORTS Main;
+
+IMPORT IO;
+
+VAR
+ I,I2:INTEGER;
+ N,N2:REF ARRAY OF INTEGER;
+ NTrimmed,N2Trimmed:REF ARRAY OF INTEGER;
+
+PROCEDURE TrimList(VAR Arr:REF ARRAY OF INTEGER;
+ I:INTEGER):REF ARRAY OF INTEGER =
+VAR
+ K,Count:INTEGER := 0;
+ Ret:REF ARRAY OF INTEGER;
+BEGIN
+ FOR J := FIRST(Arr^) TO LAST(Arr^) DO
+ IF(Arr[J] > I) THEN INC(Count) END;
+ END;
+ Ret := NEW(REF ARRAY OF INTEGER, Count);
+ FOR J := FIRST(Arr^) TO LAST(Arr^) DO
+ IF(Arr[J] > I) THEN
+ Ret[K] := Arr[J];
+ INC(K);
+ END;
+ END;
+ RETURN Ret;
+END TrimList;
+
+BEGIN
+ I := 3;
+ I2 := 4;
+ N := NEW(REF ARRAY OF INTEGER, 5);
+ N2 := NEW(REF ARRAY OF INTEGER, 7);
+ N^ := ARRAY OF INTEGER{1,4,2,3,5};
+ N2^ := ARRAY OF INTEGER{9,0,6,2,3,8,5};
+ NTrimmed := TrimList(N, I);
+ N2Trimmed := TrimList(N2, I2);
+ FOR I := FIRST(NTrimmed^) TO LAST(NTrimmed^) DO
+ IO.PutInt(NTrimmed[I]);
+ IO.Put(" ");
+ END;
+ IO.Put("\n");
+ FOR I := FIRST(N2Trimmed^) TO LAST(N2Trimmed^) DO
+ IO.PutInt(N2Trimmed[I]);
+ IO.Put(" ");
+ END;
+ IO.Put("\n");
+END Ch2.
diff --git a/challenge-180/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-180/deadmarshal/modula-3/ch2/src/m3makefile
new file mode 100644
index 0000000000..4bfbb7373d
--- /dev/null
+++ b/challenge-180/deadmarshal/modula-3/ch2/src/m3makefile
@@ -0,0 +1,3 @@
+import("libm3")
+implementation("Ch2")
+program("ch2")
diff --git a/challenge-180/deadmarshal/nim/ch1.nim b/challenge-180/deadmarshal/nim/ch1.nim
new file mode 100644
index 0000000000..30a75b2df3
--- /dev/null
+++ b/challenge-180/deadmarshal/nim/ch1.nim
@@ -0,0 +1,23 @@
+import strutils,os,strformat
+
+proc FirstUniqueCharacter(Str:string):string =
+ var
+ Chars:seq[char]
+ Hash:array[255,int]
+
+ for I in 0..pred(Str.len):
+ Chars.add(Str[I])
+ for I in 0..Chars.len-1:
+ Hash[ord(Chars[I])] = Hash[ord(Chars[I])] + 1
+ for I in 0..Chars.len-1:
+ if Hash[ord(Chars[I])] == 1:
+ result = &"{I} as {Chars[I]} is the first unique character"
+ break
+
+proc main() =
+ if paramcount() != 1:
+ stderr.writeLine("No arg(s) provded!")
+ quit(1)
+ echo FirstUniqueCharacter(paramstr(1))
+
+main()
diff --git a/challenge-180/deadmarshal/nim/ch2.nim b/challenge-180/deadmarshal/nim/ch2.nim
new file mode 100644
index 0000000000..928d1c97de
--- /dev/null
+++ b/challenge-180/deadmarshal/nim/ch2.nim
@@ -0,0 +1,14 @@
+import sequtils
+
+proc TrimList(Seq:var seq[int]; i:int):seq[int] =
+ Seq.filter(proc(x:int):bool = x > i)
+
+var
+ n = @[1,4,2,3,5]
+ n2 = @[9,0,6,2,3,8,5]
+
+proc main() =
+ echo TrimList(n, 3)
+ echo TrimList(n2, 4)
+
+main()
diff --git a/challenge-180/deadmarshal/pascal/ch1.pas b/challenge-180/deadmarshal/pascal/ch1.pas
new file mode 100644
index 0000000000..44043e4018
--- /dev/null
+++ b/challenge-180/deadmarshal/pascal/ch1.pas
@@ -0,0 +1,35 @@
+program Ch1;
+
+{$mode objfpc}
+uses
+ SysUtils;
+
+function FirstUniqueCharacter(constref Str:AnsiString):AnsiString;
+var
+ I:Integer;
+ Chars:array of Char;
+ Hash:array[0..255] of Integer;
+begin
+ SetLength(Chars, Length(Str));
+ for I := Low(Str) to High(Str) do
+ Chars[I-1] := Str[I];
+ for I := Low(Hash) to High(Hash) do Hash[I] := 0;
+ for I := Low(Chars) to High(Chars) do
+ Hash[Ord(Chars[I])] := Hash[Ord(Chars[I])] + 1;
+ for I := Low(Chars) to High(Chars) do
+ if(Hash[Ord(Chars[I])] = 1) then
+ begin
+ Result := Format('%d as ''%s'' is the first unique character',
+ [I, Chars[I]]);
+ break;
+ end;
+end;
+
+begin
+ if(ParamCount <> 1) then
+ begin
+ WriteLn(StdErr, 'No arg(s) provided!');
+ Halt(1);
+ end;
+ WriteLn(FirstUniqueCharacter(ParamStr(1)));
+end.
diff --git a/challenge-180/deadmarshal/pascal/ch2.pas b/challenge-180/deadmarshal/pascal/ch2.pas
new file mode 100644
index 0000000000..abeaed6305
--- /dev/null
+++ b/challenge-180/deadmarshal/pascal/ch2.pas
@@ -0,0 +1,41 @@
+program Ch2;
+
+{$mode objfpc}
+uses
+ SysUtils,Types;
+var
+ I,I2:Integer;
+ N:TIntegerDynArray = (1,4,2,3,5);
+ N2:TIntegerDynArray = (9,0,6,2,3,8,5);
+ NTrimmed,N2Trimmed:TIntegerDynArray;
+
+function TrimList(constref Arr:TIntegerDynArray;
+ I:Integer):TIntegerDynArray;
+var
+ J,K,Count:Integer;
+begin
+ K := 0;
+ Count := 0;
+ for J := Low(Arr) to High(Arr) do
+ if(Arr[J] > I) then Inc(Count);
+ SetLength(Result, Count);
+ for J := Low(Arr) to High(Arr) do
+ if(Arr[J] > I) then
+ begin
+ Result[K] := Arr[J];
+ Inc(K);
+ end;
+end;
+
+begin
+ I := 3;
+ I2 := 4;
+ NTrimmed := TrimList(N, I);
+ N2Trimmed := TrimList(N2, I2);
+ for I := Low(NTrimmed) to High(NTrimmed) do
+ Write(NTrimmed[I], ' ');
+ WriteLn;
+ for I := Low(N2Trimmed) to High(N2Trimmed) do
+ Write(N2Trimmed[I], ' ');
+ WriteLn;
+end.
diff --git a/challenge-180/deadmarshal/perl/ch-1.pl b/challenge-180/deadmarshal/perl/ch-1.pl
new file mode 100644
index 0000000000..a5c6cc3c94
--- /dev/null
+++ b/challenge-180/deadmarshal/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+die "No arg(s) provided!" unless
+ defined $ARGV[0] && $ARGV[0] =~ /\w+/;
+
+sub first_unique_character{
+ my @chars = split '', $_[0];
+ my %hash;
+ $hash{$_}++ foreach @chars;
+ foreach my $i(0..$#chars){
+ if($hash{$chars[$i]} == 1){
+ return sprintf "%d as '%s' is the first unique character\n", $i, $chars[$i];
+ }
+ }
+}
+
+print first_unique_character($ARGV[0]);
diff --git a/challenge-180/deadmarshal/perl/ch-2.pl b/challenge-180/deadmarshal/perl/ch-2.pl
new file mode 100644
index 0000000000..c13979bc14
--- /dev/null
+++ b/challenge-180/deadmarshal/perl/ch-2.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+sub trim_list{
+ my ($arr, $i) = @_;
+ grep {$_ > $i} @$arr;
+}
+
+my ($n, $i) = ([1,4,2,3,5], 3);
+my ($n2, $i2) = ([9,0,6,2,3,8,5], 4);
+printf "(%s)\n" => join ',' => trim_list($n, $i);
+printf "(%s)\n" => join ',' => trim_list($n2, $i2);
diff --git a/challenge-180/deadmarshal/python/ch1.py b/challenge-180/deadmarshal/python/ch1.py
new file mode 100644
index 0000000000..a006f46cb3
--- /dev/null
+++ b/challenge-180/deadmarshal/python/ch1.py
@@ -0,0 +1,20 @@
+import sys
+
+def first_unique_character(s):
+ freq = {}
+ for i in s:
+ if i not in freq:
+ freq[i] = 1
+ else:
+ freq[i] += 1
+
+ for i in range(len(s)):
+ if(freq[s[i]] == 1):
+ return f"{i} as '{s[i]}' is the first unique character"
+ return -1
+
+if(len(sys.argv) != 2):
+ sys.stderr.write("No arg(s) provided!\n")
+ sys.exit(1)
+
+print(first_unique_character(sys.argv[1]))
diff --git a/challenge-180/deadmarshal/python/ch2.py b/challenge-180/deadmarshal/python/ch2.py
new file mode 100644
index 0000000000..7522545459
--- /dev/null
+++ b/challenge-180/deadmarshal/python/ch2.py
@@ -0,0 +1,9 @@
+def trim_list(arr, i):
+ return list(filter(lambda x: x > i, arr))
+
+i,i2 = 3, 4
+n = [1,4,2,3,5]
+n2 = [9,0,6,2,3,8,5]
+
+print(trim_list(n, i))
+print(trim_list(n2, i2))
diff --git a/challenge-180/deadmarshal/raku/ch-1.raku b/challenge-180/deadmarshal/raku/ch-1.raku
new file mode 100644
index 0000000000..6203b35b4f
--- /dev/null
+++ b/challenge-180/deadmarshal/raku/ch-1.raku
@@ -0,0 +1,20 @@
+sub first_unique_character($s)
+{
+ my @chars = $s.split('');
+ my %hash;
+ %hash{$_}++ for @chars;
+ for 0..@chars.elems -> $i
+ {
+ if (%hash{@chars[$i]} == 1)
+ {
+ return sprintf "%d as '%s' is the first unique character",
+ $i-1,
+ @chars[$i];
+ }
+ }
+}
+
+sub MAIN(Str $str)
+{
+ say first_unique_character($str);
+}
diff --git a/challenge-180/deadmarshal/raku/ch-2.raku b/challenge-180/deadmarshal/raku/ch-2.raku
new file mode 100644
index 0000000000..dbbcbc296c
--- /dev/null
+++ b/challenge-180/deadmarshal/raku/ch-2.raku
@@ -0,0 +1,8 @@
+sub MAIN()
+{
+ my ($i, $i2) = (3,4);
+ my @n = (1,4,2,3,5);
+ my @n2 = (9,0,6,2,3,8,5);
+ printf "(%s)\n", (grep {$_ > $i}, @n).join(',');
+ printf "(%s)\n", (grep {$_ > $i2}, @n2).join(',');
+}