aboutsummaryrefslogtreecommitdiff
path: root/challenge-075
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-07-09 02:37:34 +0200
committerAbigail <abigail@abigail.be>2021-07-09 02:37:34 +0200
commit92ddbd92ad44b31d5dee58159aac665bd7efdd42 (patch)
treed903b5b614ec9437e26f2262b5ee3242562b32cc /challenge-075
parent5bbef30153986666ed768b86438e838099858746 (diff)
downloadperlweeklychallenge-club-92ddbd92ad44b31d5dee58159aac665bd7efdd42.tar.gz
perlweeklychallenge-club-92ddbd92ad44b31d5dee58159aac665bd7efdd42.tar.bz2
perlweeklychallenge-club-92ddbd92ad44b31d5dee58159aac665bd7efdd42.zip
Ruby solutions for week 75
Diffstat (limited to 'challenge-075')
-rw-r--r--challenge-075/abigail/README.md2
-rw-r--r--challenge-075/abigail/ruby/ch-1.rb33
-rw-r--r--challenge-075/abigail/ruby/ch-2.rb45
3 files changed, 80 insertions, 0 deletions
diff --git a/challenge-075/abigail/README.md b/challenge-075/abigail/README.md
index 5c1b57db62..0e2ef80c9d 100644
--- a/challenge-075/abigail/README.md
+++ b/challenge-075/abigail/README.md
@@ -32,6 +32,7 @@ f) (2, 4)
* [Node.js](node/ch-1.js)
* [Perl](perl/ch-1.pl)
* [Python](python/ch-1.py)
+* [Ruby](ruby/ch-1.rb)
## [Largest Rectangle Histogram](https://theweeklychallenge.org/blog/perl-weekly-challenge-075/#task-2--largest-rectangle-histogram)
@@ -85,3 +86,4 @@ is formed by columns `(5, 7 and 5)`.
* [Node.js](node/ch-2.js)
* [Perl](perl/ch-2.pl)
* [Python](python/ch-2.py)
+* [Ruby](ruby/ch-1.rb)
diff --git a/challenge-075/abigail/ruby/ch-1.rb b/challenge-075/abigail/ruby/ch-1.rb
new file mode 100644
index 0000000000..be67045a1a
--- /dev/null
+++ b/challenge-075/abigail/ruby/ch-1.rb
@@ -0,0 +1,33 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-1.rb < input-file
+#
+
+def possibilities (target, coins)
+ return 1 if target == 0
+ return 0 if target < 0 || coins . length == 0
+
+ sum = 0
+ for i in 0 .. (target / coins [0]) . to_i do
+ sum += possibilities(target - i * coins [0], coins [1 .. -1])
+ end
+
+ return sum
+end
+
+ARGF . each_line do
+ |line|
+ coins = line . split . map do
+ |c|
+ c . to_i
+ end
+
+ target = coins . shift
+
+ puts (possibilities(target, coins))
+end
diff --git a/challenge-075/abigail/ruby/ch-2.rb b/challenge-075/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..757184d399
--- /dev/null
+++ b/challenge-075/abigail/ruby/ch-2.rb
@@ -0,0 +1,45 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-2.rb < input-file
+#
+
+ARGF . each_line do
+ |line|
+ heights = line . split . map do
+ |h|
+ h . to_i
+ end
+
+ max_height = heights . max
+
+ max_area = 0
+ for h in 1 .. max_height do
+ max = 0
+ cur = 0
+ heights . map do
+ |height|
+ if height >= h
+ cur += 1
+ else
+ if max < cur
+ max = cur
+ end
+ cur = 0
+ end
+ end
+ if max < cur
+ max = cur
+ end
+
+ area = max * h
+ if max_area < area
+ max_area = area
+ end
+ end
+ puts (max_area)
+end