diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2023-01-28 20:44:13 +0330 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2023-01-28 20:44:13 +0330 |
| commit | 79df3f502658e9e7622ce49faa32ee703d936f67 (patch) | |
| tree | fc3a7c9a5c0f8ef69b77122b254f62e3578effcf | |
| parent | 27b88f614b9bb53872ef0da19a56087505836db0 (diff) | |
| download | perlweeklychallenge-club-79df3f502658e9e7622ce49faa32ee703d936f67.tar.gz perlweeklychallenge-club-79df3f502658e9e7622ce49faa32ee703d936f67.tar.bz2 perlweeklychallenge-club-79df3f502658e9e7622ce49faa32ee703d936f67.zip | |
TWC201
20 files changed, 451 insertions, 0 deletions
diff --git a/challenge-201/deadmarshal/ada/ch1.adb b/challenge-201/deadmarshal/ada/ch1.adb new file mode 100644 index 0000000000..9a33bb1240 --- /dev/null +++ b/challenge-201/deadmarshal/ada/ch1.adb @@ -0,0 +1,33 @@ +with Ada.Containers.Ordered_Sets; +with Ada.Text_IO; use Ada.Text_IO; + +procedure Ch1 is + package Integer_Sets is new Ada.Containers.Ordered_Sets + (Element_Type => Integer); + use Integer_Sets; + + procedure Show_Elements(S:in Set) is + begin + for E of S loop + Put_Line(Integer'Image(E)); + end loop; + end Show_Elements; + + A,ALen,B,BLen:Set; +begin + A.Include(0); + A.Include(1); + A.Include(3); + ALen.Include(0); + ALen.Include(1); + ALen.Include(2); + ALen.Include(3); + B.Include(0); + B.Include(1); + BLen.Include(0); + BLen.Include(1); + BLen.Include(2); + Show_Elements(ALen - A); + Show_Elements(BLen - B); +end Ch1; + diff --git a/challenge-201/deadmarshal/ada/ch2.adb b/challenge-201/deadmarshal/ada/ch2.adb new file mode 100644 index 0000000000..1ce6235e8b --- /dev/null +++ b/challenge-201/deadmarshal/ada/ch2.adb @@ -0,0 +1,23 @@ +with Ada.Text_IO; use Ada.Text_IO; + +procedure Ch2 is + Count:Integer := 0; + type TArr is array(0..4) of integer; + Arr:TArr; + + procedure Find_Combinations(Arr:in out TArr;Index,Num,Reduced_Num:Integer) is + Prev:Integer; + begin + if Reduced_Num < 0 then return; end if; + if Reduced_Num = 0 then Count := Count + 1; return; end if; + if Index = 0 then Prev := 1; else Prev := Arr(Index-1); end if; + for I in Prev..Num loop + Arr(Index) := I; + Find_Combinations(Arr,Index+1,Num,Reduced_Num-I); + end loop; + end Find_Combinations; +begin + Find_Combinations(Arr,0,5,5); + Put_Line(Integer'Image(Count)); +end Ch2; + diff --git a/challenge-201/deadmarshal/c/ch-1.c b/challenge-201/deadmarshal/c/ch-1.c new file mode 100644 index 0000000000..46ec411f3e --- /dev/null +++ b/challenge-201/deadmarshal/c/ch-1.c @@ -0,0 +1,21 @@ +#include<stdio.h> +#include<stdlib.h> + +void missing_numbers(int *arr, size_t sz) +{ + int hash[10] = {0}; + for(size_t i = 0; i < sz; ++i) hash[arr[i] % 10] = 1; + for(size_t i = 0; i <= sz; ++i) + if(hash[i] != 1) printf("%d ",i); + puts(""); +} + +int main(void) +{ + int a1[3] = {0,1,3}; + int a2[2] = {0,1}; + size_t sz1 = 3,sz2 = 2; + missing_numbers(a1,sz1); + missing_numbers(a2,sz2); +} + diff --git a/challenge-201/deadmarshal/c/ch-2.c b/challenge-201/deadmarshal/c/ch-2.c new file mode 100644 index 0000000000..6d70f675f9 --- /dev/null +++ b/challenge-201/deadmarshal/c/ch-2.c @@ -0,0 +1,32 @@ +#include<stdio.h> +#include<stdlib.h> + +#define N 5 +static int count = 0; + +void find_combinations(int *arr, + int index, + int num, + int reduced_num) +{ + if(reduced_num < 0) return; + if(reduced_num == 0) + { + count++; + return; + } + int prev = index == 0 ? 1 : arr[index-1]; + for(int i = prev; i <= num; ++i) + { + arr[index] = i; + find_combinations(arr,index+1,num,reduced_num-i); + } +} + +int main(void) +{ + int arr[N]; + find_combinations(arr,0,N,N); + printf("%d\n",count); +} + diff --git a/challenge-201/deadmarshal/cpp/ch-1.cpp b/challenge-201/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..c1b8a37d4f --- /dev/null +++ b/challenge-201/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,22 @@ +#include<iostream> +#include<vector> +#include<unordered_map> + +template<typename T> +void missing_numbers(const std::vector<T> &vec) +{ + std::unordered_map<T,T> m{}; + for(size_t i = 0; i < vec.size(); ++i) m[vec[i]] = 1; + for(size_t i = 0; i < vec.size()+1; ++i) + if(m[i] != 1) std::cout << i << ' '; + std::cout << '\n'; +} + +int main() +{ + const std::vector<int> vec1{0,1,3},vec2{0,1}; + missing_numbers<int>(vec1); + missing_numbers<int>(vec2); + return 0; +} + diff --git a/challenge-201/deadmarshal/cpp/ch-2.cpp b/challenge-201/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..d05bc157e5 --- /dev/null +++ b/challenge-201/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,31 @@ +#include<iostream> + +#define N 5 +static int count{}; + +void find_combinations(int *arr, + int index, + int num, + int reduced_num) +{ + if(reduced_num < 0) return; + if(reduced_num == 0) + { + count++; + return; + } + int prev = index == 0 ? 1 : arr[index-1]; + for(int i = prev; i <= num; ++i) + { + arr[index] = i; + find_combinations(arr,index+1,num,reduced_num-i); + } +} + +int main() +{ + int arr[N]; + find_combinations(arr,0,N,N); + std::cout << count << '\n'; +} + diff --git a/challenge-201/deadmarshal/d/ch1.d b/challenge-201/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..bf2485ee33 --- /dev/null +++ b/challenge-201/deadmarshal/d/ch1.d @@ -0,0 +1,17 @@ +import std.stdio:write,writeln; + +void missing_numbers(int[] arr) +{ + int[int] m; + foreach(const ref i;arr) m[i] = i; + for(int i = 0; i <= arr.length; ++i) + if(i !in m) write(i, ' '); + writeln; +} + +void main() +{ + missing_numbers([0,1,3]); + missing_numbers([0,1]); +} + diff --git a/challenge-201/deadmarshal/d/ch2.d b/challenge-201/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..fe9fe2368e --- /dev/null +++ b/challenge-201/deadmarshal/d/ch2.d @@ -0,0 +1,31 @@ +import std.stdio:writeln; + +const int N = 5; +static int count = 0; + +void find_combinations(int[N] arr, + int index, + int num, + int reduced_num) +{ + if(reduced_num < 0) return; + if(reduced_num == 0) + { + count++; + return; + } + int prev = index == 0 ? 1 : arr[index-1]; + for(int i = prev; i <= num; ++i) + { + arr[index] = i; + find_combinations(arr,index+1,num,reduced_num-i); + } +} + +void main() +{ + int[N] arr; + find_combinations(arr,0,N,N); + writeln(count); +} + diff --git a/challenge-201/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-201/deadmarshal/modula-3/ch1/src/Ch1.m3 new file mode 100644 index 0000000000..5404f13d19 --- /dev/null +++ b/challenge-201/deadmarshal/modula-3/ch1/src/Ch1.m3 @@ -0,0 +1,25 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT IO; + +VAR + A1:ARRAY[0..2] OF INTEGER := ARRAY OF INTEGER{0,1,3}; + A2:ARRAY[0..1] OF INTEGER := ARRAY OF INTEGER{0,1}; + +PROCEDURE MissingNumbers(VAR Arr:ARRAY OF INTEGER) = + VAR + Hash:ARRAY[0..9] OF INTEGER; + BEGIN + FOR I := 0 TO 9 DO Hash[I] := 0 END; + FOR I := FIRST(Arr) TO LAST(Arr) DO Hash[Arr[I] MOD 10] := 1 END; + FOR I := FIRST(Arr) TO NUMBER(Arr) DO + IF Hash[I] # 1 THEN IO.PutInt(I); IO.PutChar(' ') END; + END; + IO.Put("\n"); + END MissingNumbers; + +BEGIN + MissingNumbers(A1); + MissingNumbers(A2); +END Ch1. + diff --git a/challenge-201/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-201/deadmarshal/modula-3/ch1/src/m3makefile new file mode 100644 index 0000000000..0ee72d695b --- /dev/null +++ b/challenge-201/deadmarshal/modula-3/ch1/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +implementation("Ch1") +program("ch1") + diff --git a/challenge-201/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-201/deadmarshal/modula-3/ch2/src/Ch2.m3 new file mode 100644 index 0000000000..b298e7fd87 --- /dev/null +++ b/challenge-201/deadmarshal/modula-3/ch2/src/Ch2.m3 @@ -0,0 +1,30 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT IO; + +VAR + Count:INTEGER := 0; + Arr:ARRAY[0..4] OF INTEGER; + + PROCEDURE FindCombinations(VAR Arr:ARRAY OF INTEGER; + Index,Num,Reducednum:INTEGER) = + VAR + Prev:INTEGER; + BEGIN + IF Reducednum < 0 THEN RETURN END; + IF Reducednum = 0 THEN + INC(Count); + RETURN; + END; + IF Index = 0 THEN Prev := 1 ELSE Prev := Arr[Index-1] END; + FOR I := Prev TO Num DO + Arr[Index] := I; + FindCombinations(Arr,Index+1,Num,Reducednum-I); + END; + END FindCombinations; + +BEGIN + FindCombinations(Arr,0,5,5); + IO.PutInt(Count); IO.Put("\n"); +END Ch2. + diff --git a/challenge-201/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-201/deadmarshal/modula-3/ch2/src/m3makefile new file mode 100644 index 0000000000..5c32bbc4bb --- /dev/null +++ b/challenge-201/deadmarshal/modula-3/ch2/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +implementation("Ch2") +program("ch2") + diff --git a/challenge-201/deadmarshal/oberon/Ch1.Mod b/challenge-201/deadmarshal/oberon/Ch1.Mod new file mode 100644 index 0000000000..2b357edd53 --- /dev/null +++ b/challenge-201/deadmarshal/oberon/Ch1.Mod @@ -0,0 +1,33 @@ +MODULE Ch1; + + IMPORT Out; + + VAR + A1:ARRAY 3 OF INTEGER; + A2:ARRAY 2 OF INTEGER; + + PROCEDURE Init; + BEGIN + A1[0] := 0; A1[1] := 1; A1[2] := 3; + A2[0] := 0; A2[1] := 1; + END Init; + + PROCEDURE MissingNumbers(VAR arr:ARRAY OF INTEGER); + VAR + i:LONGINT; + hash:ARRAY 10 OF INTEGER; + BEGIN + FOR i := 0 TO 9 DO hash[i] := 0 END; + FOR i := 0 TO LEN(arr)-1 DO hash[arr[i] MOD 10] := 1 END; + FOR i := 0 TO LEN(arr) DO + IF hash[i] # 1 THEN Out.Int(i,0); Out.Char(' ') END; + END; + Out.Ln; + END MissingNumbers; + +BEGIN + Init; + MissingNumbers(A1); + MissingNumbers(A2); +END Ch1. + diff --git a/challenge-201/deadmarshal/oberon/Ch2.Mod b/challenge-201/deadmarshal/oberon/Ch2.Mod new file mode 100644 index 0000000000..ba63d66bfa --- /dev/null +++ b/challenge-201/deadmarshal/oberon/Ch2.Mod @@ -0,0 +1,32 @@ +MODULE Ch2; + + IMPORT Out; + + VAR + count:INTEGER; + arr:ARRAY 5 OF INTEGER; + + PROCEDURE FindCombinations(VAR arr:ARRAY OF INTEGER; + index,num,reducednum:INTEGER); + VAR + i:LONGINT; + prev:INTEGER; + BEGIN + IF reducednum < 0 THEN RETURN END; + IF reducednum = 0 THEN + INC(count); + RETURN; + END; + IF index = 0 THEN prev := 1 ELSE prev := arr[index-1] END; + FOR i := prev TO num DO + arr[index] := SHORT(i); + FindCombinations(arr,index+1,num,reducednum-SHORT(i)); + END; + END FindCombinations; + +BEGIN + count := 0; + FindCombinations(arr,0,5,5); + Out.Int(count,0); Out.Ln; +END Ch2. + diff --git a/challenge-201/deadmarshal/pascal/ch1.pas b/challenge-201/deadmarshal/pascal/ch1.pas new file mode 100644 index 0000000000..00d60a6cf5 --- /dev/null +++ b/challenge-201/deadmarshal/pascal/ch1.pas @@ -0,0 +1,29 @@ +program Ch1; + +{$mode objfpc} + +uses + SysUtils,Types; + +var + A1,A2:TIntegerDynArray; + +procedure MissingNumbers(Arr:TIntegerDynArray); +var + I:Integer; + Hash:array[0..9] of Integer; +begin + FillDword(Hash,10,0); + for I := Low(Arr) to High(Arr) do Hash[Arr[I] mod 10] := 1; + for I := Low(Arr) to Length(Arr) do + if Hash[I] <> 1 then Write(I,' '); + WriteLn; +end; + +begin + A1 := [0,1,3]; + A2 := [0,1]; + MissingNumbers(A1); + MissingNumbers(A2); +end. + diff --git a/challenge-201/deadmarshal/pascal/ch2.pas b/challenge-201/deadmarshal/pascal/ch2.pas new file mode 100644 index 0000000000..0b7889aa42 --- /dev/null +++ b/challenge-201/deadmarshal/pascal/ch2.pas @@ -0,0 +1,38 @@ +program Ch2; + +{$mode objfpc} + +uses + SysUtils; + +const + N:Integer = 5; + +var + Count:Integer = 0; + Arr:array[0..4] of Integer; + +procedure FindCombinations(var Arr:array of Integer; + Index,Num,ReducedNum:Integer); +var + I,Prev:Integer; +begin + if ReducedNum < 0 then Exit; + if ReducedNum = 0 then + begin + inc(Count); + Exit; + end; + if Index = 0 then Prev := 1 else Prev := Arr[Index-1]; + for I := Prev to Num do + begin + Arr[Index] := I; + FindCombinations(Arr,Index+1,Num,ReducedNum-I); + end; +end; + +begin + FindCombinations(Arr,0,N,N); + WriteLn(Count); +end. + diff --git a/challenge-201/deadmarshal/perl/ch-1.pl b/challenge-201/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..b3f29407f5 --- /dev/null +++ b/challenge-201/deadmarshal/perl/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Set::Scalar; + +sub missing_numbers{ + my ($a,$b) = (Set::Scalar->new,Set::Scalar->new); + $a->insert(@{$_[0]}); + $b->insert(0..@{$_[0]}); + $b - $a; +} + +printf "%s\n", missing_numbers([0,1,3]); +printf "%s\n", missing_numbers([0,1]); + diff --git a/challenge-201/deadmarshal/perl/ch-2.pl b/challenge-201/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..5d30215883 --- /dev/null +++ b/challenge-201/deadmarshal/perl/ch-2.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +my ($count,@arr) =(0,(0) x 5); +penny_piles(\@arr,0,5,5); +print "$count\n"; + +sub penny_piles{ + my ($arr,$index,$num,$reducednum) = @_; + return if $reducednum < 0; + do{$count++; return} if $reducednum == 0; + my $prev = $index == 0 ? 1 : $arr->[$index-1]; + foreach my $i($prev..$num){ + $arr->[$index] = $i; + penny_piles($arr,$index+1,$num,$reducednum-$i); + } +} + diff --git a/challenge-201/deadmarshal/raku/ch-1.raku b/challenge-201/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..5273eeb522 --- /dev/null +++ b/challenge-201/deadmarshal/raku/ch-1.raku @@ -0,0 +1,8 @@ +sub missing-numbers(@arr) +{ + ((0..max(max(@arr),@arr.elems)) (-) @arr).keys; +} + +say missing-numbers([0,1,3]); +say missing-numbers([0,1]); + diff --git a/challenge-201/deadmarshal/raku/ch-2.raku b/challenge-201/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..a38065edc5 --- /dev/null +++ b/challenge-201/deadmarshal/raku/ch-2.raku @@ -0,0 +1,4 @@ +#!/usr/bin/env raku +use Math::Combinatorics <partitions>; +say partitions(5).elems; + |
