From 5ab72f71a1b4686f5acf6ae5562f2829b0ee9c6e Mon Sep 17 00:00:00 2001 From: Abigail Date: Fri, 19 Feb 2021 01:47:09 +0100 Subject: Ruby solution for week 100, part 1 --- challenge-100/abigail/README.md | 1 + challenge-100/abigail/ruby/ch-1.rb | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 challenge-100/abigail/ruby/ch-1.rb diff --git a/challenge-100/abigail/README.md b/challenge-100/abigail/README.md index 7afd7bb3f2..e87dfae955 100644 --- a/challenge-100/abigail/README.md +++ b/challenge-100/abigail/README.md @@ -26,6 +26,7 @@ Output: 07:15 pm or 07:15pm * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) ### Blog diff --git a/challenge-100/abigail/ruby/ch-1.rb b/challenge-100/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..012c2e700f --- /dev/null +++ b/challenge-100/abigail/ruby/ch-1.rb @@ -0,0 +1,43 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do |_| + # + # Parse data. Note that we have to map the hours and + # minutes from strings to integers. + # + m = _ . match /(?[0-9]+):(?[0-9]+)\s*(?[ap]?)/ + hour = m [:hour] . to_i + minute = m [:minute] . to_i + ampm = m [:ampm] + + # + # Calculate the new AM/PM marker + # + new_ampm = ampm == "" ? hour >= 12 ? "pm" : "am" : "" + + # + # Calculate the new hour + # + hour %= 12 + + if ampm == "" && hour == 0 + then hour = 12 + end + + if ampm == "p" + then hour += 12 + end + + # + # Print the result + # + puts sprintf("%02d:%02d%s", hour, minute, new_ampm) +end -- cgit