aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-079/paulo-custodio/Makefile2
-rw-r--r--challenge-079/paulo-custodio/perl/ch-1.pl9
-rw-r--r--challenge-079/paulo-custodio/perl/ch-2.pl13
-rw-r--r--challenge-079/paulo-custodio/python/ch-1.py65
-rw-r--r--challenge-079/paulo-custodio/python/ch-2.py82
-rw-r--r--challenge-079/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-079/paulo-custodio/t/test-2.yaml10
-rw-r--r--challenge-079/paulo-custodio/test.pl19
8 files changed, 184 insertions, 26 deletions
diff --git a/challenge-079/paulo-custodio/Makefile b/challenge-079/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-079/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-079/paulo-custodio/perl/ch-1.pl b/challenge-079/paulo-custodio/perl/ch-1.pl
index 7e555b3480..103aa7013e 100644
--- a/challenge-079/paulo-custodio/perl/ch-1.pl
+++ b/challenge-079/paulo-custodio/perl/ch-1.pl
@@ -2,16 +2,19 @@
# Challenge 079
#
-# TASK #1 › Count Set Bits
+# TASK #1 > Count Set Bits
# Submitted by: Mohammad S Anwar
# You are given a positive number $N.
#
-# Write a script to count the total numbrer of set bits of the binary representations of all numbers from 1 to $N and return $total_count_set_bit % 1000000007.
+# Write a script to count the total numbrer of set bits of the binary
+# representations of all numbers from 1 to $N and return
+# $total_count_set_bit % 1000000007.
#
# Example 1:
# Input: $N = 4
#
-# Explanation: First find out the set bit counts of all numbers i.e. 1, 2, 3 and 4.
+# Explanation: First find out the set bit counts of all numbers i.e. 1, 2, 3
+# and 4.
#
# Decimal: 1
# Binary: 001
diff --git a/challenge-079/paulo-custodio/perl/ch-2.pl b/challenge-079/paulo-custodio/perl/ch-2.pl
index cb8d4ceee3..be994474f1 100644
--- a/challenge-079/paulo-custodio/perl/ch-2.pl
+++ b/challenge-079/paulo-custodio/perl/ch-2.pl
@@ -2,11 +2,12 @@
# Challenge 079
#
-# TASK #2 › Trapped Rain Water
+# TASK #2 > Trapped Rain Water
# Submitted by: Mohammad S Anwar
# You are given an array of positive numbers @N.
#
-# Write a script to represent it as Histogram Chart and find out how much water it can trap.
+# Write a script to represent it as Histogram Chart and find out how much water
+# it can trap.
#
# Example 1:
# Input: @N = (2, 1, 4, 1, 2, 5)
@@ -18,7 +19,9 @@
# 1 # # # # # #
# _ _ _ _ _ _ _
# 2 1 4 1 2 5
-# Looking at the above histogram, we can see, it can trap 1 unit of rain water between 1st and 3rd column. Similary it can trap 5 units of rain water betweem 3rd and last column.
+# Looking at the above histogram, we can see, it can trap 1 unit of rain water
+# between 1st and 3rd column. Similary it can trap 5 units of rain water
+# between 3rd and last column.
#
# Therefore your script should print 6.
#
@@ -32,7 +35,9 @@
# 1 # # # # # #
# _ _ _ _ _ _ _
# 3 1 3 1 1 5
-# Looking at the above histogram, we can see, it can trap 2 units of rain water between 1st and 3rd column. Also it can trap 4 units of rain water between 3rd and last column.
+# Looking at the above histogram, we can see, it can trap 2 units of rain water
+# between 1st and 3rd column. Also it can trap 4 units of rain water between
+# 3rd and last column.
#
# Therefore your script should print 6.
diff --git a/challenge-079/paulo-custodio/python/ch-1.py b/challenge-079/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..2f9ee891a0
--- /dev/null
+++ b/challenge-079/paulo-custodio/python/ch-1.py
@@ -0,0 +1,65 @@
+#!/usr/bin/python3
+
+# Challenge 079
+#
+# TASK #1 > Count Set Bits
+# Submitted by: Mohammad S Anwar
+# You are given a positive number $N.
+#
+# Write a script to count the total numbrer of set bits of the binary
+# representations of all numbers from 1 to $N and return
+# $total_count_set_bit % 1000000007.
+#
+# Example 1:
+# Input: $N = 4
+#
+# Explanation: First find out the set bit counts of all numbers i.e. 1, 2, 3
+# and 4.
+#
+# Decimal: 1
+# Binary: 001
+# Set Bit Counts: 1
+#
+# Decimal: 2
+# Binary: 010
+# Set Bit Counts: 1
+#
+# Decimal: 3
+# Binary: 011
+# Set Bit Counts: 2
+#
+# Decimal: 4
+# Binary: 100
+# Set Bit Counts: 1
+#
+# Total set bit count: 1 + 1 + 2 + 1 = 5
+#
+# Output: Your script should print `5` as `5 % 1000000007 = 5`.
+# Example 2:
+# Input: $N = 3
+#
+# Explanation: First find out the set bit counts of all numbers i.e. 1, 2 and 3.
+#
+# Decimal: 1
+# Binary: 01
+# Set Bit Count: 1
+#
+# Decimal: 2
+# Binary: 10
+# Set Bit Count: 1
+#
+# Decimal: 3
+# Binary: 11
+# Set Bit Count: 2
+#
+# Total set bit count: 1 + 1 + 2 = 4
+#
+# Output: Your script should print `4` as `4 % 1000000007 = 4`.
+
+import sys
+
+def bit_count(n):
+ bits = "{:b}".format(n)
+ return sum([int(x) for x in bits])
+
+print(sum([bit_count(x) for x in range(1, int(sys.argv[1])+1)]) % 1000000007)
diff --git a/challenge-079/paulo-custodio/python/ch-2.py b/challenge-079/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..3c6d7a4b67
--- /dev/null
+++ b/challenge-079/paulo-custodio/python/ch-2.py
@@ -0,0 +1,82 @@
+#!/usr/bin/python3
+
+# Challenge 079
+#
+# TASK #2 > Trapped Rain Water
+# Submitted by: Mohammad S Anwar
+# You are given an array of positive numbers @N.
+#
+# Write a script to represent it as Histogram Chart and find out how much water
+# it can trap.
+#
+# Example 1:
+# Input: @N = (2, 1, 4, 1, 2, 5)
+# The histogram representation of the given array is as below.
+# 5 #
+# 4 # #
+# 3 # #
+# 2 # # # #
+# 1 # # # # # #
+# _ _ _ _ _ _ _
+# 2 1 4 1 2 5
+# Looking at the above histogram, we can see, it can trap 1 unit of rain water
+# between 1st and 3rd column. Similary it can trap 5 units of rain water
+# between 3rd and last column.
+#
+# Therefore your script should print 6.
+#
+# Example 2:
+# Input: @N = (3, 1, 3, 1, 1, 5)
+# The histogram representation of the given array is as below.
+# 5 #
+# 4 #
+# 3 # # #
+# 2 # # #
+# 1 # # # # # #
+# _ _ _ _ _ _ _
+# 3 1 3 1 1 5
+# Looking at the above histogram, we can see, it can trap 2 units of rain water
+# between 1st and 3rd column. Also it can trap 4 units of rain water between
+# 3rd and last column.
+#
+# Therefore your script should print 6.
+
+import sys
+import re
+
+# draw histogram
+def draw_hist(n):
+ max_height = max(n)
+ hist = []
+ for row in range(max_height):
+ height = max_height-row
+ line = ""
+ for col in range(len(n)):
+ if n[col]>=height:
+ line += "#"
+ else:
+ line += " "
+ hist.append(line)
+ return hist
+
+# fill buckets with water
+def fill_watter(hist):
+ for i in range(len(hist)):
+ while True:
+ matches = re.search(r"#( +)#", hist[i])
+ if not matches:
+ break
+ hist[i] = hist[i][:matches.start(1)] + \
+ "w"*(matches.end(1)-matches.start(1)) + \
+ hist[i][matches.end(1):]
+ return hist
+
+# count water buckets
+def count_watter(hist):
+ count = 0
+ for row in hist:
+ row, watter = re.subn(r"w", r"w", row)
+ count += watter
+ return count
+
+print(count_watter(fill_watter(draw_hist([int(x) for x in sys.argv[1:]]))))
diff --git a/challenge-079/paulo-custodio/t/test-1.yaml b/challenge-079/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..c719dc2872
--- /dev/null
+++ b/challenge-079/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 3
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: 4
+ input:
+ output: 5
diff --git a/challenge-079/paulo-custodio/t/test-2.yaml b/challenge-079/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..3f50ced9a4
--- /dev/null
+++ b/challenge-079/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 2 1 4 1 2 5
+ input:
+ output: 6
+- setup:
+ cleanup:
+ args: 3 1 3 1 1 5
+ input:
+ output: 6
diff --git a/challenge-079/paulo-custodio/test.pl b/challenge-079/paulo-custodio/test.pl
deleted file mode 100644
index d6afa37ba2..0000000000
--- a/challenge-079/paulo-custodio/test.pl
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/perl
-
-use Modern::Perl;
-use Test::More;
-
-is capture("perl perl/ch-1.pl 3"), "4\n";
-is capture("perl perl/ch-1.pl 4"), "5\n";
-
-is capture("perl perl/ch-2.pl 2 1 4 1 2 5"), "6\n";
-is capture("perl perl/ch-2.pl 3 1 3 1 1 5"), "6\n";
-
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}