aboutsummaryrefslogtreecommitdiff
path: root/challenge-141/abigail/ruby/ch-2.rb
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-12-06 14:55:30 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-12-06 14:55:30 +0000
commitcd04687f29d4ebea2fd5ec165585b21f2951f413 (patch)
tree5c17b623d6b7a8cb803a4464f19d157152143e62 /challenge-141/abigail/ruby/ch-2.rb
parentfe57a0405720a40beaeb75100e069aaa13e4c49e (diff)
parentb9773f5c38387d865a093d2ecdfd1b01b4452c34 (diff)
downloadperlweeklychallenge-club-cd04687f29d4ebea2fd5ec165585b21f2951f413.tar.gz
perlweeklychallenge-club-cd04687f29d4ebea2fd5ec165585b21f2951f413.tar.bz2
perlweeklychallenge-club-cd04687f29d4ebea2fd5ec165585b21f2951f413.zip
Merge branch 'master' into devel
Diffstat (limited to 'challenge-141/abigail/ruby/ch-2.rb')
-rw-r--r--challenge-141/abigail/ruby/ch-2.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-141/abigail/ruby/ch-2.rb b/challenge-141/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..86129c8794
--- /dev/null
+++ b/challenge-141/abigail/ruby/ch-2.rb
@@ -0,0 +1,35 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-2.rb < input-file
+#
+
+def substrings (n, m, prefix, max)
+ if n . length == 0 then
+ return prefix > -1 &&
+ prefix < max &&
+ prefix % m == 0 ? 1 : 0
+ end
+
+ fc = n[0] . to_i
+ tail = n[1 .. -1]
+ if prefix == -1 then
+ n_prefix = fc
+ else
+ n_prefix = 10 * prefix + fc
+ end
+
+ return substrings(tail, m, n_prefix, max) +
+ substrings(tail, m, prefix, max)
+end
+
+
+ARGF . each_line do
+ | line |
+ n, m = line . strip() . split
+ puts substrings(n, m . to_i, -1, n . to_i)
+end