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

2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Uiua, 8 bytes

▽+.◿2⇡⧻.

Try it!

▽+.◿2⇡⧻.
       .  # duplicate
      ⧻   # length
     ⇡    # range
   ◿2     # modulo two
 +.       # double
▽         # keep
\$\endgroup\$
2
\$\begingroup\$

Nekomata, 3 bytes

ĭ:Ĭ

Attempt This Online!

ĭ:Ĭ
ĭ   Uninterleave
 :  Duplicate
  Ĭ Interleave
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
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
    Commented Jan 18, 2022 at 17:13
  • \$\begingroup\$ @Adám Thanks. Done. \$\endgroup\$
    – Graham
    Commented Jan 18, 2022 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
    Commented Jan 18, 2022 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\$ Commented Jan 18, 2022 at 18:38
  • \$\begingroup\$ -1 when written as a train: ⊢⊢⍤/⍨0 2⍴⍨≢ \$\endgroup\$ Commented Jan 18, 2022 at 20:50
  • \$\begingroup\$ I thought you still had to include the braces in a tacid definition @kamila szewczyk \$\endgroup\$ Commented Jan 19, 2022 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\$ Commented Jan 19, 2022 at 13:42
  • \$\begingroup\$ @JayantChoudhary tacit definitions don't require braces, only parens. \$\endgroup\$ Commented Jan 19, 2022 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\$
0
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
    Commented Jan 18, 2022 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\$

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
\$\endgroup\$
2
1
\$\begingroup\$

Wolfram Language (Mathematica), 23 bytes

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

Try it online!

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

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

munge, 20 bytes

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

Munge it!

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

Ruby, 32 bytes

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

Attempt This Online!

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

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.

\$\endgroup\$
4
  • \$\begingroup\$ Can you not use $.%2*2? \$\endgroup\$
    – Neil
    Commented Jan 20, 2022 at 10:33
  • \$\begingroup\$ Ah nice! I can't use that, but I could use $|--*2! Thanks! \$\endgroup\$ Commented Jan 20, 2022 at 11:29
  • \$\begingroup\$ Ah, right, the indexing was the wrong way around. You should update your explanation now that you're not using $. now though. \$\endgroup\$
    – Neil
    Commented Jan 20, 2022 at 11:54
  • \$\begingroup\$ You're right, have done! Thank you! \$\endgroup\$ Commented Jan 20, 2022 at 11:58
1
\$\begingroup\$

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.


About the code...

  • {@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.


You can try this on http://sandbox.onlinephpfunctions.com/code/b006b2ec293bab18cd6db2f1b8a2454da047ea49

Please pick PHP 7.4.13.

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

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

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

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
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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, 108 106 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!

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

Rattle, 25 bytes

|!I=@[0q][=#%[1=#1g`bb]]@

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              
\$\endgroup\$
1
\$\begingroup\$

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 ""
\$\endgroup\$
1
\$\begingroup\$

Japt -hP, 5 4 bytes

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

¬m²ó

Try it

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

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 _
  # Read two more characters
  N
  N
}
\$\endgroup\$
0
\$\begingroup\$

><>, 11 bytes

ii::1+?!;oo

Try it online!

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

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)
\$\endgroup\$
2
  • \$\begingroup\$ Æï¥∞* seems to work for 5 bytes. \$\endgroup\$
    – Neil
    Commented Jan 18, 2022 at 17:58
  • \$\begingroup\$ @Neil Ah, of course! Thanks. :) \$\endgroup\$ Commented Jan 18, 2022 at 18:22
0
\$\begingroup\$

Python 3, 74 bytes

from functools import*
lambda s:reduce(tuple.__add__,zip(s[1::2],s[1::2]))

Try it online!

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

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!

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

Burlesque, 6 bytes

2en)^^

Try it online!

2en # Take every 2nd
)^^ # Map duplicate char
\$\endgroup\$
0
\$\begingroup\$

J-uby, 21 bytes

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

Attempt This Online!

\$\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.