aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordeadmarshal <adeadmarshal@gmail.com>2023-09-27 21:19:23 +0330
committerdeadmarshal <adeadmarshal@gmail.com>2023-09-27 21:19:23 +0330
commita3cf4ec084c21631ceb70a03f5760e6dc6f616ee (patch)
tree9292158bbcc60d13748835be1ff48e94b35a4593
parent200e728ec7b29b6f1807ed966264407834eb9f92 (diff)
downloadperlweeklychallenge-club-a3cf4ec084c21631ceb70a03f5760e6dc6f616ee.tar.gz
perlweeklychallenge-club-a3cf4ec084c21631ceb70a03f5760e6dc6f616ee.tar.bz2
perlweeklychallenge-club-a3cf4ec084c21631ceb70a03f5760e6dc6f616ee.zip
TWC236
-rw-r--r--challenge-236/deadmarshal/blog.txt1
-rw-r--r--challenge-236/deadmarshal/c/ch-1.c49
-rw-r--r--challenge-236/deadmarshal/c/ch-2.c37
-rw-r--r--challenge-236/deadmarshal/d/ch1.d30
-rw-r--r--challenge-236/deadmarshal/d/ch2.d33
-rw-r--r--challenge-236/deadmarshal/modula-3/ch1/src/Ch1.m341
-rw-r--r--challenge-236/deadmarshal/modula-3/ch1/src/m3makefile4
-rw-r--r--challenge-236/deadmarshal/modula-3/ch2/src/Ch2.m338
-rw-r--r--challenge-236/deadmarshal/modula-3/ch2/src/m3makefile4
-rw-r--r--challenge-236/deadmarshal/oberon/Ch1.Mod49
-rw-r--r--challenge-236/deadmarshal/oberon/Ch2.Mod50
-rw-r--r--challenge-236/deadmarshal/pascal/ch1.pas54
-rw-r--r--challenge-236/deadmarshal/pascal/ch2.pas42
-rw-r--r--challenge-236/deadmarshal/perl/ch-1.pl24
-rw-r--r--challenge-236/deadmarshal/perl/ch-2.pl24
-rw-r--r--challenge-236/deadmarshal/ruby/ch1.rb26
-rw-r--r--challenge-236/deadmarshal/ruby/ch2.rb19
17 files changed, 525 insertions, 0 deletions
diff --git a/challenge-236/deadmarshal/blog.txt b/challenge-236/deadmarshal/blog.txt
new file mode 100644
index 0000000000..03aabc4ff0
--- /dev/null
+++ b/challenge-236/deadmarshal/blog.txt
@@ -0,0 +1 @@
+https://deadmarshal.blogspot.com/2023/09/twc236.html
diff --git a/challenge-236/deadmarshal/c/ch-1.c b/challenge-236/deadmarshal/c/ch-1.c
new file mode 100644
index 0000000000..a3e3badfa1
--- /dev/null
+++ b/challenge-236/deadmarshal/c/ch-1.c
@@ -0,0 +1,49 @@
+#include<stdio.h>
+
+int exact_change(int *arr,size_t sz)
+{
+ int fives = 0,tens = 0,twenties = 0;
+ for(size_t i = 0; i < sz; ++i)
+ {
+ if(arr[i] == 10) if(!--fives) return 0;
+ else if(arr[i] == 20)
+ {
+ if(fives && tens)
+ {
+ fives--;
+ tens--;
+ }
+ else if(fives > 2) fives -= 3;
+ else return 0;
+ }
+ switch(arr[i])
+ {
+ case 5:
+ fives++;
+ break;
+ case 10:
+ tens++;
+ break;
+ case 20:
+ twenties++;
+ break;
+ default:
+ fprintf(stderr,"Only 5,10,20 allowed!");
+ return 0;
+ }
+ }
+ return 1;
+}
+
+int main(void)
+{
+ int arr1[] = {5,5,5,10,20};
+ int arr2[] = {5,5,10,10,20};
+ int arr3[] = {5,5,5,20};
+ size_t sz1 = 5,sz2 = 5, sz3 = 4;
+ printf("%d\n",exact_change(arr1,sz1));
+ printf("%d\n",exact_change(arr2,sz2));
+ printf("%d\n",exact_change(arr3,sz3));
+ return 0;
+}
+
diff --git a/challenge-236/deadmarshal/c/ch-2.c b/challenge-236/deadmarshal/c/ch-2.c
new file mode 100644
index 0000000000..4032a4435d
--- /dev/null
+++ b/challenge-236/deadmarshal/c/ch-2.c
@@ -0,0 +1,37 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<string.h>
+
+size_t array_loops(int *arr,size_t sz)
+{
+ size_t count = 0;
+ int *indices = malloc(sz*sizeof(*arr));
+ memset(indices,-1,sz*sizeof(*arr));
+ for(size_t i = 0; i < sz; ++i)
+ {
+ if(indices[i] == -1)
+ {
+ count++;
+ while(indices[i] == -1)
+ {
+ indices[i] = 1;
+ i = arr[i];
+ }
+ }
+ }
+ free(indices);
+ return count;
+}
+
+int main(void)
+{
+ int arr1[] = {4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10};
+ int arr2[] = {0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19};
+ int arr3[] = {9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17};
+ const size_t sz1 = 20,sz2 = 20,sz3 = 20;
+ printf("%zu\n",array_loops(arr1,sz1));
+ printf("%zu\n",array_loops(arr2,sz2));
+ printf("%zu\n",array_loops(arr3,sz3));
+ return 0;
+}
+
diff --git a/challenge-236/deadmarshal/d/ch1.d b/challenge-236/deadmarshal/d/ch1.d
new file mode 100644
index 0000000000..29015b28a2
--- /dev/null
+++ b/challenge-236/deadmarshal/d/ch1.d
@@ -0,0 +1,30 @@
+import std.stdio:writeln;
+
+int exact_change(int[] arr)
+{
+ int[int] hash;
+ foreach(e;arr)
+ {
+ if(e == 10) if(!--hash[5]) return false;
+ else if(e == 20)
+ {
+ if(hash[5] && hash[10])
+ {
+ hash[5]--;
+ hash[10]--;
+ }
+ else if(hash[5] > 2) hash[5] -= 3;
+ else return false;
+ }
+ hash[e]++;
+ }
+ return true;
+}
+
+void main()
+{
+ writeln(exact_change([5,5,5,10,20]));
+ writeln(exact_change([5,5,10,10,20]));
+ writeln(exact_change([5,5,5,20]));
+}
+
diff --git a/challenge-236/deadmarshal/d/ch2.d b/challenge-236/deadmarshal/d/ch2.d
new file mode 100644
index 0000000000..9d22f2fc55
--- /dev/null
+++ b/challenge-236/deadmarshal/d/ch2.d
@@ -0,0 +1,33 @@
+import std.stdio:writeln;
+
+ulong array_loops(ref int[] arr)
+{
+ ulong count = 0,i = 0;
+ int[] indices = new int[arr.length];
+ indices[] = -1;
+ while(i < arr.length)
+ {
+ if(indices[i] == -1)
+ {
+ count++;
+ while(indices[i] == -1)
+ {
+ indices[i] = 1;
+ i = arr[i];
+ }
+ }
+ i++;
+ }
+ return count;
+}
+
+void main()
+{
+ int[] arr1 = [4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10];
+ int[] arr2 = [0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19];
+ int[] arr3 = [9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17];
+ writeln(array_loops(arr1));
+ writeln(array_loops(arr2));
+ writeln(array_loops(arr3));
+}
+
diff --git a/challenge-236/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-236/deadmarshal/modula-3/ch1/src/Ch1.m3
new file mode 100644
index 0000000000..095503bb93
--- /dev/null
+++ b/challenge-236/deadmarshal/modula-3/ch1/src/Ch1.m3
@@ -0,0 +1,41 @@
+MODULE Ch1 EXPORTS Main;
+
+IMPORT IO;
+
+VAR
+ A1:ARRAY[0..4] OF INTEGER := ARRAY OF INTEGER{5,5,5,10,20};
+ A2:ARRAY[0..4] OF INTEGER := ARRAY OF INTEGER{5,5,10,10,20};
+ A3:ARRAY[0..3] OF INTEGER := ARRAY OF INTEGER{5,5,5,20};
+
+PROCEDURE ExactExchange(VAR A:ARRAY OF INTEGER):INTEGER =
+ VAR
+ Fives,Tens,Twenties:INTEGER := 0;
+ BEGIN
+ FOR I := FIRST(A) TO LAST(A) DO
+ IF A[I] = 10 THEN
+ DEC(Fives);
+ IF Fives = 0 THEN RETURN 0 END
+ ELSIF A[I] = 20 THEN
+ IF (Fives # 0) AND (Tens # 0) THEN DEC(Fives); DEC(Tens)
+ ELSIF Fives > 2 THEN DEC(Fives,3)
+ ELSE RETURN 0
+ END
+ END;
+ CASE A[I] OF
+ | 5 => INC(Fives)
+ | 10 => INC(Tens)
+ | 20 => INC(Twenties)
+ ELSE
+ IO.Put("Only 5,10,20 allowed!\n");
+ RETURN 0
+ END
+ END;
+ RETURN 1
+ END ExactExchange;
+
+BEGIN
+ IO.PutInt(ExactExchange(A1)); IO.Put("\n");
+ IO.PutInt(ExactExchange(A2)); IO.Put("\n");
+ IO.PutInt(ExactExchange(A3)); IO.Put("\n");
+END Ch1.
+
diff --git a/challenge-236/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-236/deadmarshal/modula-3/ch1/src/m3makefile
new file mode 100644
index 0000000000..0ee72d695b
--- /dev/null
+++ b/challenge-236/deadmarshal/modula-3/ch1/src/m3makefile
@@ -0,0 +1,4 @@
+import("libm3")
+implementation("Ch1")
+program("ch1")
+
diff --git a/challenge-236/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-236/deadmarshal/modula-3/ch2/src/Ch2.m3
new file mode 100644
index 0000000000..2fbfddfb08
--- /dev/null
+++ b/challenge-236/deadmarshal/modula-3/ch2/src/Ch2.m3
@@ -0,0 +1,38 @@
+MODULE Ch2 EXPORTS Main;
+
+IMPORT IO;
+
+VAR
+ A1:ARRAY[0..19] OF INTEGER :=
+ ARRAY OF INTEGER{4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10};
+ A2:ARRAY[0..19] OF INTEGER :=
+ ARRAY OF INTEGER{0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19};
+ A3:ARRAY[0..19] OF INTEGER :=
+ ARRAY OF INTEGER{9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17};
+
+PROCEDURE ArrayLoops(VAR A:ARRAY OF INTEGER):INTEGER =
+ VAR
+ I,Count:INTEGER := 0;
+ Indices:REF ARRAY OF INTEGER;
+ BEGIN
+ Indices := NEW(REF ARRAY OF INTEGER,NUMBER(A));
+ FOR I := FIRST(Indices^) TO LAST(Indices^) DO Indices[I] := -1 END;
+ WHILE I <= LAST(A) DO
+ IF Indices[I] = -1 THEN
+ INC(Count);
+ WHILE Indices[I] = -1 DO
+ Indices[I] := 1;
+ I := A[I];
+ END
+ END;
+ INC(I);
+ END;
+ RETURN Count
+ END ArrayLoops;
+
+BEGIN
+ IO.PutInt(ArrayLoops(A1)); IO.Put("\n");
+ IO.PutInt(ArrayLoops(A2)); IO.Put("\n");
+ IO.PutInt(ArrayLoops(A3)); IO.Put("\n");
+END Ch2.
+
diff --git a/challenge-236/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-236/deadmarshal/modula-3/ch2/src/m3makefile
new file mode 100644
index 0000000000..5c32bbc4bb
--- /dev/null
+++ b/challenge-236/deadmarshal/modula-3/ch2/src/m3makefile
@@ -0,0 +1,4 @@
+import("libm3")
+implementation("Ch2")
+program("ch2")
+
diff --git a/challenge-236/deadmarshal/oberon/Ch1.Mod b/challenge-236/deadmarshal/oberon/Ch1.Mod
new file mode 100644
index 0000000000..de2147ab1b
--- /dev/null
+++ b/challenge-236/deadmarshal/oberon/Ch1.Mod
@@ -0,0 +1,49 @@
+MODULE Ch1;
+
+ IMPORT Out;
+
+ VAR
+ A1,A2:ARRAY 5 OF LONGINT;
+ A3:ARRAY 4 OF LONGINT;
+
+ PROCEDURE Init;
+ BEGIN
+ A1[0] := 5; A1[1] := 5; A1[2] := 5; A1[3] := 10; A1[4] := 20;
+ A2[0] := 5; A2[1] := 5; A2[2] := 10; A2[3] := 10; A2[4] := 20;
+ A3[0] := 5; A3[1] := 5; A3[2] := 5; A3[3] := 20;
+ END Init;
+
+ PROCEDURE ExactExchange(VAR arr:ARRAY OF LONGINT):LONGINT;
+ VAR
+ i,fives,tens,twenties:LONGINT;
+ BEGIN
+ fives := 0; tens := 0; twenties := 0;
+ FOR i := 0 TO LEN(arr)-1 DO
+ IF arr[i] = 10 THEN
+ DEC(fives);
+ IF fives = 0 THEN RETURN 0 END
+ ELSIF arr[i] = 20 THEN
+ IF (fives # 0) & (tens # 0) THEN DEC(fives); DEC(tens)
+ ELSIF fives > 2 THEN DEC(fives,3)
+ ELSE RETURN 0
+ END
+ END;
+ CASE arr[i] OF
+ | 5: INC(fives)
+ | 10: INC(tens)
+ | 20: INC(twenties)
+ ELSE
+ Out.String("Only 5,10,20 allowed!"); Out.Ln;
+ RETURN 0
+ END
+ END;
+ RETURN 1
+ END ExactExchange;
+
+BEGIN
+ Init;
+ Out.Int(ExactExchange(A1),0); Out.Ln;
+ Out.Int(ExactExchange(A2),0); Out.Ln;
+ Out.Int(ExactExchange(A3),0); Out.Ln;
+END Ch1.
+
diff --git a/challenge-236/deadmarshal/oberon/Ch2.Mod b/challenge-236/deadmarshal/oberon/Ch2.Mod
new file mode 100644
index 0000000000..075c91bf9d
--- /dev/null
+++ b/challenge-236/deadmarshal/oberon/Ch2.Mod
@@ -0,0 +1,50 @@
+MODULE Ch2;
+
+ IMPORT Out;
+
+ VAR
+ A1,A2,A3:ARRAY 20 OF LONGINT;
+
+ PROCEDURE Init;
+ BEGIN
+ A1[0] := 4; A1[1] := 6; A1[2] := 3; A1[3] := 8; A1[4] := 15;
+ A1[5] := 0; A1[6] := 13; A1[7] := 18; A1[8] := 7; A1[9] := 16;
+ A1[10] := 14; A1[11] := 19; A1[12] := 17; A1[13] := 5; A1[14] := 11;
+ A1[15] := 1; A1[16] := 12; A1[17] := 2; A1[18] := 9; A1[19] := 10;
+ A2[0] := 0; A2[1] := 1; A2[2] := 13; A2[3] := 7; A2[4] := 6;
+ A2[5] := 8; A2[6] := 10; A2[7] := 11; A2[8] := 2; A2[9] := 14;
+ A2[10] := 16; A2[11] := 4; A2[12] := 12; A2[13] := 9; A2[14] := 17;
+ A2[15] := 5; A2[16] := 3; A2[17] := 18; A2[18] := 15; A2[19] := 19;
+ A3[0] := 9; A3[1] := 8; A3[2] := 3; A3[3] := 11; A3[4] := 5; A3[5] := 7;
+ A3[6] := 13; A3[7] := 19; A3[8] := 12; A3[9] := 4; A3[10] := 14;
+ A3[11] := 10; A3[12] := 18; A3[13] := 2; A3[14] := 16; A3[15] := 1;
+ A3[16] := 0; A3[17] := 15; A3[18] := 6; A3[19] := 17;
+ END Init;
+
+ PROCEDURE ArrayLoops(VAR arr:ARRAY OF LONGINT):LONGINT;
+ VAR
+ i,count:LONGINT;
+ indices:POINTER TO ARRAY OF LONGINT;
+ BEGIN
+ count := 0;
+ NEW(indices,LEN(arr));
+ FOR i := 0 TO LEN(indices^)-1 DO indices[i] := -1 END;
+ FOR i := 0 TO LEN(arr)-1 DO
+ IF indices[i] = -1 THEN
+ INC(count);
+ WHILE indices[i] = -1 DO
+ indices[i] := 1;
+ i := arr[i];
+ END
+ END;
+ END;
+ RETURN count
+ END ArrayLoops;
+
+BEGIN
+ Init;
+ Out.Int(ArrayLoops(A1),0); Out.Ln;
+ Out.Int(ArrayLoops(A2),0); Out.Ln;
+ Out.Int(ArrayLoops(A3),0); Out.Ln;
+END Ch2.
+
diff --git a/challenge-236/deadmarshal/pascal/ch1.pas b/challenge-236/deadmarshal/pascal/ch1.pas
new file mode 100644
index 0000000000..b0c5aa45d0
--- /dev/null
+++ b/challenge-236/deadmarshal/pascal/ch1.pas
@@ -0,0 +1,54 @@
+program Ch1;
+
+{$mode objfpc}
+uses
+ SysUtils,Types;
+
+var
+ A1,A2,A3:TIntegerDynArray;
+
+function ExactExchange(var Arr:TIntegerDynArray):Boolean;
+var
+ I,Fives,Tens,Twenties:Integer;
+begin
+ Fives := 0; Tens := 0; Twenties := 0;
+ for I := Low(Arr) to High(Arr) do
+ begin
+ if Arr[I] = 10 then
+ begin
+ Dec(Fives);
+ if Fives = 0 then Exit(False);
+ end
+ else if Arr[I] = 20 then
+ begin
+ if (Fives <> 0) and (Tens <> 0) then
+ begin
+ Dec(Fives);
+ Dec(Tens);
+ end
+ else if Fives > 2 then Dec(Fives,3)
+ else Exit(False);
+ end;
+ case Arr[I] of
+ 5: Inc(Fives);
+ 10: Inc(Tens);
+ 20: Inc(Twenties);
+ else
+ begin
+ WriteLn(StdErr,'Only 5,10,20 allowed!');
+ Exit(False);
+ end;
+ end;
+ end;
+ Exit(True);
+end;
+
+begin
+ A1 := [5,5,5,10,20];
+ A2 := [5,5,10,10,20];
+ A3 := [5,5,5,20];
+ WriteLn(ExactExchange(A1));
+ WriteLn(ExactExchange(A2));
+ WriteLn(ExactExchange(A3));
+end.
+
diff --git a/challenge-236/deadmarshal/pascal/ch2.pas b/challenge-236/deadmarshal/pascal/ch2.pas
new file mode 100644
index 0000000000..b98796652c
--- /dev/null
+++ b/challenge-236/deadmarshal/pascal/ch2.pas
@@ -0,0 +1,42 @@
+program Ch2;
+
+{$mode objfpc}
+uses
+ SysUtils,Types;
+
+var
+ A1,A2,A3:TIntegerDynArray;
+
+function ArrayLoops(var Arr:TIntegerDynArray):Integer;
+var
+ I:Integer;
+ Indices:TIntegerDynArray;
+begin
+ Result := 0;
+ SetLength(Indices,Length(Arr));
+ for I := Low(Indices) to High(Indices) do Indices[I] := -1;
+ I := 0;
+ while I <= High(Arr) do
+ begin
+ if Indices[I] = -1 then
+ begin
+ Inc(Result);
+ while Indices[I] = -1 do
+ begin
+ Indices[I] := 1;
+ I := Arr[I];
+ end;
+ end;
+ Inc(I);
+ end;
+end;
+
+begin
+ A1 := [4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10];
+ A2 := [0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19];
+ A3 := [9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17];
+ WriteLn(ArrayLoops(A1));
+ WriteLn(ArrayLoops(A2));
+ WriteLn(ArrayLoops(A3));
+end.
+
diff --git a/challenge-236/deadmarshal/perl/ch-1.pl b/challenge-236/deadmarshal/perl/ch-1.pl
new file mode 100644
index 0000000000..b32afddb1c
--- /dev/null
+++ b/challenge-236/deadmarshal/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+sub exact_change{
+ my ($arr) = @_;
+ my %hash;
+ foreach(@$arr){
+ if($_ == 10){
+ return 0 unless $hash{5}--;
+ }
+ elsif($_ == 20){
+ $hash{5} && $hash{10} ? do{--$hash{$_} foreach(5,10)} :
+ $hash{5} > 2 ? $hash{5} -= 3 : return 0;
+ }
+ $hash{$_}++;
+ }
+ 1
+}
+
+printf "%d\n",exact_change([5,5,5,10,20]);
+printf "%d\n",exact_change([5,5,10,10,20]);
+printf "%d\n",exact_change([5,5,5,20]);
+
diff --git a/challenge-236/deadmarshal/perl/ch-2.pl b/challenge-236/deadmarshal/perl/ch-2.pl
new file mode 100644
index 0000000000..a44c5fdb7d
--- /dev/null
+++ b/challenge-236/deadmarshal/perl/ch-2.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+sub array_loops{
+ my ($count,@indices) = (0);
+ foreach my $i(0..$#{$_[0]}){
+ next if $indices[$i];
+ $count++;
+ while(!$indices[$i]){
+ $indices[$i] = 1;
+ $i = $_[0]->[$i];
+ }
+ }
+ $count
+}
+
+printf "%d\n",
+ array_loops([4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10]);
+printf "%d\n",
+ array_loops([0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19]);
+printf "%d\n",
+ array_loops([9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17]);
+
diff --git a/challenge-236/deadmarshal/ruby/ch1.rb b/challenge-236/deadmarshal/ruby/ch1.rb
new file mode 100644
index 0000000000..a9d7b346e8
--- /dev/null
+++ b/challenge-236/deadmarshal/ruby/ch1.rb
@@ -0,0 +1,26 @@
+#!/usr/bin/env ruby
+
+def exact_change(arr)
+ hash = Hash.new(0)
+ arr.each do |n|
+ if n == 10
+ return false unless hash[5] -= 1
+ elsif n == 20
+ if hash[5] > 0 && hash[10] > 0
+ hash[5] -= 1
+ hash[10] -= 1
+ elsif hash[5] > 2
+ hash[5] -= 3
+ else
+ return false
+ end
+ end
+ hash[n] += 1
+ end
+ true
+end
+
+p exact_change([5,5,5,10,20])
+p exact_change([5,5,10,10,20])
+p exact_change([5,5,5,20])
+
diff --git a/challenge-236/deadmarshal/ruby/ch2.rb b/challenge-236/deadmarshal/ruby/ch2.rb
new file mode 100644
index 0000000000..cf9b4dbb7b
--- /dev/null
+++ b/challenge-236/deadmarshal/ruby/ch2.rb
@@ -0,0 +1,19 @@
+#!/usr/bin/env ruby
+
+def array_loops(arr)
+ count,indices = 0,[]
+ arr.each do |i|
+ next if indices[i]
+ count += 1
+ while not indices[i]
+ indices[i] = 1
+ i = arr[i]
+ end
+ end
+ count
+end
+
+p array_loops([4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10])
+p array_loops([0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19])
+p array_loops([9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17])
+