33
\$\begingroup\$

Requirements:

  • Take a single word as input with only lowercase alphabetical characters (a-z)
  • Output a string where every other character is repeated, with the first repeated character being the second character in the string

Example:

Input: abcde

Output: abbcdde

Winning Criteria:
Standard code golf. Shortest number of bytes in program wins.

Clarification:
For languages with 1-based indexing the output must still match the above, aabccdee is not an acceptable solution.

\$\endgroup\$
13
  • \$\begingroup\$ May we start from 1 as the first character, for the benefit of languages with 1-based indexing? \$\endgroup\$
    – Shaggy
    Feb 1, 2020 at 0:08
  • \$\begingroup\$ Sure, i just called the first character 0th so my title "repeat odd index characters" makes sense. As long as you the output is repeating "Every other" character starting from the second character I the string (which I call index 1) then it's acceptable \$\endgroup\$
    – Taako
    Feb 1, 2020 at 0:10
  • 1
    \$\begingroup\$ I suggest you use standard I/O rules instead of requesting to use stdin (and never saying to use stdout, by the way). Or is there some reason to avoid it? \$\endgroup\$ Feb 1, 2020 at 20:23
  • 1
    \$\begingroup\$ Also, python answer is already a function that actually uses argument for input and return for output, so I'm assuming that you actually allow default I/O ways and are not restricting to stdin. \$\endgroup\$ Feb 1, 2020 at 20:31
  • 2
    \$\begingroup\$ @Neyt "Every other" is an English phrase that could mean all but one exception but in this case it does not. It is similar to "alternating", for example "every other day" means "either all even days and no odd days or all odd days and no even days". I don't think there is really any logic to it. If you are still confused the ESL stack exchange is probably the place to ask about this further. \$\endgroup\$
    – Wheat Wizard
    Feb 3, 2020 at 16:52

83 Answers 83

1 2
3
1
\$\begingroup\$

Z80Golf, 12 bytes

00000000: d52e 76e5 2e07 e5cd 0380 38f6            ..v.......8.

Try it online!

  push de
Z:ld l, $76
  push hl
  ld l, G
  push hl
G:call $8003
  jr c, Z+1   ; halt on EOF, else fallthrough

Explanation

Z80golf is a small Z80 computer hooked up to $8000=putchar, $8003=getchar. (putchar prints byte A; getchar reads a byte into A or sets the carry flag on EOF.) Our code is placed at $0000 and all registers start at 0, including PC. The rest of memory is filled with zeroes.

Let's run through some sample input: uvwxyz.

We push three addresses to the stack: $0000, then $0076, then G.

Then (here's label G), we call getchar(), and on EOF jump to the HALT instruction ($76) hidden in the argument to ld l, $76.

Otherwise, control flow falls through a sea of NOPs into $8000. This is hardcoded to perform putchar(A); ret, so we print u and return back to G.

The next time we get here, we print v and return to $0076, amid the NOP sea: control flow runs back to $8000, we print v again, then return to $0000, where the code starts over.

Repeating this process, the output is uvvwxxyzz → HALT.

\$\endgroup\$
1
\$\begingroup\$

GolfScript, 9 bytes

2/{1/~.}%
2/{1/~.}% #Repeat every other character
2/        #Split string into bits of size 2
  {    }% #For every of those elements, do block
  {1/  }  #Divide into elements of size 1
  {  ~ }  #Pop from their array
  {   .}  #Duplicate the last element

Probably possible to shave a byte or two by refining the block. Unfortunately ) doesn't decouple properly.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Chevron - 96 bytes

(link goes to interpreter)

>^__>^a
^a~s>>^s
^__>^n
0>>^i
^i+1>>^i
->+6?^i>^s
^a,^i~c>>^l
^n^l>^n
->-4?^i~o
^n^l>^n
->-6
>^n
\$\endgroup\$
1
\$\begingroup\$

Pyth, 9 8 7 bytes

And of course, the simple solution always prevails, and comes to me after I thought I'd done something clever :/

-1 byte because I literally put my laptop away and then remembered that s was a thing

%2ts*L3

Explanation:

%2tjk*L3  
          # Implicit Q = eval(input())
%2        # Every second character of:
  t       #   All but the first character of:
   jk     #     Joined on the empty string:
     *L3  #       Each character of Q * 3    

Previous version (9 bytes)

r9m,h=!Td

Falling behind a lot of other golfing languages unfortunately

Explanation:

           # Implicit T=10, Q=eval(input())
r9         # Run length decode
  m        # map over (implicit) Q
   ,h=!Td  # create a pair of:
    h=!T   # 1 + (T = not T) and
        d  # the character

Try it online!

\$\endgroup\$
1
\$\begingroup\$

T-SQL, 88 bytes

DECLARE @x INT=len(@)/2*2WHILE @x>0SELECT
@=stuff(@,@x,0,substring(@,@x,1)),@x-=2PRINT @

Try it online

\$\endgroup\$
1
\$\begingroup\$

Retina, 9 bytes

.(.)
$0$1

Port of @DigitalTrauma's sed's answer, so make sure to upvote him as well!

Try it online.

Explanation:

.(.)  # MATCH:
.(.)  #  Every pair of characters,
      #  where the second character is saved in capture group 1

$0$1  # And replace it with:
$0    #  The entire match, so both characters
  $1  #  Followed by the character from capture group 1
\$\endgroup\$
1
\$\begingroup\$

Java 8, 30 bytes

s->s.replaceAll(".(.)","$0$1")

Same approach as my Retina answer, which was a port of @DigitalTrauma's sed's answer.

Try it online.

\$\endgroup\$
1
\$\begingroup\$

Clojure, 91 bytes

(defn d[s](apply str(map #(str(first %)(second %)(second %))(partition-all 2 2(into[]s)))))

Ungolfed version:

(defn dup-alternate-chars[s]
  (apply str (map #(str (first %) (second %) (second %)) (partition-all 2 2 (into [] s)))))

Test harness:

(println (d "abcde"))

which prints "abbcdde".

Try it online!

\$\endgroup\$
1
\$\begingroup\$

APL (dzaima/APL), 10 9 bytes

-1 thanks to Bubbler

Anonymous tacit prefix function. Requires 0-based indexing (⎕IO←0)

⊢⌿⍨1 2⍴⍨≢

Try it online!

the length

2⍴⍨ use that to cyclically reshape [1,2]

⊢⌿⍨ use that to replicate the letters

\$\endgroup\$
2
  • \$\begingroup\$ 9 bytes. Straightforward is better, I guess. \$\endgroup\$
    – Bubbler
    Feb 25, 2020 at 3:50
  • \$\begingroup\$ @Bubbler Yeah. Thanks. \$\endgroup\$
    – Adám
    Feb 25, 2020 at 7:53
1
\$\begingroup\$

Rabbit~, 13

_[.0:.>:.:.>]

Explanation:

_ - Move to input
 [.0 - start loop with input 0, and loop until you hit 0
    :. - print
      > - move along input
       :.:. - print twice
           > - move along input
            ] - end loop if we have reached past input
\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 30 bytes

StringReplace[a_~~b_:>a<>b<>b]

Try it online!

For every non-overlapping pair of characters, appends the second character.

""<>MapAt[#<>#&,#,;;;;2]& doesn't work on an empty input.

\$\endgroup\$
1
\$\begingroup\$

Ly, 17 bytes

irs[l!s[p:o0]po]>

Try it online!

This is pretty much a direct translation of the description of the challenge. It reads in the all the input, then loops over the list printing each character once or twice.

ir                - read input as codepoints onto the stack, reverse the stack
  s               - save a codepoint to the backup cell, just has to be non-zero
   [           ]  - loop while the stack isn't empty
    l!s           - load the backup cell, flip 1/0, save it back
       [   0]p    - if/then, true for even numbered chars
        p         - pop (delete) the if/then test boolean
         :o       - duplicate top of stack and print as char
              o   - print the top of stack as a char
                > - switch stacks to avoid auto-printing 
\$\endgroup\$
1
\$\begingroup\$

TI-Basic, 39 bytes

For(I,2,1.5length(Ans),3
sub(Ans,1,I)+sub(Ans,I,1-I+length(Ans
End

Same as this answer except for the first line. Input is taken in Ans. Output is stored in Ans.

\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 11 chars

⊢(/⍨)1 2⍴⍨⍴
           ⍴   monadic: shape of the right argument (number of elements)

      1 2⍴⍨    repeat numbers 1 and 2 as many times as the right argument

⊢              the right argument

 (/⍨)          replicate each element of the left argument as many times as 
               specified by the corresponding elements of the right argument 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Thunno 2 t, 3 bytes

3Ḅz

Attempt This Online!

Port of emanresu A's Vyxal answer.

Thunno 2 J, 5 bytes

dŻɗ⁺×

Attempt This Online!

Explanation

3Ḅz  # Implicit input                      ->  "abcde"
3Ḅ   # Repeat each character three times   ->  "aaabbbcccdddeee"
  z  # Uninterleave into two parts         ->  ["aabccdee","abbcdde"]
     # Implicit output of last item        ->  "abbcdde"
dŻɗ⁺×  # Implicit input                    ->  "abcde"
d      # Cast to list of characters        ->  ["a","b","c","d","e"]
 Ż     # Push the length range             ->  ["a","b","c","d","e"]  [0,1,2,3,4]
  ɗ    # Pop and push each item mod two    ->  ["a","b","c","d","e"]  [0,1,0,1,0]
   ⁺   # Increment each value in the list  ->  ["a","b","c","d","e"]  [1,2,1,2,1]
    ×  # Multiply the two lists together   ->  ["a","bb","c","dd","e"]
       # Implicit output of joined list    ->  "abbcdde"
\$\endgroup\$
1
\$\begingroup\$

Arturo, 26 bytes

$=>[replace&{/.(.)}"$0$1"]

And non-regex:

Arturo, 41 bytes

$=>[i:0loop&=>[&if=1i%2[++<=]prints'i+1]]

Try it!

\$\endgroup\$
1
\$\begingroup\$

Grass, 47 bytes

wWWWWWwWWWWWWwwWWWWwwWWWWWwwWWWWWWwWWWWWWwwwwww

Try it online!

Exits with an error after printing out the string.

Ungolfed version:

w             (Function takes one argument)
WWWWWw        (Take one character from stdin)
WWWWWWww      (Take another character from stdin)
WWWWww        (Print first character, or error if no characters left)
WWWWWww       (Print second character, or error if no characters left)
WWWWWWw       (Print second character again)
WWWWWWwwwwww  (Call this function again)
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 88 bytes

$c={param($i)$c=0;$o;$i.toCharArray()|%{if($c%2-eq1){$o+="$_$_"}else{$o+="$_"}$c++;};$o}

Try it online!

PowerShell, 102 bytes

function b($i){$c=0;$o;$i.ToCharArray().ForEach{if($c%2-eq1){$o+="$_$_"}else{$o+="$_"}$c++;};write $o}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Factor + sequences.repeating, 19 bytes

[ 3 repeat <odds> ]

Attempt This Online!

Repeat each character three times and take every other character starting with the second one. Port of emanresu A's Vyxal answer.

\$\endgroup\$
0
\$\begingroup\$

Python, 62 bytes

lambda s:''.join(c if i%2==0 else 2*c for i,c in enumerate(s))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Fig, \$4\log_{256}(96)\approx\$ 3.292 bytes

n2@h

Try it online!

n2@h
n @  # For every
 2   # Second character
   h # Double
\$\endgroup\$
0
\$\begingroup\$

Zsh, 32 bytes

for a b (`fold -1`)printf $a$b$b

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Uiua, 8 bytes

▽+1◿2⇡⧻.

Try it!

▽+1◿2⇡⧻.
       .  # duplicate
      ⧻   # length
     ⇡    # range
   ◿2     # modulo two
 +1       # increment
▽         # keep
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.