diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-26 15:48:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-26 15:48:31 +0000 |
| commit | b4d2998acaab110134b17acacedca0645cc011d4 (patch) | |
| tree | 66741a6d0ae8b19611b4b9f6be1e470de7e85af7 /challenge-002/abigail/ruby | |
| parent | 224ce57074c8183cb687c8395897eb03e0230ce3 (diff) | |
| parent | 9aeed56c942517679d2e97deea955c6f72abd70a (diff) | |
| download | perlweeklychallenge-club-b4d2998acaab110134b17acacedca0645cc011d4.tar.gz perlweeklychallenge-club-b4d2998acaab110134b17acacedca0645cc011d4.tar.bz2 perlweeklychallenge-club-b4d2998acaab110134b17acacedca0645cc011d4.zip | |
Merge pull request #3378 from Abigail/abigail/week-002
Abigail/week 002
Diffstat (limited to 'challenge-002/abigail/ruby')
| -rw-r--r-- | challenge-002/abigail/ruby/ch-1.rb | 13 | ||||
| -rw-r--r-- | challenge-002/abigail/ruby/ch-2.rb | 50 |
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-002/abigail/ruby/ch-1.rb b/challenge-002/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..0ef00293d6 --- /dev/null +++ b/challenge-002/abigail/ruby/ch-1.rb @@ -0,0 +1,13 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do |_| + puts _ . to_i +end diff --git a/challenge-002/abigail/ruby/ch-2.rb b/challenge-002/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..95efc096a7 --- /dev/null +++ b/challenge-002/abigail/ruby/ch-2.rb @@ -0,0 +1,50 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb {-f | -t} < input-file +# + +require 'optparse' + +BASE = 35 + +# +# Parse and validate options +# +params = ARGV . getopts ('ft') +do_from_base = params ["f"] ? 1 : 0 +do_to_base = params ["t"] ? 1 : 0 +if do_from_base + do_to_base != 1 + STDERR . puts "Program requires exactly one of '-f' or '-t'" + exit 1 +end + + +def to_base (number) + out = "" + while number > 0 do + rest = number % BASE + if rest < 10 + then char = rest . to_s + else char = (65 + rest - 10) . chr + end + out = char + out + number = (number / BASE) . to_i + end + return out +end + +def from_base (number) + return number . to_i (35) +end + +ARGF . each_line do |_| + if do_from_base == 1 + then puts from_base (_) + else puts to_base (_ . to_i) + end +end |
