28
\$\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\$
  • \$\begingroup\$ May we start from 1 as the first character, for the benefit of languages with 1-based indexing? \$\endgroup\$ – Shaggy Feb 1 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 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\$ – val says Reinstate Monica Feb 1 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\$ – val says Reinstate Monica Feb 1 at 20:31
  • 1
    \$\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 at 16:52

69 Answers 69

1 2
3
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
| improve this answer | |
\$\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.

| improve this answer | |
\$\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!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ruby, 25 bytes

->s{s.scan(/(.(.?))/)*''}

Try it online!

Same byte count as the other answer, different approach.

| improve this answer | |
\$\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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 9 bytes. Straightforward is better, I guess. \$\endgroup\$ – Bubbler Feb 25 at 3:50
  • \$\begingroup\$ @Bubbler Yeah. Thanks. \$\endgroup\$ – Adám Feb 25 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
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Rockstar, 95 bytes

listen to S
cut S
X's0
Y's0
while S-X
let S at X be*1+Y
let Y be not Y
let X be+1

join S
say S

Try it here (Code will need to be pasted in)

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Pip, 13 bytes

{a%2?b.bb}MEa

Try it online!

| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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