17
\$\begingroup\$

Your task is simple, just remove the odd indices and double the even indices

Example

the input is Hello, World! and we get indices

H e l l o , _ W o r  l  d  !
1 2 3 4 5 6 7 8 9 10 11 12 13

and remove the odd indices

el,Wrd

Double!

eell,,WWrrdd

and you are done

1-Indexing

Test cases

abcdef => bbddff
umbrella => mmrrllaa
looooooooong text => ooooooooooggttxx
abc => bb
xkcd => kkdd
Hello, World! => eell,,WWrrdd
D => <empty>
KK => KK
Hi => ii
odd_length! => dd__eegghh
<empty> => <empty>

The input can be list if you want.

\$\endgroup\$
5
  • \$\begingroup\$ Can we use zero-indexed? \$\endgroup\$
    – Bgil Midol
    Jan 18 at 16:44
  • \$\begingroup\$ @BgilMidol No you can't \$\endgroup\$
    – Fmbalbuena
    Jan 18 at 16:45
  • \$\begingroup\$ Can we return as the form of [(n, n), (n, n), ...]? \$\endgroup\$
    – Bgil Midol
    Jan 18 at 16:52
  • \$\begingroup\$ No, array depth must be 1 \$\endgroup\$
    – Fmbalbuena
    Jan 18 at 16:53
  • \$\begingroup\$ Can we assume input is not empty? \$\endgroup\$
    – att
    Jan 18 at 21:54

44 Answers 44

15
\$\begingroup\$

brainfuck, 8 bytes

,,[..,,]

Try it online!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ That's some elegant procedural programming. \$\endgroup\$
    – Jonah
    Jan 18 at 18:02
  • \$\begingroup\$ ,[,..,] is shorter and it works \$\endgroup\$
    – okie
    Jan 19 at 5:42
  • 2
    \$\begingroup\$ @okie That prints two trailing zero bytes for inputs of odd length \$\endgroup\$
    – ovs
    Jan 19 at 9:18
9
\$\begingroup\$

J, 8 bytes

#~0 2$~#

Try it online!

  • 0 2$~# Repeat the pattern 0 2 for the length of the string:

    Hello, World
    020202020202
    
  • #~ Use that mask to "copy" the characters: zeros get deleted, twos get doubled:

    eell,,WWrrdd
    
\$\endgroup\$
9
\$\begingroup\$

convey, 8 bytes

-1 thanks to Wheat Wizard!

v2{
0"!}

Try it online!

example run

The 0 and 2 loop around, applying themselves to the input via ! (take), thus duping the element 0 or 2 times.

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

R, 35 34 32 bytes

Edit: -1 byte thanks to Giuseppe

function(x)rep(x,`[<-`(x,0:1*2))

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Use !1:0 as the index. \$\endgroup\$
    – Giuseppe
    Jan 19 at 0:22
  • \$\begingroup\$ @Giuseppe - I tried a bit, but couldn't make the "D" test-case work... am I missing something? \$\endgroup\$ Jan 19 at 0:27
  • 3
    \$\begingroup\$ No, you're right. R has such annoying length-1 special cases. I suppose a minor improvement would be !seq(!x)%%2 then. \$\endgroup\$
    – Giuseppe
    Jan 19 at 0:49
5
\$\begingroup\$

Jelly, 4 bytes

Ḋm2Ḥ

Try It Online!

Ḋm2Ḥ    Main Link
Ḋ       Remove the first character
 m2     Return every other character
   Ḥ    Double. This turns ["a", "b"] into ["aa", "bb"], which
        is not the correct format for a Jelly string, but displays
        correctly when printed.
\$\endgroup\$
2
  • 2
    \$\begingroup\$ There really ought to be a builtin for m2 \$\endgroup\$
    – pxeger
    Jan 18 at 16:56
  • 1
    \$\begingroup\$ Almost a three byte monadic Link by abusing how sparse is implemented TIO :( \$\endgroup\$ Jan 18 at 20:52
5
\$\begingroup\$

Python, 37 bytes

lambda a:"".join(x*2for x in a[1::2])

Attempt This Online!

\$\endgroup\$
5
\$\begingroup\$

Husk, 5 bytes

Ṙ2Ċ2t

Try it online!

Explanation

Ṙ2Ċ2t
    t   drop first char
  Ċ2    take every 2-nd char
Ṙ2      repeat each char 2 times
\$\endgroup\$
4
\$\begingroup\$

Vyxal, 3 bytes

y2•

Try it Online!

Vyxal has a better built-in for this than Jelly.

y2•     Full Program
y       Uninterleave; push a[::2] and a[1::2]
 2•     Repeat each character twice
\$\endgroup\$
4
\$\begingroup\$

JavaScript (Node.js), 29 bytes

x=>x.replace(/.(.?)/g,'$1$1')

Try it online!

\$\endgroup\$
4
\$\begingroup\$

ayr, 9 8 bytes

Using Jonah's J method gets me -1 byte

]#0 2$`#

I have the advantage of #'s args being swapped compared to J, but unfortunately monadic 2-trains are not hooks like in J.
Old answer:

:,_2]/,@:

Try it!

Explained

: is partial application (K-style train)

     ,@:   Zip-self with concatenation
 _2]/      Split into groups of two, take right element
,          Flatten
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 4 bytes

ιθºS

I/O as a list of characters.

Try it online or verify all test cases.

Explanation:

ι     # Uninterleave the (implicit) input-list into two parts
      #  e.g. ["a","b","c","d","e","f"] → [["a","c","e"],["b","d","f"]]
 θ    # Only leave the last/second part
      #  → ["b","d","f"]
  º   # Mirror/double each character
      #  → ["bb","dd","ff"]
   S  # Convert the list of strings to a flattened list of characters
      #  → ["b","b","d","d","f","f"]
      # (after which it is output implicitly as result)
\$\endgroup\$
3
\$\begingroup\$

Retina 0.8.2, 10 bytes

.(.?)
$1$1

Try it online! Link includes test cases. Explanation: Port of @l4m2's JavaScript answer. In Retina 1 you can write 2*$1 for the same byte count.

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 8 bytes

⭆S×ι⊗﹪κ²

Try it online! Link is to verbose version of code. Explanation:

 S          Input as a string
⭆           Map over characters and join
   ι        Current character
  ×         Repeated by
      κ     Current index
     ﹪      Modulo
       ²    Literal integer `2`
    ⊗       Doubled
\$\endgroup\$
3
\$\begingroup\$

Factor + sequences.repeating, 19 bytes

[ <odds> 2 repeat ]

Get the odd-indexed elements then repeat them twice.

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ Interpreter or GIF? \$\endgroup\$
    – Fmbalbuena
    Jan 18 at 17:41
  • \$\begingroup\$ @Fmbalbuena I don't understand what you are asking. \$\endgroup\$
    – chunes
    Jan 18 at 17:44
  • \$\begingroup\$ Can you change the screenshot to GIF? \$\endgroup\$
    – Fmbalbuena
    Jan 18 at 17:45
  • 4
    \$\begingroup\$ @Fmbalbuena Okay, done, but why? \$\endgroup\$
    – chunes
    Jan 18 at 17:59
3
\$\begingroup\$

BQN, 9 bytesSBCS

⊢/˜≠⥊0‿2˙

Run online!

Uses Jonah's J idea.

BQN, 10 bytesSBCS

2/1⊑˘⌊‿2⊸⥊

Run online!

\$\endgroup\$
3
\$\begingroup\$

Excel, 46 bytes

=CONCAT(REPT(MID(A1,SEQUENCE(2^15,,2,2),1),2))

Input is in cell A1. Output is wherever the formula is.

Working from the inside out:

  • SEQUENCE(2^15,,2,2) creates an array of even numbers from 2 to 32,768. This is the limit for how many characters can be in a cell so it's the limit of the input.
  • MID(A1,SEQUENCE(~),1) pulls out all the even-index characters one at a time.
  • REPT(MID(~),2) doubles all those characters.
  • CONCAT(REPT(~)) combines them all into a single string.

Screenshot

\$\endgroup\$
1
  • \$\begingroup\$ =LET(a,MID(A1,2*ROW(1:16384),1),CONCAT(a&a)) for 44 or =CONCAT(REPT(MID(A1,2*ROW(1:16384),1),2)) for 41 \$\endgroup\$ Jan 20 at 22:01
2
\$\begingroup\$

Haskell, 23 bytes

f(a:b:c)=b:b:f c
f _=[]

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Red, 53 bytes

func[s][foreach c extract/index s 2 2[prin c prin c]]

Try it online!

extract/index s 2 2 says "get every other element of s starting at index 2" (keeping in mind Red is 1-indexed). Then print each of those elements twice.

\$\endgroup\$
2
\$\begingroup\$

C (gcc), 35 bytes

f(char*c){for(;*c++;c++)c[-1]=*c;}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 31 bytes \$\endgroup\$
    – Noodle9
    Jan 18 at 19:38
  • \$\begingroup\$ This answer overwrites past the end of the array if its length is odd (unless you're lucky and there are two null bytes in a row), which seems wrong. \$\endgroup\$
    – Neil
    Jan 20 at 10:30
2
\$\begingroup\$

GNU AWK, 35 bytes

BEGIN{RS=".|"}{printf NR%2?e:RT RT}

or

BEGIN{RS=".|";ORS=e}$0=NR%2?e:RT RT

Try it online!

This is possible thanks to the RT variable, only available in GNU AWK. The characters are stored one at a time in RT, and the ternary conditional operator is used to skip the odds and double the even characters.

AWK, 47 bytes

split($0,a,e){for(i in a)printf i%2?e:a[i]a[i]}

Try it online!

Splits the input character by characters into the array a, and does the skip-and-double magic through the ternary operator ?:.

\$\endgroup\$
2
\$\begingroup\$

MATLAB, 25 bytes

@(x)repelem(x(2:2:end),2)
\$\endgroup\$
2
\$\begingroup\$

Perl 5 -p, 14 bytes

s/.(.?)/$1$1/g

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Uses the -p flag, for those of you not brave enough to click on the link. \$\endgroup\$
    – Neil
    Jan 20 at 10:26
2
\$\begingroup\$

Lua,77 83 bytes

Edited to comply with code golf rules. Assumes at least a space in input because I'm not entirely sure how to make it work with nil input without throwing an error.

p,t=io.read(),{} for i=0,#p,2 do t[i],t[i+1]=string.sub(p,i,i),string.sub(p,i,i)end

Try it online!

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

APL+WIN, 15 14 bytes

-1 byte thanks to Adám

Prompts for string

2/(~2|⍳⍴m)/m←⍞

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
2
  • \$\begingroup\$ Use instead of so the user doesn't need to deal with quotes and 'a' being a scalar. Then you certainly don't need , \$\endgroup\$
    – Adám
    Jan 18 at 17:13
  • \$\begingroup\$ @Adám Thanks. Done. \$\endgroup\$
    – Graham
    Jan 18 at 17:24
1
\$\begingroup\$

APL (Dyalog Unicode), 13 bytes

{2/⍵/⍨~2|⍳≢⍵}

Try it online!

pretty self explnatory

\$\endgroup\$
1
  • \$\begingroup\$ A translation of my J answer would probably save some bytes. \$\endgroup\$
    – Jonah
    Jan 18 at 18:05
1
\$\begingroup\$

APL (Dyalog Unicode), 12 bytes

{⍵/⍨0 2⍴⍨≢⍵}

Try it online!

APL port of @jonah 's j answer

\$\endgroup\$
5
  • \$\begingroup\$ my bad ,fixed it @jonah \$\endgroup\$ Jan 18 at 18:38
  • \$\begingroup\$ -1 when written as a train: ⊢⊢⍤/⍨0 2⍴⍨≢ \$\endgroup\$ Jan 18 at 20:50
  • \$\begingroup\$ I thought you still had to include the braces in a tacid definition @kamila szewczyk \$\endgroup\$ Jan 19 at 2:50
  • \$\begingroup\$ @JayantChoudhary no, as it can be assigned directly to a var and treated as a function without the braces, which due to how this site handles golfing means you don't need them. \$\endgroup\$ Jan 19 at 13:42
  • \$\begingroup\$ @JayantChoudhary tacit definitions don't require braces, only parens. \$\endgroup\$ Jan 19 at 14:31
1
\$\begingroup\$

naz, 40 bytes

2x1v1x1f1r3x1v2e1r3x1v2e2o1f0x1x2f0a0x1f

It's sure been a while, hasn't it? Works for any null-terminated input string.

Try it online!

Explanation (with 0x instructions removed)

2x1v                                     # Set variable 1 equal to 0
    1x1f                                 # Function 1
        1r                               # Read a byte of input
          3x1v2e                         # Goto function 2 if it equals variable 1
                1r                       # Read another byte of input
                  3x1v2e                 # Goto function 2 if it equals variable 1
                        2o1f             # Otherwise, output twice and call function 1
                              1x2f       # Function 2
                                  0a     # No-op
                                      1f # Call function 1
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to CGCC \$\endgroup\$
    – Fmbalbuena
    Jan 19 at 2:16
1
\$\begingroup\$

Python 2, 34 bytes

f=lambda s:s and s[1:2]*2+f(s[2:])

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Seems to work for Python 3 just as well. \$\endgroup\$
    – loopy walt
    Jan 18 at 20:54
1
\$\begingroup\$

Lexurgy, 32 bytes

Docs here.

Due to Lexurgy's input considering strings as "stuff surrounded by whitespace", no whitespace can appear in the input string. Otherwise, Lexurgy will consider ["Hello, world!"] as ["Hello,", "world!"]. Whitespace must be replaced with a different character (such as _).

o:
[] []$1=>* $1 $1
[]=>* /_ $

Ungolfed:

only-evens:
 [] []$1 => * $1 $1 # capture the second character and dup it
 [] => * / _ $ # remove last odd character as well
\$\endgroup\$
1
\$\begingroup\$

><>, 4 bytes

i#o:

Try it online!

Terminates with an error.

This again?

How?

i     Take an input (instruction pointer (IP) going right)
 #    IP bounces off # and starts going left
i     Take another input
   :  Duplicate it
  o   Output it
 #    IP bounces off # and starts going right
  o   Output again
   :  Duplicate (Useless)

And now, we're back to where we started, at the first character facing right.
The code will continue executing until it terminates with an error when running out of input.
\$\endgroup\$

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.