10
Related: Rather similar (but much easier) challenge horizontally: There I fixed it (with tape)
Challenge:
Given a string only containing upper- and/or lowercase letters (whichever you prefer) and new-lines, put rope
vertically to fix it. We do this by checking the difference of two adjacent letters in the alphabet (ignoring wrap-around and only going downward), and filling the space with as much ROPE
/rope
as we would need.
NOTE: One other key difference between this challenge and the There I fixed it (with tape) challenge is that we're not wasting rope
this time like we did with the tape
(suggested by @JonathanAllan in part one of the challenge).
Example:
Input:
abc
bcd
ddd
eex
gfz
hka
imh
Output:
abc
bcd
Rdd
deE
efR
OPO
gEP
hRE
iOR
kO
PP
mE
R
O
P
E
R
O
P
E
R
O
x
P
z
a
E
R
O
P
E
R
h
Why?
- Between
b
andd
in column 1 should bec
(length 1), so we fill this withR
; - Between
e
andg
in column 1 should bef
(length 1), so we fill this withO
; - Between
f
andk
in column 2 should beghij
(length 4), so we fill this withPERO
; - Between
k
andm
in column 2 should bel
(length 1), so we fill this withP
; - Between
d
andc
in column 3 should beefghijklmnopqrstuvw
(length 19), so we fill this withEROPEROPEROPEROPERO
; - Between
x
andz
in column 3 should bey
(length 1), so we fill this withP
; - Between
a
andh
in column 3 should bebcdefg
(length 6), so we fill this withEROPER
.
Challenge rules:
- The difference only applies downwards, so no rope between
za
(column 3 in the example above). - It is possible to have multiple of the same adjacent letters like
dd
(column 3 in the example above). - You will continue using the
ROPE
one column at a time to not waste parts (suggested by @JonathanAllan in part 1 of the challenge). - You are allowed to take the input in any reasonable format. Can be a single string, string-array/list, character-matrix, etc. Output has the same flexibility.
- You are allowed to use lowercase and/or uppercase any way you'd like. This applies both to the input, output, and
ROPE
. - Trailing spaces are optional (note that the correct amount of leading spaces are mandatory so the columns are correct).
Any amount of trailing and/or leading new-lines are also optional. - You can assume that all test cases will have the same length across all rows, so
a\naa
/[[a][a,a]]
wouldn't be a valid input. - It is possible no
ROPE
is necessary, in which case the input remains unchanged.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code.
- Also, please add an explanation if necessary.
Test cases:
As string:
Input: "abc\nbcd\nddd\neex\ngfz\nhka\nimh"
Output: "abc\nbcd\nRdd\ndeE\nefR\nOPO\ngEP\nhRE\niOR\n kO\n PP\n mE\n R\n O\n P\n E\n R\n O\n P\n E\n R\n O\n x\n P\n z\n a\n E\n R\n O\n P\n E\n R\n h"
As array-matrix:
Input: [[a,b,c],[b,c,d],[d,d,d],[e,e,x],[g,f,z],[h,k,a],[i,m,h]]
Output: [[a,b,c],[b,c,d],[R,d,d],[d,e,E],[e,f,R],[O,P,O],[g,E,P],[h,R,E],[i,O,R],[ ,k,O],[ ,P,P],[ ,m,E],[ , ,R],[ , ,O],[ , ,P],[ , ,E],[ , ,R],[ , ,O],[ , ,P],[ , ,E],[ , ,R],[ , ,O],[ , ,x],[ , ,P],[ , ,z],[ , ,a],[ , ,E],[ , ,R],[ , ,O],[ , ,P],[ , ,E],[ , ,R],[ , ,h]]
As string:
Input: "a\nz\na"
Output: "a\nR\nO\nP\nE\nR\nO\nP\nE\nR\nO\nP\nE\nR\nO\nP\nE\nR\nO\nP\nE\nR\nO\nP\nE\nz\na"
As array-matrix:
Input: [[a],[z],[a]]
Output: [[a],[R],[O],[P],[E],[R],[O],[P],[E],[R],[O],[P],[E],[R],[O],[P],[E],[R],[O],[P],[E],[R],[O],[P],[E],[z],[a]]
As string:
Input: "zz\nyy\nxx\nxx\ncc\ncc\nbb\nad"
Output: "zz\nyy\nxx\nxx\ncc\ncc\nbb\naR\n d"
As array-matrix:
Input: [[z,z],[y,y],[x,x],[x,x],[c,c],[c,c],[b,b],[a,d]]
Output: [[z,z],[y,y],[x,x],[x,x],[c,c],[c,c],[b,b],[a,R],[ ,d]]
As string:
Input: "a\nb\nc\nc\nx\nx\ny\nz"
Output: "a\nb\nc\nc\nR\nO\nP\nE\nR\nO\nP\nE\nR\nO\nP\nE\nR\nO\nP\nE\nR\nO\nP\nE\nx\nx\ny\nz"
As array-matrix:
Input: [[a],[b],[c],[c],[x],[x],[y],[z]]
Output: [[a],[b],[c],[c],[R],[O],[P],[E],[R],[O],[P],[E],[R],[O],[P],[E],[R],[O],[P],[E],[R],[O],[P],[E],[x],[x],[y],[z]]
As string:
Input: "zai\nybj\nxcq\nxcu\ncxw\ncxw\nbyr\nazw"
Output: "zai\nybj\nxcR\nxcO\ncRP\ncOE\nbPR\naEO\n Rq\n OP\n PE\n ER\n Ru\n OO\n Pw\n Ew\n Rr\n OP\n PE\n ER\n RO\n Ow\n P \n E \n x \n x \n y \n z "
As array-matrix:
Input: [[z,a,i],[y,b,j],[x,c,q],[x,c,u],[c,x,w],[c,x,w],[b,y,r],[a,z,w]]
Output: [[z,a,i],[y,b,j],[x,c,R],[x,c,O],[c,R,P],[c,O,E],[b,P,R],[a,E,O],[ ,R,q],[ ,O,P],[ ,P,E],[ ,E,R],[ ,R,u],[ ,O,O],[ ,P,w],[ ,E,w],[ ,R,r],[ ,O,P],[ ,P,E],[ ,E,R],[ ,R,O],[ ,O,w],[ ,P, ],[ ,E, ],[ ,x, ],[ ,x, ],[ ,y, ],[ ,z, ]]
As string:
Input: "asdljasdjk"
Output: "asdljasdjk"
As array-matrix:
Input: [[a,s,d,l,j,a,s,d,j,k]]
Output: [[a,s,d,l,j,a,s,d,j,k]]
As string:
Input: "asdljasdjk\nlkawdasuhq\nasjdhajksd"
Output: "asdljasdjk\nRkaOdasPhR\nOPOPEajEPO\nPEPER REP\nERERO ORE\nROROh POR\nOPOP EPq\nPEPE ROd\nERER OR \nRsRO PO \nO jP EP \nl w RE \na d Os \n P \n E \n R \n O \n u \n k "
As array-matrix:
Input: [[a,s,d,l,j,a,s,d,j,k],[l,k,a,w,d,a,s,u,h,q],[a,s,j,d,h,a,j,k,s,d]]
Output: [[a,s,d,l,j,a,s,d,j,k],[R,k,a,O,d,a,s,P,h,R],[O,P,O,P,E,a,j,E,P,O],[P,E,P,E,R, , ,R,E,P],[E,R,E,R,O, , ,O,R,E],[R,O,R,O,h, , ,P,O,R],[O,P,O,P, , , ,E,P,q],[P,E,P,E, , , ,R,O,d],[E,R,E,R, , , ,O,R, ],[R,s,R,O, , , ,P,O, ],[O, ,j,P, , , ,E,P, ],[l, , ,w, , , ,R,E, ],[a, , ,d, , , ,O,s, ],[ , , , , , , ,P, , ],[ , , , , , , ,E, , ],[ , , , , , , ,R, , ],[ , , , , , , ,O, , ],[ , , , , , , ,u, , ],[ , , , , , , ,k, , ]]
As string:
Input: "re\nop\npo\ner"
Output: "re\noR\npO\neP\n E\n R\n O\n P\n E\n R\n O\n p\n o\n P\n E\n r"
As array-matrix:
Input: [[r,e],[o,p],[p,o],[e,r]]
Output: [[r,e],[o,R],[p,O],[e,P],[ ,E],[ ,R],[ ,O],[ ,P],[ ,E],[ ,R],[ ,O],[ ,p],[ ,o],[ ,P],[ ,E],[ ,r]]
8Can I fix it with
hope
instead? Hope is stronger than rope. (This is a poor attempt at humor, not a real question) – Magic Octopus Urn – 2018-03-08T14:47:31.757@KevinCruijssen did you mean this is easier of the TAPE is easier?? – Dat – 2018-03-08T15:24:15.873
3@MagicOctopusUrn Nope, rope is our only hope. – Steadybox – 2018-03-08T15:53:38.827
Can I ouput and array of columns?? – Dat – 2018-03-08T15:56:24.923
@Dat The
tape
challenge is a lot easier (imho). And yes, you can output an array of column. – Kevin Cruijssen – 2018-03-08T15:59:30.470Just to doublecheck - if we can output an array of columns, I suppose we are also allowed to take input in columnwise manner, right? – Kirill L. – 2018-03-09T09:04:25.317
@KirillL. Yep, completely fine by me. – Kevin Cruijssen – 2018-03-09T09:06:55.787