From 5f51c70957a90c3ea098217e195c8f84dd87d389 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 19 Oct 2021 17:33:42 +0200 Subject: Ruby solutions for week 135 --- challenge-135/abigail/README.md | 2 ++ challenge-135/abigail/ruby/ch-1.rb | 24 ++++++++++++++++++++++++ challenge-135/abigail/ruby/ch-2.rb | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 challenge-135/abigail/ruby/ch-1.rb create mode 100644 challenge-135/abigail/ruby/ch-2.rb diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index a69904f5c4..ef6c3b18c8 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -9,6 +9,7 @@ * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) ## Part 2 @@ -19,3 +20,4 @@ * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) +* [Ruby](ruby/ch-2.rb) diff --git a/challenge-135/abigail/ruby/ch-1.rb b/challenge-135/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..508907eb97 --- /dev/null +++ b/challenge-135/abigail/ruby/ch-1.rb @@ -0,0 +1,24 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do + | line | + line = line . strip() . gsub(/^[-+]/, "") + ll = line . length + if line . match (/[^0-9]/) then + puts ("not an integer") + elsif ll % 2 == 0 then + puts ("even number of digits") + elsif ll < 3 then + puts ("too short") + else + puts (line [((ll - 3) / 2) . to_i .. ((ll + 2) / 2) . to_i]) + end +end diff --git a/challenge-135/abigail/ruby/ch-2.rb b/challenge-135/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..e77981d64a --- /dev/null +++ b/challenge-135/abigail/ruby/ch-2.rb @@ -0,0 +1,37 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +w = [1, 3, 1, 7, 3, 9, 1] + +ARGF . each_line do + | line | + line . strip! + if line . match (/^[0-9BCDFGHJKLMNPQRSTVWXYZ]/) then + check = 0 + line . split('') . each_with_index do + | char, i | + val = char . ord + if val <= "9" . ord then + val -= "0" . ord + else + val -= "A" . ord + val += 10 + end + check += w [i] * val + end + if check % 10 == 0 then + puts (1) + else + puts (0) + end + else + puts (0) + end +end -- cgit