23
\$\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\$ Commented Jan 18, 2022 at 16:44
  • \$\begingroup\$ @BgilMidol No you can't \$\endgroup\$
    – Fmbalbuena
    Commented Jan 18, 2022 at 16:45
  • \$\begingroup\$ Can we return as the form of [(n, n), (n, n), ...]? \$\endgroup\$ Commented Jan 18, 2022 at 16:52
  • \$\begingroup\$ No, array depth must be 1 \$\endgroup\$
    – Fmbalbuena
    Commented Jan 18, 2022 at 16:53
  • \$\begingroup\$ Can we assume input is not empty? \$\endgroup\$
    – att
    Commented Jan 18, 2022 at 21:54

62 Answers 62

17
\$\begingroup\$

brainfuck, 8 bytes

,,[..,,]

Try it online!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ That's some elegant procedural programming. \$\endgroup\$
    – Jonah
    Commented Jan 18, 2022 at 18:02
  • \$\begingroup\$ ,[,..,] is shorter and it works \$\endgroup\$
    – okie
    Commented Jan 19, 2022 at 5:42
  • 2
    \$\begingroup\$ @okie That prints two trailing zero bytes for inputs of odd length \$\endgroup\$
    – ovs
    Commented Jan 19, 2022 at 9:18
11
\$\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
10
\$\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\$
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\$
6
  • \$\begingroup\$ Use !1:0 as the index. \$\endgroup\$
    – Giuseppe
    Commented Jan 19, 2022 at 0:22
  • \$\begingroup\$ @Giuseppe - I tried a bit, but couldn't make the "D" test-case work... am I missing something? \$\endgroup\$ Commented Jan 19, 2022 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
    Commented Jan 19, 2022 at 0:49
  • \$\begingroup\$ I think you can switch to a newer version of R (I think it's 4.1) and get this down to 25 bytes. (I have no experience in R, and I just saw this trick here...) \$\endgroup\$
    – The Thonnu
    Commented Dec 3, 2022 at 20:32
  • \$\begingroup\$ @TheThonnu - You're right, of course, but I (and many of the other R golfers) am generally happy to ignore the function/\ choice if it's just a case of 7-bytes with no other inventiveness. Sometimes using the less-expensive \ notation favours or allows fundamentally different approaches, which makes it worthwhile, but here it would (to me) just feel a little 'cheap' to change it... \$\endgroup\$ Commented Dec 4, 2022 at 0:27
5
\$\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\$
1
  • \$\begingroup\$ Taking input as a list of characters allows this to be just yd \$\endgroup\$
    – lyxal
    Commented Dec 4, 2022 at 0:22
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
    Commented Jan 18, 2022 at 16:56
  • 1
    \$\begingroup\$ Almost a three byte monadic Link by abusing how sparse is implemented TIO :( \$\endgroup\$ Commented Jan 18, 2022 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\$
1
  • \$\begingroup\$ Alternative 5-byter: Ṙ¢Θ;2 \$\endgroup\$ Commented Oct 11, 2022 at 9:35
5
\$\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\$
5
\$\begingroup\$

Uiua, 7 bytes

▽↯△,0_2

Try it online!

▽↯△,0_2    input: a string
  ↯△,0_2    [0, 2] cycled to the length of the input:
      0_2      [0, 2]
   △,         shape of the input ([length])
  ↯            reshape [0, 2] to the shape [length]
▽           keep; delete chars at 0s and duplicate at 2s
\$\endgroup\$
1
  • \$\begingroup\$ Nice idea to reshape 0_2 \$\endgroup\$ Commented Mar 19 at 2:12
4
\$\begingroup\$

JavaScript (Node.js), 29 bytes

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

Try it online!

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

Factor + sequences.repeating, 19 bytes

[ <odds> 2 repeat ]

Get the odd-indexed elements then repeat them twice.

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ @Fmbalbuena I don't understand what you are asking. \$\endgroup\$
    – chunes
    Commented Jan 18, 2022 at 17:44
  • 5
    \$\begingroup\$ @Fmbalbuena Okay, done, but why? \$\endgroup\$
    – chunes
    Commented Jan 18, 2022 at 17:59
4
\$\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\$ Commented Jan 20, 2022 at 22:01
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\$
1
  • \$\begingroup\$ ºS could alternatively also be €D, which duplicates each item while still remaining a flattened list. \$\endgroup\$ Commented Oct 17, 2023 at 13:57
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\$

BQN, 9 bytesSBCS

⊢/˜≠⥊0‿2˙

Run online!

Uses Jonah's J idea.

BQN, 10 bytesSBCS

2/1⊑˘⌊‿2⊸⥊

Run online!

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

JavaScript (Node.js), 28 bytes

f=([,y,...z])=>y?y+y+f(z):''

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 28 \$\endgroup\$
    – l4m2
    Commented Jan 19, 2022 at 9:06
3
\$\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
    Commented Jan 20, 2022 at 10:26
3
\$\begingroup\$

K (ngn/k), 10 bytes

(2*2!!#:)#

Try it online!

  • (...)# set up a filter/replicate, where the code in (...) is run on the (implicit) input and returns a list of numbers. for each such number, that many copies of the corresponding input item will be returned.
  • (2*2!!#:) return a sequence like 0 2 0 2 ... 0 2 that matches the length of the input. this removes items at even indices (0-based), and doubles those at odd indices
\$\endgroup\$
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
    Commented Jan 18, 2022 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
    Commented Jan 20, 2022 at 10:30
2
\$\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\$
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\$

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
  • \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Commented Feb 23, 2022 at 16:36
  • \$\begingroup\$ Welcome to Code Golf, and nice first answer! Unfortunately, assuming input and output is in a variable in not an accepted method of I/O \$\endgroup\$ Commented Feb 23, 2022 at 17:12
  • \$\begingroup\$ @cairdcoinheringaahing Should be fixed now \$\endgroup\$ Commented Feb 23, 2022 at 17:24
2
\$\begingroup\$

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

eh]y

Try it online!

Explanation

eh]y
   y   Uninterleave the input - return a list like [a[::2], a[1::2]]
  ]    Get the last element (which is a[1::2])
e      Over each character...
 h     Double it
\$\endgroup\$
2
  • \$\begingroup\$ Can you explain it? \$\endgroup\$
    – Fmbalbuena
    Commented Dec 3, 2022 at 17:55
  • \$\begingroup\$ @Fmbalbuena Sure, done \$\endgroup\$
    – naffetS
    Commented Dec 3, 2022 at 18:06
2
\$\begingroup\$

4, 25 bytes

3.72372382352352372372394

Try it online!

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

Thunno 2, 4 bytes

zt2Ḅ

Attempt This Online!

Explanation

zt2Ḅ  # Implicit input
z     # Uninterleave
 t    # Leave only the tail
  2Ḅ  # Double each character
      # Implicit output
\$\endgroup\$

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.