aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-087/paulo-custodio/Makefile2
-rw-r--r--challenge-087/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-087/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-087/paulo-custodio/python/ch-1.py45
-rw-r--r--challenge-087/paulo-custodio/python/ch-2.py122
-rw-r--r--challenge-087/paulo-custodio/t/test-1.yaml20
-rw-r--r--challenge-087/paulo-custodio/t/test-2.yaml67
-rw-r--r--challenge-087/paulo-custodio/test.pl94
8 files changed, 258 insertions, 96 deletions
diff --git a/challenge-087/paulo-custodio/Makefile b/challenge-087/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-087/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-087/paulo-custodio/perl/ch-1.pl b/challenge-087/paulo-custodio/perl/ch-1.pl
index 3564e6be41..e89c27c141 100644
--- a/challenge-087/paulo-custodio/perl/ch-1.pl
+++ b/challenge-087/paulo-custodio/perl/ch-1.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 087
#
diff --git a/challenge-087/paulo-custodio/perl/ch-2.pl b/challenge-087/paulo-custodio/perl/ch-2.pl
index 561a2d11c7..cc76329393 100644
--- a/challenge-087/paulo-custodio/perl/ch-2.pl
+++ b/challenge-087/paulo-custodio/perl/ch-2.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 087
#
diff --git a/challenge-087/paulo-custodio/python/ch-1.py b/challenge-087/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..d740ce7f49
--- /dev/null
+++ b/challenge-087/paulo-custodio/python/ch-1.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+
+# Challenge 087
+#
+# TASK #1 > Longest Consecutive Sequence
+# Submitted by: Mohammad S Anwar
+# You are given an unsorted array of integers @N.
+#
+# Write a script to find the longest consecutive sequence. Print 0 if none sequence found.
+#
+# Example 1:
+# Input: @N = (100, 4, 50, 3, 2)
+# Output: (2, 3, 4)
+# Example 2:
+# Input: @N = (20, 30, 10, 40, 50)
+# Output: 0
+# Example 3:
+# Input: @N = (20, 19, 9, 11, 10)
+# Output: (9, 10, 11)
+
+import sys
+
+def longest_seq(n):
+ seq = []
+
+ # sort the sequence
+ n.sort()
+
+ # look for sequences of consecutive numbers
+ while len(n) > 0:
+ i = 0
+ while i < len(n) and n[i]==n[0]+i:
+ i += 1
+ got = n[:i]
+ n = n[i:]
+ if len(got) > 1 and len(got) > len(seq):
+ seq = got
+
+ return seq
+
+seq = longest_seq([int(x) for x in sys.argv[1:]])
+if seq:
+ print("("+ ", ".join([str(x) for x in seq]) +")")
+else:
+ print(0)
diff --git a/challenge-087/paulo-custodio/python/ch-2.py b/challenge-087/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..c80842bebd
--- /dev/null
+++ b/challenge-087/paulo-custodio/python/ch-2.py
@@ -0,0 +1,122 @@
+#!/usr/bin/env python3
+
+# Challenge 087
+#
+# TASK #2 > Largest Rectangle
+# Submitted by: Mohammad S Anwar
+# You are given matrix m x n with 0 and 1.
+#
+# Write a script to find the largest rectangle containing only 1. Print 0 if none found.
+#
+# Example 1:
+# Input:
+# [ 0 0 0 1 0 0 ]
+# [ 1 1 1 0 0 0 ]
+# [ 0 0 1 0 0 1 ]
+# [ 1 1 1 1 1 0 ]
+# [ 1 1 1 1 1 0 ]
+#
+# Output:
+# [ 1 1 1 1 1 ]
+# [ 1 1 1 1 1 ]
+# Example 2:
+# Input:
+# [ 1 0 1 0 1 0 ]
+# [ 0 1 0 1 0 1 ]
+# [ 1 0 1 0 1 0 ]
+# [ 0 1 0 1 0 1 ]
+#
+# Output: 0
+# Example 3:
+# Input:
+# [ 0 0 0 1 1 1 ]
+# [ 1 1 1 1 1 1 ]
+# [ 0 0 1 0 0 1 ]
+# [ 0 0 1 1 1 1 ]
+# [ 0 0 1 1 1 1 ]
+#
+# Output:
+# [ 1 1 1 1 ]
+# [ 1 1 1 1 ]
+
+import fileinput
+import re
+
+def read_input():
+ lines = []
+ for line in fileinput.input():
+ lines.append(line)
+ return lines
+
+def read_matrix(lines):
+ m = []
+ for line in lines:
+ line = re.sub(r"\D+", " ", line)
+ cols = [int(x) for x in line.split()]
+ m.append(cols)
+ return m
+
+# extract a vertical slice of an array
+def vert_slice(c, m):
+ slice = []
+ for r in range(len(m)):
+ slice.append(m[r][c])
+ return slice
+
+# check an array for 1's
+def all_ones(a):
+ for n in a:
+ if not n:
+ return False
+ return True
+
+# largest rectangle horizontally starting at (r0,c0)
+def largest_horiz(r0, c0, m):
+ # find longest row of 1's
+ c = c0+1
+ while c < len(m[0]) and m[r0][c]:
+ c += 1
+
+ # search for bottom rows of 1's of the same size
+ r = r0+1
+ while r < len(m) and all_ones(m[r][c0:c]):
+ r += 1
+
+ return r-r0, c-c0
+
+# largest rectangle vertically starting at (r0,c0)
+def largest_vert(r0, c0, m):
+ # find longest column of 1's
+ r = r0+1
+ while r < len(m) and m[r][c0]:
+ r += 1
+
+ # search for right columns of 1's of the same size
+ c = c0+1
+ while c < len(m[0]) and all_ones(vert_slice(c, m[r0:r])):
+ c += 1
+
+ return r-r0, c-c0
+
+# largest rectangle
+def largest(m):
+ h, w = 0, 0
+ for r in range(len(m)): # try each row
+ for c in range(len(m[r])): # and column
+ if m[r][c]: # have a 1
+ h1, w1 = largest_horiz(r, c, m)
+ if h1*w1 > 1 and h1*w1 > h*w:
+ h, w = h1, w1
+
+ h1, w1 = largest_vert(r, c, m)
+ if h1*w1 > 1 and h1*w1 > h*w:
+ h, w = h1, w1
+
+ return h, w
+
+h, w = largest(read_matrix(read_input()))
+if h==0:
+ print(0)
+else:
+ for r in range(h):
+ print("[ "+ "1 "*w +"]")
diff --git a/challenge-087/paulo-custodio/t/test-1.yaml b/challenge-087/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..185d97f02a
--- /dev/null
+++ b/challenge-087/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 100 4 50 3 2
+ input:
+ output: (2, 3, 4)
+- setup:
+ cleanup:
+ args: 100 101 50 3 2
+ input:
+ output: (2, 3)
+- setup:
+ cleanup:
+ args: 20 30 10 40 50
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 20 19 9 11 10
+ input:
+ output: (9, 10, 11)
diff --git a/challenge-087/paulo-custodio/t/test-2.yaml b/challenge-087/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..da81717b6c
--- /dev/null
+++ b/challenge-087/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,67 @@
+- setup:
+ cleanup:
+ args:
+ input: |
+ [ 0 0 0 1 0 0 ]
+ [ 1 1 1 0 0 0 ]
+ [ 0 0 1 0 0 1 ]
+ [ 1 1 1 1 1 0 ]
+ [ 1 1 1 1 1 0 ]
+ output: |
+ [ 1 1 1 1 1 ]
+ [ 1 1 1 1 1 ]
+- setup:
+ cleanup:
+ args:
+ input: |
+ [ 1 0 1 0 1 0 ]
+ [ 0 1 0 1 0 1 ]
+ [ 1 0 1 0 1 0 ]
+ [ 0 1 0 1 0 1 ]
+ output: |
+ 0
+- setup:
+ cleanup:
+ args:
+ input: |
+ [ 0 0 0 1 1 1 ]
+ [ 1 1 1 1 1 1 ]
+ [ 0 0 1 0 0 1 ]
+ [ 0 0 1 1 1 1 ]
+ [ 0 0 1 1 1 1 ]
+ output: |
+ [ 1 1 1 1 ]
+ [ 1 1 1 1 ]
+- setup:
+ cleanup:
+ args:
+ input: |
+ [ 1 ]
+ output: |
+ 0
+- setup:
+ cleanup:
+ args:
+ input: |
+ [ 1 1 ]
+ output: |
+ [ 1 1 ]
+- setup:
+ cleanup:
+ args:
+ input: |
+ [ 1 1 ]
+ [ 1 0 ]
+ output: |
+ [ 1 1 ]
+- setup:
+ cleanup:
+ args:
+ input: |
+ [ 1 1 ]
+ [ 1 0 ]
+ [ 1 0 ]
+ output: |
+ [ 1 ]
+ [ 1 ]
+ [ 1 ]
diff --git a/challenge-087/paulo-custodio/test.pl b/challenge-087/paulo-custodio/test.pl
deleted file mode 100644
index 6d84250030..0000000000
--- a/challenge-087/paulo-custodio/test.pl
+++ /dev/null
@@ -1,94 +0,0 @@
-#!/usr/bin/perl
-
-use Modern::Perl;
-use Test::More;
-
-my $in = "in.txt";
-
-is capture("perl perl/ch-1.pl 100 4 50 3 2"), "(2, 3, 4)\n";
-is capture("perl perl/ch-1.pl 100 101 50 3 2"), "(2, 3)\n";
-is capture("perl perl/ch-1.pl 20 30 10 40 50"), "0\n";
-is capture("perl perl/ch-1.pl 20 19 9 11 10"), "(9, 10, 11)\n";
-
-spew($in, <<END);
-[ 0 0 0 1 0 0 ]
-[ 1 1 1 0 0 0 ]
-[ 0 0 1 0 0 1 ]
-[ 1 1 1 1 1 0 ]
-[ 1 1 1 1 1 0 ]
-END
-is capture("perl perl/ch-2.pl <$in"), <<END;
-[ 1 1 1 1 1 ]
-[ 1 1 1 1 1 ]
-END
-
-spew($in, <<END);
-[ 1 0 1 0 1 0 ]
-[ 0 1 0 1 0 1 ]
-[ 1 0 1 0 1 0 ]
-[ 0 1 0 1 0 1 ]
-END
-is capture("perl perl/ch-2.pl <$in"), <<END;
-0
-END
-
-spew($in, <<END);
-[ 0 0 0 1 1 1 ]
-[ 1 1 1 1 1 1 ]
-[ 0 0 1 0 0 1 ]
-[ 0 0 1 1 1 1 ]
-[ 0 0 1 1 1 1 ]
-END
-is capture("perl perl/ch-2.pl <$in"), <<END;
-[ 1 1 1 1 ]
-[ 1 1 1 1 ]
-END
-
-spew($in, <<END);
-[ 1 ]
-END
-is capture("perl perl/ch-2.pl <$in"), <<END;
-0
-END
-
-spew($in, <<END);
-[ 1 1 ]
-END
-is capture("perl perl/ch-2.pl <$in"), <<END;
-[ 1 1 ]
-END
-
-spew($in, <<END);
-[ 1 1 ]
-[ 1 0 ]
-END
-is capture("perl perl/ch-2.pl <$in"), <<END;
-[ 1 1 ]
-END
-
-spew($in, <<END);
-[ 1 1 ]
-[ 1 0 ]
-[ 1 0 ]
-END
-is capture("perl perl/ch-2.pl <$in"), <<END;
-[ 1 ]
-[ 1 ]
-[ 1 ]
-END
-
-unlink($in);
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}
-
-sub spew {
- my($file, $text) = @_;
- open(my $fh, ">", $file) or die "write $file: $!\n";
- print $fh $text;
-}