There, I fixed it (with tape)

41

4

Challenge:

Given a string only containing upper- and/or lowercase letters (whichever you prefer), put tape horizontally to fix it. We do this by checking the difference of two adjacent letters in the alphabet (ignoring wrap-around and only going forward), and filling the space with as much TAPE/tape as we would need.


Example:

Input: abcmnnnopstzra
Output: abcTAPETAPETmnnnopTAstTAPETzra

Why?

  • Between c and m should be defghijkl (length 9), so we fill this with TAPETAPET;
  • Between p and s should be qr (length 2), so we fill this with TA;
  • Between t and z should be uvwxy (length 5), so we fill this with TAPET.

Challenge rules:

  • The difference only applies forward, so no tape between zra.
  • It is possible to have multiple of the same adjacent letters like nnn.
  • You are allowed to take the input in any reasonable format. Can be a single string, string-array/list, character-array/list, 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 TAPE.
  • It is possible no TAPE is necessary, in which case the input remains unchanged.

General rules:

  • This is , so the 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 to a test for your code.
  • Also, please add an explanation if necessary.

Test cases:

Input:  "abcmnnnopstzra"
Output: "abcTAPETAPETmnnnopTAstTAPETzra"

Input:  "aza"
Output: "aTAPETAPETAPETAPETAPETAPEza"

Input:  "ghijk"
Output: "ghijk"

Input:  "aabbddeeffiiacek"
Output: "aabbTddeeffTAiiaTcTeTAPETk"

Input:  "zyxxccba"
Output: "zyxxccba"

Input:  "abccxxyz"
Output: "abccTAPETAPETAPETAPETAPExxyz"

Input:  "abtapegh"
Output: "abTAPETAPETAPETAPETtaTAPETAPETAPETApeTgh"

Input:  "tape"
Output: "taTAPETAPETAPETApe"

Kevin Cruijssen

Posted 2018-03-05T08:44:37.910

Reputation: 67 575

10Unsure why we would discard some between fix-ups (e.g. we discard APE after fixing with TAPETAPET and before fixing with TA) seems like a waste of good TAPE to me, but maybe that's just how I roll (sorry). – Jonathan Allan – 2018-03-05T15:37:39.807

@JonathanAllan Hehe, you're indeed right that it's kinda a waste of 'tape'. Hmm, could be something I might use in part 2 of the challenge. ;)

– Kevin Cruijssen – 2018-03-05T15:48:22.770

What about if the string comes in with tape - e.g., abTAPEgh ? – manassehkatz-Moving 2 Codidact – 2018-03-05T15:50:10.393

@manassehkatz It would be interpret as every other character, so ab[TAPETAPETAPETAPET]TA[TAPETAPETAPETA]PE[T]gh (added the [] to make it more readable). – Kevin Cruijssen – 2018-03-05T15:57:20.600

1@KevinCruijssen Which is consistent, though (as with the "wasting tape" question) not 100% logical. So maybe one more test case: Input TAPE, output TATAPETAPETAPETAPE (I think I got that right...) – manassehkatz-Moving 2 Codidact – 2018-03-05T16:05:31.980

Can i take each character as a separate argument? – Asone Tuhid – 2018-03-05T16:54:01.173

@AsoneTuhid Sure, why not. :) – Kevin Cruijssen – 2018-03-06T07:51:14.223

Answers

8

05AB1E, 14 12 bytes

'¡ÉIÇ¥<∍‚ζJJ

Try it online!

Explanation

'¡É            # push the string "tape"
   I           # push input
    Ç          # convert to a list of character codes
     ¥         # calculate deltas
      <        # decrement
       ∍       # extend the string "tape" to each of these sizes
               # results in an empty string for sizes smaller than zero
        ‚ζ     # zip with input (results in a list of pairs)
          JJ   # join to a list of strings and then to a string

Emigna

Posted 2018-03-05T08:44:37.910

Reputation: 50 798

4Would you mind adding an explanation? I think it's fairly similar to the Jelly answer, but I'm curious which of the characters are used for which of the operations to get the result. One of the general rules in my challenges: "Also, please add an explanation if necessary.", with languages like Jelly, 05AB1E, Charcoal, APL, etc. it's save to assume most people can't read it and an explanation is mandatory. :) – Kevin Cruijssen – 2018-03-05T14:14:24.817

@KevinCruijssen: Certainly. I usually add an explanation to my 05AB1E answers, but I don't always have time right when I post it. – Emigna – 2018-03-05T14:31:06.563

1@KevinCruijssen in the arguments of any 05AB1E TIO link you can add -d to get the raw stack operation-by-operation dump of what is going on in lieu of an explanation, but I try to post them too it isn't straightforward especially on some of mine LOL. – Magic Octopus Urn – 2018-03-05T20:34:29.700

10

Jelly, 13 bytes

OI’“¡ʂƁ»ṁ$€ż@

Try it online!

Explanation

OI’“¡ʂƁ»ṁ$€ż@ – Full program. Take a string as a command line argument.
O             – Ordinal. Get the ASCII values of each character.
 I’           – Get the increments (deltas), and subtract 1 from each.
          €   – For each difference I...
   “¡ʂƁ»ṁ$    – Mold the compressed string "tape" according to these values.
                Basically extends / shortens "tape" to the necessary length.
           ż@ – Interleave with the input.

Mr. Xcoder

Posted 2018-03-05T08:44:37.910

Reputation: 39 774

Here's a trick to get you down to 12 – Jonathan Allan – 2018-03-05T13:46:47.697

@JonathanAllan That seems invalid to me. It outputs abctapetapetmnnnopapstetapezra instead of abctapetapetmnnnoptasttapetzra. – Mr. Xcoder – 2018-03-05T13:51:34.397

7Oh, yes it's invalid - I had not realised we were to waste tape from the roll! – Jonathan Allan – 2018-03-05T13:54:24.967

7

Haskell, 58 bytes

f(x:y:r)=x:take(length[x..y]-2)(cycle"TAPE")++f(y:r)
f s=s

Try it online! The function f recurses over the string and looks at consecutive characters x and y. cycle"TAPE" yields the infinite string "TAPETAPETAPE...". [x..y] gets the range of characters from x to y inclusive, so we need to subtract two from the length. In case x occurs later in the alphabet then y or both are the same character, we get a negative number after subtracting, but luckily take accepts those as well and just takes nothing.

Laikoni

Posted 2018-03-05T08:44:37.910

Reputation: 23 676

6

Perl 5, -F 46 bytes

#/usr/bin/perl -F
use 5.10.0;
say map{((P,E,T,A)x7)[2..-$^H+($^H=ord)],$_}@F

Try it online!

Ton Hospel

Posted 2018-03-05T08:44:37.910

Reputation: 14 114

2Was about to ask why P,E,T,A instead of T,A,P,E, but I now notice you've used ((P,E,T,A)x7)[2..-$^H+($^H=ord) instead of ((T,A,P,E)x7)[0..-$^H+($^H=ord)-2 to save two bytes. Nice answer! – Kevin Cruijssen – 2018-03-05T11:53:12.910

Very nice indeed! Much better than my naiive approach! You can save 2 bytes using literal ^H (\x08) though! – Dom Hastings – 2018-03-05T14:17:06.810

@DomHastings Literal control character variables have been disabled for quite many perl versions already. I could claim a score on an older perl version (Like I did recently for do$0) but it's only 2 bytes here, so I didn't bother – Ton Hospel – 2018-03-05T14:36:23.143

Ah, of course! It's because macOS has 5.18.2 by default, so it's the version I'm most familiar with! – Dom Hastings – 2018-03-05T16:52:46.247

5

Python 2, 96 87 80 bytes

lambda s:''.join(c+(C>c)*('TAPE'*6)[:ord(C)+~ord(c)]for c,C in zip(s,s[1:]+' '))

Try it online!

TFeld

Posted 2018-03-05T08:44:37.910

Reputation: 19 246

5

Haskell, 64 bytes

t="TAPE"++t
e=fromEnum
f(x:y:z)=x:take(e y-e x-1)t++f(y:z)
f x=x

Handles strings of either uppercase or lowercase letters, but not both.

Try it online!

Cristian Lupascu

Posted 2018-03-05T08:44:37.910

Reputation: 8 369

4

Python 3, 98 bytes

lambda a:"".join(sum(zip(a,[("TAPE"*9)[:y>x and~ord(x)+ord(y)]for x,y in zip(a,a[1:])]),()))+a[-1]

Try it online!

-1 byte thanks to Asone Tuhid

HyperNeutrino

Posted 2018-03-05T08:44:37.910

Reputation: 26 575

@AsoneTuhid Thanks. I'd recommend posting your own answer because you've golfed off quite a bit and you also golfed TFeld's answer to the same thing so editing would cause dupes (which isn't disallowed) – HyperNeutrino – 2018-03-05T16:58:51.657

Alright, have a -1 byte

– Asone Tuhid – 2018-03-05T17:21:43.713

@AsoneTuhid oh okay, thanks – HyperNeutrino – 2018-03-05T18:41:09.880

4

C, 84 bytes

i,l;f(char*s){for(;*s;)for(putchar(l=*s++),l=s[i=0]+~l;i<l;)putchar("TAPE"[i++%4]);}

Try it online!

C (run on Windows Command Prompt), 81 bytes

i,l;f(char*s){for(;putchar(l=*s++);)for(l=s[i=0]+~l;i<l;)putchar("TAPE"[i++%4]);}

Output:

Steadybox

Posted 2018-03-05T08:44:37.910

Reputation: 15 798

4

Scala, 66 bytes

(s:String)=>s./:("z"){(o,c)=>o+("TAPE"*6).take(c-o.last-1)+c}.tail

Try it online!

Explanation

/: foldLeft over the string
("z") starting with a non-empty string to we don't have to handle the first iteration in a special way
"TAPE"*6 generate a long enough string of TAPETAPETA...
.take(c-o.last-1) take the difference between this character and the previous (now the last char in the output so far) characters from the TAPETAPETA... string. o.last will always be safe because we start with a non-empty string.
o+...+c append it to the output so far ... and add this character to the end
.tail get rid of the leading z we added

Jonny Graham

Posted 2018-03-05T08:44:37.910

Reputation: 41

Welcome to PPCG, and nice first answer! +1 from me. – Kevin Cruijssen – 2018-03-06T11:50:42.533

4

PHP, 85 bytes

$s=str_split($argv[1]);foreach($s as$l)echo str_pad($l,ord(next($s))-ord($l),'TAPE');

Try it online!

Explanation

$s = str_split($argv[1]);   // convert the parameter string to an array
foreach($s as $l)           // loop the array
echo str_pad(               // print
  $l,                       // the letter
  ord(next($s)) - ord($l),  // calculate the distance to the next letter using ASCII values
  'TAPE'                    // padding string
);                          // profit!

M4tini

Posted 2018-03-05T08:44:37.910

Reputation: 141

2Welcome to the site! :) – James – 2018-03-07T23:29:55.100

3

Javascript, 131 127 Bytes

4 Bytes saved thanks to Rick Hitchcock.

z=(a=>[...a].reduce((x,y)=>x+[...Array((f=y[c='charCodeAt']()-x.slice(-1)[c]())>1?f-1:0)].reduce((e,r,t)=>e+"TAPE"[t%4],"")+y))

Unrolled

z=a=>[...a].reduce(
  (x,y)=>
    x + [...Array(
      (f = y.charCodeAt()-(x.slice(-1).charCodeAt()) ) > 1 ? (f-1) : 0
    )].reduce(
      (e,r,t)=> 
        e + "TAPE"[t%4],"") + y
);

My Problem here is that Javascript had no clean way to get the distance between character a and b.

<script>
  z=(a=>[...a].reduce((x,y)=>x+[...Array((f=y[c='charCodeAt']()-x.slice(-1)[c]())>1?f-1:0)].reduce((e,r,t)=>e+"TAPE"[t%4],"")+y))
</script>

<main>
  <input id="input-box" type="text">
  <pre id=output>output</pre>
</main>

<script>
  inputBox = document.getElementById("input-box");
  inputBox.addEventListener("keyup", function(e){
    output.innerText = z(inputBox.value);
  });
</script>

Jhal

Posted 2018-03-05T08:44:37.910

Reputation: 191

1Nice. You can save a byte by removing the trailing semi-colon, and save a few more bytes by assigning charCodeAt to a variable: z=(a=>[...a].reduce((x,y)=>x+[...Array((f=y[c='charCodeAt']()-x.slice(-1)[c]())>1?f-1:0)].reduce((e,r,t)=>e+"TAPE"[t%4],"")+y)) – Rick Hitchcock – 2018-03-05T22:26:43.083

Thanks! I'm absolutely disgusted by how that works, but it's good to know that it does for the future. – Jhal – 2018-03-06T08:57:07.270

3

Python 2 / 3, 70 69 bytes

f=lambda s,*t:t and s+('TAPE'*6)[:max(ord(t[0])+~ord(s),0)]+f(*t)or s

Try it online!

Asone Tuhid

Posted 2018-03-05T08:44:37.910

Reputation: 1 944

2

Charcoal, 20 bytes

⭆θ⁺…TAPE∧κ⊖⁻℅ι℅§θ⊖κι

Try it online! Explanation:

 θ              θ       Input string
⭆                       Map over characters
                  κ     Current index
                 ⊖      Decremented
               §        Index into string
             ι          Current character
            ℅ ℅         Ordinal
           ⁻            Subtract
          ⊖             Decremented
         κ              Current index
        ∧               Logical and
    TAPE                Literal string
   …                    Mold to length
                   ι    Current character
  ⁺                     Concatenate
                        Implicitly print

Neil

Posted 2018-03-05T08:44:37.910

Reputation: 95 035

2

Java (JDK), 91 bytes

s->{var p='z';for(var c:s)System.out.print("ETAP".repeat(9).substring(1,c>p?c-p:1)+(p=c));}

Try it online!

Explanation

s->{                       // char[]-accepting lambda consumer, printing a String
 var p='z';                //  store the previous character
 for(var c:s){             //  for each character of the string
  System.out.print(        //   print...
   "ETAP".repeat(9)        //    "ETAP" repeated 9 times (to go above 26 chars)
    .substring(1,          //     of which, we substring c-p -1 characters
     c>p?c-p:1             //
    )                      //
   +(p=c)                  //    and append c, while also storing the previous character
  );

Credits

  • -2 bytes thanks to R.M
  • -4 bytes thanks to ceilingcat, by upgrading to Java 10+ and switching types to var
  • -3 bytes thanks to Kevin Cruijssen, by printing the result of my (previously) alternate version instead of returning it

Olivier Grégoire

Posted 2018-03-05T08:44:37.910

Reputation: 10 647

int p=123 would save one character. It doesn't matter what p is in the first iteration, as long as it is larger than or equal to the first character. The largest value the first character can have is 'z' == ASCII 122, so 123 is good enough. Also, if you used capital letters, you could use 91 instead of 123, saving another character. – Reinis Mazeiks – 2018-03-11T17:45:41.887

@R.M Thanks, indeed that works! – Olivier Grégoire – 2018-03-11T19:26:19.807

1-3 bytes by printing directly in your alternative method. – Kevin Cruijssen – 2019-11-07T10:57:05.370

2

Ruby, 78 77 64 62 bytes

-1 byte thanks to Kevin Cruijssen

f=->s,*t{t[0]?s+('TAPE'*6)[0,[0,t[0].ord+~s.ord].max]+f[*t]:s}

Try it online!

Asone Tuhid

Posted 2018-03-05T08:44:37.910

Reputation: 1 944

You can save a byte changing ord-l[-1].ord-1 to ord+~l[-1].ord. Nice answer, though. +1 from me. – Kevin Cruijssen – 2018-03-05T15:22:41.910

2

Pip, 29 bytes

O@a{"TAPE"@<MX[0v-$-Ag]}.BMPa

Takes input as a command-line argument (lower- or uppercase, doesn't matter). Try it online!

Explanation

O@a{"TAPE"@<MX[0v-$-Ag]}.BMPa
                               a is 1st cmdline arg; v is -1 (implicit)
O                              Output without newline
 @a                            the first character of a
                          MPa  Map this function to pairs of successive characters of a:
                    Ag          Get the ASCII codes of the two characters
                  $-            Fold on subtraction (i.e. asc(first)-asc(second))
                v-              -1 minus the above (i.e. asc(second)-asc(first)-1)
              [0      ]         A list containing 0 and the above
            MX                  Max of the list
          @<                    The first ^ characters (with cyclic indexing)
    "TAPE"                      of this string
   {                   }.B      Concatenate the second character

DLosc

Posted 2018-03-05T08:44:37.910

Reputation: 21 213

2

JavaScript (ES6), 80 78 bytes

f=([s,...S],t=S[0])=>t?s.padEnd((t>s)*(parseInt(s+t,36)-370)%37,'TAPE')+f(S):s

The distance between two characters can be determined by converting their concatenation to base 36, subtracting 370, modulus 37.

For example, (parseInt('cy',36)-370)%37 == 22.

We can then use padEnd to fill in the gaps, and recursion to handle the loop.

Test Cases:

f=([s,...S],t=S[0])=>t?s.padEnd((t>s)*(parseInt(s+t,36)-370)%37,'TAPE')+f(S):s

console.log(f('abcmnnnopstzra'));
console.log(f('aza'));
console.log(f('ghijk'));
console.log(f('aabbddeeffiiacek'));
console.log(f('zyxxccba'));
console.log(f('abccxxyz'));
console.log(f('abtapegh'));
console.log(f('tape'));

Rick Hitchcock

Posted 2018-03-05T08:44:37.910

Reputation: 2 461

2

K4, 48 bytes

Solution:

{,/_[w;x],',[1_d w:&0<d:-1+-':"j"$x;0]#\:"TAPE"}

Examples:

q)k){,/_[w;x],',[1_d w:&0<d:-1+-':"j"$x;0]#\:"TAPE"}"abcmnnnopstzra"
"abcTAPETAPETmnnnopTAstTAPETzra"
q)k){,/_[w;x],',[1_d w:&0<d:-1+-':"j"$x;0]#\:"TAPE"}"aza"
"aTAPETAPETAPETAPETAPETAPEza"
q)k){,/_[w;x],',[1_d w:&0<d:-1+-':"j"$x;0]#\:"TAPE"}"zyxxccba"
"zyxxccba"
q)k){,/_[w;x],',[1_d w:&0<d:-1+-':"j"$x;0]#\:"TAPE"}"aabbddeeffiiacek"
"aabbTddeeffTAiiaTcTeTAPETk"

Explanation:

Fairly simple solution, but a high byte count... Find the deltas, take from the string "TAPE", join to the original string cut where the deltas are > 1.

{,/_[w;x],',[1_d w:&0<d:-1+-':"j"$x;0]#\:"TAPE"} / the solution
{                                              } / lambda
                                         "TAPE"  / the string TAPE
                                      #\:        / take each-left
           ,[                      ; ]           / join (,)
                                   0             / zero (ie append zero)           
                              "j"$x              / cast input to int
                           -':                   / deltas
                        -1+                      / subtract 1
                      d:                         / assign to d
                    0<                           / delta greater than 0?
                   &                             / indices where true
                 w:                              / assign to w
               d                                 / index into deltas at w
             1_                                  / drop first
         ,'                                      / join each-both
   _[w;x]                                        / cut input x at indices w
 ,/                                              / flatten

streetster

Posted 2018-03-05T08:44:37.910

Reputation: 3 635

2

Excel VBA, 106 bytes

An anonymous VBE immediate window function that takes input as an uppercase string via cell A1 and outputs to the VBE immediate window.

a=90:For i=1To[Len(A1)]:c=Mid([A1],i,1):b=Asc(c):For j=2To b-a:?Mid("peta",j Mod 4+1,1);:Next:?c;:a=b:Next

Taylor Scott

Posted 2018-03-05T08:44:37.910

Reputation: 6 709

2

Ruby, 59 53 bytes

->s{s.reduce{|x,y|x+y.rjust(y.ord-x[-1].ord,"TAPE")}}

Try it online!

This is actually pretty straightforward - we take input as split our string into an array of chars (thanks to Asone Tuhid for pointing this out) and apply reduce operation, where we justify each char to the required length using "TAPE" as filler string.

Kirill L.

Posted 2018-03-05T08:44:37.910

Reputation: 6 693

The question says you can take input as an array of characters. (53 bytes)

– Asone Tuhid – 2018-03-07T16:08:13.103

2

K (oK), 33 bytes

{,/((0|-1+0,1_-':x)#\:"TAPE"),'x}

Try it online!

{ } anonymous function with argument x

-':x subtract each prior (use an imaginary 0 before the first item)

1_ drop first item

0, prepend a 0

-1+ add -1

0| max(0, ...)

(...)#\:"TAPE" reshape the string "TAPE" to each item from the list on the left

(...),'x append the corresponding character from x to each reshaped string

,/ concatenate all

ngn

Posted 2018-03-05T08:44:37.910

Reputation: 11 449

1

C# (.NET Core), 122 111 bytes

Saved 11 bytes thanks to @KevinCruijssen

s=>{var r=""+s[0];for(int i=1,e,d;i<s.Length;r+=s[i++])for(e=d=s[i]-s[i-1];d-->1;)r+="ETAP"[(e-d)%4];return r;}

Try it online!

Explanation:

s => 
{
    var r = "" + s[0];                  //Declare string for the result and initialize with the first character from the input.
    for (                               //Loop over the input,
        int i = 1, e, d;                //starting with the second character, also declare helper variables.
        i < s.Length;                   //Loop until the end of the input is reached.
        r += s[i++])                    //Add the current character to the result and increase the counter.
        for (                           //Loop for adding the TAPE.
            e = d = s[i] - s[i - 1];    //Calculate the differnce between the current and the previous character.
            d-- > 1;)                   //Loop until the difference is 1.
            r += "ETAP"[(e - d) % 4];   //Add a character from the TAPE to the result.
    return r;                           //Return the result.
}

raznagul

Posted 2018-03-05T08:44:37.910

Reputation: 424

1Nice answer, +1 from me. You can save 4 bytes by changing the while to a for and removing the brackets: for(int i=1,e,d;i<s.Length;r+=s[i++])for(e=d=s[i]-s[i-1];d-->1;r+=t[(e-d)%4]);. :) Oh, and since you're using t="ETAP" only once, you can use it directly, and change string to var to save 7 more bytes: s=>{var r=""+s[0];for(int i=1,e,d;i<s.Length;r+=s[i++])for(e=d=s[i]-s[i-1];d-->1;r+="ETAP"[(e-d)%4]);return r;}. – Kevin Cruijssen – 2018-03-06T13:21:37.380

@KevinCruijssen: Thanks, I wouldn't have thought about getting rid of the brackets by moving the stuff to the for loop. Also if feel stupid for missing that I can use "ETAP" directly. – raznagul – 2018-03-06T13:29:44.663

Your answer is still great, so don't worry about it. :) I get golf-tips almost every time I post an answer myself.. And it's also easier to golf an existing answer further than having it completely golfed right from the start. PS: you might have already seen them, but Tips for code-golfing in C# and Tips for golfing in <all languages> might be interesting to read through if you haven't yet.

– Kevin Cruijssen – 2018-03-06T13:39:16.813

1

Python 3, 90 bytes

p,o=' ',''
for t in input():y=(ord(t)-ord(p)-1)*(p!=' ');o+=('TAPE'*y)[0:y]+t;p=t
print(o)

Try it Online

Dat

Posted 2018-03-05T08:44:37.910

Reputation: 879

Hi, I've fixed your title from **title** to #title. Also, would you mind adding a TryItOnline-link with test code?

– Kevin Cruijssen – 2018-03-07T09:03:24.260

1

Yabasic, 119 bytes

An anonymous function that takes input as an uppercase string and outputs to STDOUT.

Input""s$
a=90
For i=1To Len(s$)
c$=Mid$(s$,i,1)
b=Asc(c$)
For j=2To b-a
?Mid$("peta",Mod(j,4)+1,1);
Next
?c$;
a=b
Next

Try it online!

Taylor Scott

Posted 2018-03-05T08:44:37.910

Reputation: 6 709

1

Clojure, 139 119 bytes

#(reduce-kv(fn[r x c](let[a(cycle "TAPE")i int d(-(i(nth(cycle %)(inc x)))(i c))](str r(if(> d 1)(apply str c(take(dec d)a))c))))""(vec %))

Anonymous function which take the string and returns the taped one. As always, Clojure doesn't seem to perform all too well. What I couldn't really work out is fetching the next char in a short way. On the last char I would get an OutOfBoundsException, reason obvious. So I put a cycle around it. Maybe there is a more elegant solution.

Ungolfed

#(reduce-kv
  (fn [r x c]
    (let [a (cycle "TAPE")
          i int
          d (-
             (i (nth (cycle %) (inc x)))
             (i c))]
      (str r
           (if (> d 1)
             (apply str c (take (dec d) a))
             c))))
  ""
  (vec %))

Update

Managed to shafe off a few bytes. Got rid of the pesky if statement by decrementing the difference. take produces an empty list if the number is 0 or less which in turn results in an empty string.

#(reduce-kv(fn[r x c](let[d(-(int(nth(cycle %)(inc x)))(int c)1)](str r c(apply str(take d(cycle "TAPE"))))))""(vec %))

Ungolfed

#(reduce-kv
  (fn [r x c]
    (let [d (-
             (int (nth (cycle %) (inc x)))
             (int c)
             1)]
      (str
       r
       c
       (apply
        str
        (take
         d
         (cycle "TAPE"))))))
  ""
  (vec %))

Joshua

Posted 2018-03-05T08:44:37.910

Reputation: 231

1

APL (Dyalog Classic), 30 bytes

{∊⍵,¨⍨⍴∘'TAPE'¨0,0⌈-1+2-/⎕a⍳⍵}

Try it online!

{ } anonymous function with argument

⎕a⍳⍵ find indices of its chars in the alphabet

2-/ pairwise differences (prev minus next)

1+ add 1

- negate

0⌈ max(0, ...)

0, prepend a 0

⍴∘'TAPE'¨ reshape cyclically the string 'TAPE' to each

⍵,¨⍨ append each char from the argument to the corresponding reshaped string

flatten

ngn

Posted 2018-03-05T08:44:37.910

Reputation: 11 449

1

CJam, 27 25 bytes

q_:i2ew.{:-~0e>_"TAPE"*<}

Try it online!

Far, far from the other golfing languages, but I'm proud of this golf anyway.

Explanation

q                            Read the input
     ew                      And take windows of size
    2                          2
   i                           from the code points
  :                            of each of its characters.
        {               }    For each of these windows:
         :                     Reduce with
          -                      subtraction.
                                 Since there are only 2 elements, this just subtracts them.
             e>                Take the maximum
           ~                     of this difference's bitwise negation
            0                    and zero.
                                 This returns -n-1 if n is negative, and 0 otherwise.
                                 Call this new value m.
                      *        Repeat
                "TAPE"           the string "TAPE" m times.
               _       <       And then take the first m elements.
                             The result of this will be an array of strings which consist of
                             the string "TAPE" repeated the proper amount of times.
       .                     Zip this array with the original input.
                             Since the original input is one element longer than this array,
                             the nothing is pushed after the final character.
                             Implicitly print everything.

Esolanging Fruit

Posted 2018-03-05T08:44:37.910

Reputation: 13 542

1

Husk, 26 25 bytes

ΣSoż+C1(moo↑¢¨tȦ9¨>0←Ẋ-mc

Try it online!

Esolanging Fruit

Posted 2018-03-05T08:44:37.910

Reputation: 13 542

1

PowerShell, 72 bytes

-join($args|% t*y|%{for(;$p*(++$p-lt$_)){'TAPE'[$i++%4]}$i=0;$p=+$_;$_})

Try it online!

mazzy

Posted 2018-03-05T08:44:37.910

Reputation: 4 832

0

Java , 213 166 153 bytes

i->{String o="";for(int a=0,l=i.length;++a<=l;){char u=i[a-1],n;o+=u;if(a<l){n=i[a];o+="TAPETAPETAPETAPETAPETAPET".substring(0,n-u>0?n+~u:0);}}return o;}

try it online

    String o = "";
    for (int a = 0, l = i.length; ++a <= l; ) {              // for each character
        char u = i[a - 1];                                    //  current character
        o += u;                                               //  add current character to output string 
        if (a < l) {                                          //  if it's not the last one
            char n = i[a];                                    //  next character
            o += "TAPETAPETAPETAPETAPETAPET".substring(0, n - u > 0 ? n +~ u : 0); // fill with enough tape but only forward
        }
    }
    return o;

Please help me make it better.

Thanks to @cairdcoinheringaahing for the tip on whitespaces. Thanks to @R.M for the tip on tape string. Thanks to @KevinCruijssen for the lambda and expressions tips.

Amir M

Posted 2018-03-05T08:44:37.910

Reputation: 1

1

Welcome to the site! You can remove a lot of whitespace to golf this answer and make sure to check these tips for golfing in Java!

– caird coinheringaahing – 2018-03-07T16:12:10.773

1You don't need to create the variable t, because you're only using it once. You can just do "TAPETAPETAPETAPETAPETAPET".substring.... – Reinis Mazeiks – 2018-03-11T17:53:51.143

Welcome to PPCG! In addition to what @R.M said, you can golf a few more things: int a=1,l=i.length;a<=l;a++ can be int a=0,l=i.length;++a<=l;, char u=i[a-1];o+=u;if(a<l){char n= can be char u=i[a-1],n;o+=u;if(a<l){n=, (n-u) doesn't need the parenthesis, and n-u-1 can be n+~u. Also, your answer is currently a snippet instead of a function. To make it a lambda you'll need to add i->{ in front and } at the end. So in total: Try it online. 153 bytes

– Kevin Cruijssen – 2018-03-12T08:19:16.690

140 bytes – ceilingcat – 2019-11-07T02:23:38.153