aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-03 02:51:30 +0100
committerAbigail <abigail@abigail.be>2021-03-03 02:51:30 +0100
commit26ae49523623bfe48ad8ccfa78efbea4d1378133 (patch)
treeafc769f52ca16302eddef9aeb75fbafcdd5dc64c
parent1ed243eb389da924b7a89910bec23c4faa720170 (diff)
downloadperlweeklychallenge-club-26ae49523623bfe48ad8ccfa78efbea4d1378133.tar.gz
perlweeklychallenge-club-26ae49523623bfe48ad8ccfa78efbea4d1378133.tar.bz2
perlweeklychallenge-club-26ae49523623bfe48ad8ccfa78efbea4d1378133.zip
Ruby solution for week 102, part 2
-rw-r--r--challenge-102/abigail/README.md2
-rw-r--r--challenge-102/abigail/python/ch-2.py31
-rw-r--r--challenge-102/abigail/ruby/ch-2.rb31
3 files changed, 64 insertions, 0 deletions
diff --git a/challenge-102/abigail/README.md b/challenge-102/abigail/README.md
index 89d0827e52..687594c5f5 100644
--- a/challenge-102/abigail/README.md
+++ b/challenge-102/abigail/README.md
@@ -91,3 +91,5 @@ such length-`N` string.
* [Lua](lua/ch-2.lua)
* [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-102/abigail/python/ch-2.py b/challenge-102/abigail/python/ch-2.py
new file mode 100644
index 0000000000..e8c3ee71ec
--- /dev/null
+++ b/challenge-102/abigail/python/ch-2.py
@@ -0,0 +1,31 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as python ch-2.py < input-file
+#
+
+import fileinput
+
+#
+# Working from the end of the required string backwards, we alternate
+# placing a hash, and placing a number. We place them in an array @out,
+# and at the end, print out said array in reverse order.
+#
+for index in fileinput . input ():
+ index = int (index)
+ out = []
+ hash = 0
+ while index > 0:
+ hash = not hash
+ if hash:
+ out . append ("#")
+ index = index - 1
+ else:
+ out . append (str (index + 1))
+ index = index - len (str (index + 1))
+ out . reverse ()
+ print ("" . join (out))
diff --git a/challenge-102/abigail/ruby/ch-2.rb b/challenge-102/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..9a4301117e
--- /dev/null
+++ b/challenge-102/abigail/ruby/ch-2.rb
@@ -0,0 +1,31 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-2.rb < input-file
+#
+
+#
+# Working from the end of the required string backwards, we alternate
+# placing a hash, and placing a number. We place them in an array @out,
+# and at the end, print out said array in reverse order.
+#
+
+ARGF . each_line do |_|
+ index = _ . to_i
+ out = Array . new
+ hash = false
+ while index > 0 do
+ if hash = !hash
+ out . push ("#")
+ index -= 1
+ else
+ out . push ((index + 1) . to_s)
+ index -= (index + 1) . to_s . length
+ end
+ end
+ puts (out . reverse . join)
+end