Remove odd indices and double the even indices

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.

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

Coconut, 40 bytes

"".join(map(->_*2,input()[1::2]))print


Try it online!

I'm just learning Coconut, so there's probably a shorter, better way to do this... Feedback 100% welcomed!

Here's how it works:

            ->_*2                        - def lambda that dup's a char
input()                - read string from STDIN
[1::2]          - make a list of even num chars
map(    ,              )         - map the lambda over char list
"".join(                        )        - convert list to a string
print - apply "print" implicit lambda


Uiua, 8 bytes

▽+.◿2⇡⧻.


Try it!

▽+.◿2⇡⧻.
.  # duplicate
⧻   # length
⇡    # range
◿2     # modulo two
+.       # double
▽         # keep


Nekomata, 3 bytes

ĭ:Ĭ


Attempt This Online!

ĭ:Ĭ
ĭ   Uninterleave
:  Duplicate
Ĭ Interleave


Pip, 9 8 bytes

S*<>aX:2


Attempt This Online!

Explanation

    a    ; Command-line argument
<>     ; Break into two-character substrings
S*       ; Remove the first character of each
X:2 ; Double each
; Concatenate together and output (implicit)


If the argument string has an odd number of characters, the final substring created by <> will have one character in it. S will remove that one character, leaving the empty string. Doubling the empty string and concatenating it to the output does not affect the output.

APL+WIN, 15 14 bytes

Prompts for string

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


Try it online! Thanks to Dyalog Classic

• Use ⍞ instead of ⎕ so the user doesn't need to deal with quotes and 'a' being a scalar. Then you certainly don't need ,
Commented Jan 18, 2022 at 17:13
• @Adám Thanks. Done. Commented Jan 18, 2022 at 17:24

APL (Dyalog Unicode), 13 bytes

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


Try it online!

pretty self explnatory

• A translation of my J answer would probably save some bytes. Commented Jan 18, 2022 at 18:05

APL (Dyalog Unicode), 12 bytes

{⍵/⍨0 2⍴⍨≢⍵}


Try it online!

APL port of @jonah 's j answer

• my bad ,fixed it @jonah Commented Jan 18, 2022 at 18:38
• -1 when written as a train: ⊢⊢⍤/⍨0 2⍴⍨≢ Commented Jan 18, 2022 at 20:50
• I thought you still had to include the braces in a tacid definition @kamila szewczyk Commented Jan 19, 2022 at 2:50
• @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. Commented Jan 19, 2022 at 13:42
• @JayantChoudhary tacit definitions don't require braces, only parens. Commented Jan 19, 2022 at 14:31

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


Python 2, 34 bytes

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


Try it online!

• Seems to work for Python 3 just as well. Commented Jan 18, 2022 at 20:54

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


sed 4.2.2, 5 bytes

n;p;p


Try it online!

Takes string input one character per line (if allowed).

• -n option Don't print by default
• n; Skips a line.
• p;p Print the line twice.

sed 4.2.2, 15 bytes

s/.$$.$$/\1\1/g


Try it online!

Solution taking a single string.

• s/.$$.$$/ Substitute any char, followed by any other char...
• \1\1/ With the 2nd char repeated twice...
• g Globally
• Commented Jan 18, 2022 at 22:13
– Neil
Commented Jan 20, 2022 at 10:24

Wolfram Language (Mathematica), 23 bytes

#~##&~#&/@#~Drop~;;;;2&


Try it online!

Input a list. Drops odd indices and doubles everything left.

• You can't assume that the input is empty Commented Jan 19, 2022 at 0:39
• @Fmbalbuena I assume you meant non-empty?
– att
Commented Jan 19, 2022 at 0:58
• yes, this is what i meant. Commented Jan 19, 2022 at 1:00
– att
Commented Jan 19, 2022 at 1:02
• Added, now you can see the last test case. Commented Jan 19, 2022 at 2:16

munge, 20 bytes

/.(.)?/=>{$1$1}


Munge it!

Ruby, 32 bytes

->s{s.scan(/.(.)/).map{_1*2}*''}

Attempt This Online!

• 19 bytes as a full program (with -p flag). Commented Jan 19, 2022 at 10:58
• @Dingus great! You may post your answer, i feel there's too much improvements to be just a suggestion Commented Jan 19, 2022 at 13:20

Perl 5 + -p, 11 bytes

Takes a list of chars (to differentiate from @Kjetil's answer).

2 bytes saved thanks to @Neil!

$_ x=$|--*2


Try it online!

Explanation

Uses the string repetition operator (x) to repeat $_ $|--*2 times which, since $| can only be 1 or 0, will alternate between 0 or 2 for each character of input. • Can you not use $.%2*2?
– Neil
Commented Jan 20, 2022 at 10:33
• Ah nice! I can't use that, but I could use $|--*2! Thanks! Commented Jan 20, 2022 at 11:29 • Ah, right, the indexing was the wrong way around. You should update your explanation now that you're not using $. now though.
– Neil
Commented Jan 20, 2022 at 11:54
• You're right, have done! Thank you! Commented Jan 20, 2022 at 11:58

SimpleTemplate 0.84, 36 bytes

Requires that an array of characters is passed to the render() method in PHP. Argument unpacking can be used for convenience. Each argument is 1 character.

{@eachargv}{@if__ is mod2}{@echo_,_}


This is a full program.

How it works?

It simply loops all over the arguments passed to the render() method.

It checks if the index is odd, and, in case it is, displays the character twice.

• {@eachargv}
This is a simple loop over the elements in argv.
Automatically, the current value is stored in the variable _ and the index is stored in __.
A more readable example is {@each argv as _ key __}

• {@if__ is mod2}
This checks if the key's modulo is a truthy value.
If it is a truthy value, it means we are dealing with an odd index (the language is 0-indexed, like PHP). A readable alternative is {@if __ is not multiple of 2}

• {@echo_,_}
Simply outputs the character twice.

• Usually, it would be needed to close the loop and the if, but it is optional since there isn't any other code to run outside.

Pyth, 6 bytes

.i=%2t


Test suite. Slightly more interesting one..

Explanation:

.i=%2t # whole program

# take the implicit input (Q)
t # remove the first letter
%2  # keep the odd indices only
%2t # keep the even indices only
=    # assign this to Q (the variable encountered)
.i     # then interleave this, with
# newly assigned implicit Q (implicit output)


7 bytes

This one uses a map in order to double the characters.

s+Rd%2t


Test suite

PLIS, 13 bytes

&R($0@@i@VfB)  Defines a function R which takes a sequence as its first and only parameter. The language implements strings as infinite sequences of 0s preceded by the string itself. This program works for all sequences, strings included. We define a function instead of using command line parameters because it is currently impossible to supply anything besides big integers through command line parameters. This would be 9 bytes if it were possible to supply, say, a string via command line ($0@@i@VfB).

print(R("umbrella"))        #=> mmrrllaa
print(R("Hello, World!"))   #=> eell,,WWrrdd
print(R("odd_length!"))     #=> dd__eegghh


Explanation

After expanding shortnames, we have the following, equivalent code:

&R($0@@A000035@A004526) &R( ) define a function R A000035 a(n) = n%2 ( 0 1 0 1 0 1 0 1 ... ) @ indices where true; ( 1 3 5 7 9 11 ... ) using shortnames, this is shorter than the equivalent A005408; compare CwB <=> @i @ index by A004526 non-negative integers repeated ( 0 0 1 1 2 2 ...) this gives ( 1 1 3 3 5 5 7 7 9 9 ... )$0@                       index input by this sequence


Dis, 4 bytes

}}{{


Offline interpreter on dis.web

Easy for this kind of task.

A string of any bytes shall be given from standard input, and outputs to standard output.

Explained

}    store odd-indexed byte to accumulator
59048 is stored if EOF
}   same for even-indexed byte
{  output the accumulator
halt if accumulator is 59048
{ same
implicitly NOP until cell 59048
loop back to cell 0


Rust, 118 bytes

fn main(){let y=&mut"".into();std::io::stdin().read_line(y);for(i,c)in(0..).zip(y.chars()){if i%2>0{print!("{c}{c}")}}}


Attempt This Online!

Half is just stdin...

Else,

Rust, 108106 94 bytes

|c:&str|{let mut s=format!("");for(i,c)in(0..).zip(c.chars()){if i%2>0{s.push(c);s.push(c)}}s}


Attempt This Online!

• I know Rust isn't great for everything... Commented Dec 4, 2022 at 0:53
• You can just use a block instead of a named function to save several bytes: Try it online! Commented Dec 4, 2022 at 14:27
• Right, not sure if an explicit function was required Commented Dec 4, 2022 at 15:22
• You don't need the space at &mut "" Commented Dec 4, 2022 at 20:22
• Um, that's weird. the official rust playground treats the mut in that case as a string prefix... Commented Dec 4, 2022 at 20:58

Rattle, 25 bytes

|!I=@[0q][=#%[1=#1gbb]]@


Try it Online!

Might not be the most elegant solution...

Explanation

|                  parse input
!                 disables implicit output (necessary only for special case of empty input)
I                save input as char array in consecutive memory slots, move pointer to next empty slot
=@              set top of stack to value of pointer
[0q]          if the pointer is in the zero position (meaning empty input), terminate the program
[     ]@  repeat x number of times where x is the value of the pointer (to iterate over all input characters)

=#               set top of stack to current loop iterator
%              mod 2
[1       ]    if the result is 1...
=#1         set top of stack to current loop iterator for main loop
g       get value from the memory slot according to the top of the stack
bb     add char to output buffer twice with no separator


Rockstar, 70 bytes

listen to S
cut S
O's ""
while S
roll S
let O be+roll S*2 or ""

say O


Try it (Code will need to be pasted in)

Or, if we can output each pair of characters separately:

51 bytes

listen to S
cut S
while S
roll S
say roll S*2 or ""


Japt-hP, 5 4 bytes

The ¬ can be removed if we can take input as a character array.

¬m²ó


Try it

Acc!!, 46 bytes

N
N
Count i while _/32 {
Write _
Write _
N
N
}


Try it online!

Explanation

# Read two characters
N
N
# Loop while the most recent character code is 32 or greater
Count i while _/32 {
# Write the most recent character twice
Write _
Write _
N
N
}


><>, 11 bytes

ii::1+?!;oo


Try it online!

MathGolf, 6 5 bytes

{ï¥∞*


-1 byte thanks to @Neil.

Try it online.

Explanation:

{      # Foreach over the characters of the (implicit) input-string:
ï     #  Push the 0-based loop index
¥    #  Modulo-2 this index
∞   #  Double it
*  #  Repeat the current character that many times (0 results in an empty string)
# (after which the entire stack joined together is output implicitly)

• Æï¥∞* seems to work for 5 bytes.
– Neil
Commented Jan 18, 2022 at 17:58
• @Neil Ah, of course! Thanks. :) Commented Jan 18, 2022 at 18:22

Python 3, 74 bytes

from functools import*


Try it online!

SNOBOL4 (CSNOBOL4), 76 74 bytes

	I =INPUT
N	I POS(X) LEN(1) (LEN(1) | '') . E @X =E E	:S(N)
OUTPUT =I
END


Try it online!

Burlesque, 6 bytes

2en)^^


Try it online!

2en # Take every 2nd
)^^ # Map duplicate char


J-uby, 21 bytes

~:gsub&'\1\1'&/.(.)?/


Attempt This Online!