79
\$\begingroup\$

Super simple challenge today, or is it?

I feel like we've heard a fair bit about double speak recently, well let's define it in a codable way...

Double speak is when each and every character in a string of text is immediately repeated. For example:

"DDoouubbllee  ssppeeaakk!!"

The Rules

  • Write code which accepts one argument, a string.
  • It will modify this string, duplicating every character.
  • Then it will return the double speak version of the string.
  • It's code golf, try to achieve this in the smallest number of bytes.
  • Please include a link to an online interpreter for your code.
  • Input strings will only contain characters in the printable ASCII range. Reference: http://www.asciitable.com/mobile/

Leaderboards

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

var QUESTION_ID=188988;
var OVERRIDE_USER=53748;
var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;function answersUrl(d){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+d+"&pagesize=100&order=asc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(d,e){return"https://api.stackexchange.com/2.2/answers/"+e.join(";")+"/comments?page="+d+"&pagesize=100&order=asc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(d){answers.push.apply(answers,d.items),answers_hash=[],answer_ids=[],d.items.forEach(function(e){e.comments=[];var f=+e.share_link.match(/\d+/);answer_ids.push(f),answers_hash[f]=e}),d.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(d){d.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),d.has_more?getComments():more_answers?getAnswers():process()}})}getAnswers();var SCORE_REG=function(){var d=String.raw`h\d`,e=String.raw`\-?\d+\.?\d*`,f=String.raw`[^\n<>]*`,g=String.raw`<s>${f}</s>|<strike>${f}</strike>|<del>${f}</del>`,h=String.raw`[^\n\d<>]*`,j=String.raw`<[^\n<>]+>`;return new RegExp(String.raw`<${d}>`+String.raw`\s*([^\n,]*[^\s,]),.*?`+String.raw`(${e})`+String.raw`(?=`+String.raw`${h}`+String.raw`(?:(?:${g}|${j})${h})*`+String.raw`</${d}>`+String.raw`)`)}(),OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(d){return d.owner.display_name}function process(){var d=[];answers.forEach(function(n){var o=n.body;n.comments.forEach(function(q){OVERRIDE_REG.test(q.body)&&(o="<h1>"+q.body.replace(OVERRIDE_REG,"")+"</h1>")});var p=o.match(SCORE_REG);p&&d.push({user:getAuthorName(n),size:+p[2],language:p[1],link:n.share_link})}),d.sort(function(n,o){var p=n.size,q=o.size;return p-q});var e={},f=1,g=null,h=1;d.forEach(function(n){n.size!=g&&(h=f),g=n.size,++f;var o=jQuery("#answer-template").html();o=o.replace("{{PLACE}}",h+".").replace("{{NAME}}",n.user).replace("{{LANGUAGE}}",n.language).replace("{{SIZE}}",n.size).replace("{{LINK}}",n.link),o=jQuery(o),jQuery("#answers").append(o);var p=n.language;p=jQuery("<i>"+n.language+"</i>").text().toLowerCase(),e[p]=e[p]||{lang:n.language,user:n.user,size:n.size,link:n.link,uniq:p}});var j=[];for(var k in e)e.hasOwnProperty(k)&&j.push(e[k]);j.sort(function(n,o){return n.uniq>o.uniq?1:n.uniq<o.uniq?-1:0});for(var l=0;l<j.length;++l){var m=jQuery("#language-template").html(),k=j[l];m=m.replace("{{LANGUAGE}}",k.lang).replace("{{NAME}}",k.user).replace("{{SIZE}}",k.size).replace("{{LINK}}",k.link),m=jQuery(m),jQuery("#languages").append(m)}}
body{text-align:left!important}#answer-list{padding:10px;float:left}#language-list{padding:10px;float:left}table thead{font-weight:700}table td{padding:5px}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/codegolf/primary.css?v=f52df912b654"> <div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> 

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, [Other information] N bytes

where N is the size of your submission. Other information may include flags set and if you've improved your score (usually a struck out number like <s>M</s>). N should be the right-most number in this heading, and everything before the first , is the name of the language you've used. The language name and the word bytes may be links.

For example:

# [><>](http://esolangs.org/wiki/Fish), <s>162</s> 121 [bytes](https://esolangs.org/wiki/Fish#Instructions)
\$\endgroup\$
10
  • 3
    \$\begingroup\$ It will modify this string. Are you intentionally requiring pass-by-reference and modify in-place? And then return a copy or reference to that modified string? If so, languages like asm or C would need to accept an explicit-length string (pointer + length) where the length is either the current string length (with the buffer being twice that size), or it's the total size and you need to duplicate the low half. Thus you need to start from the end and work backwards, or allocate scratch space and then copy back. But there are answers in C and 8086 asm that totally violate all that. \$\endgroup\$ Aug 3 '19 at 1:49
  • 4
    \$\begingroup\$ @PeterCordes I do not care if it modifies the same object or builds a new one. \$\endgroup\$
    – AJFaraday
    Aug 3 '19 at 5:08
  • 3
    \$\begingroup\$ I'd suggest wording it as "modify (or produce a modified copy) of the string" to explicitly allow answers that do or don't modify in-place. Simplifying the wording to "return a string that's twice as long, with each character repeated" would be nice but then it's not clear if void foo(char *c, size_t len) is legal that takes one input/output buffer and a length, and doesn't have any return value, just a side-effect on the object it has a pointer to. \$\endgroup\$ Aug 3 '19 at 5:16
  • \$\begingroup\$ Can the string be empty? \$\endgroup\$ Aug 6 '19 at 19:58
  • 1
    \$\begingroup\$ @cschultz2048 it says the string will only contain printable ascii characters, so that implies that they’ll always be populated. I’d expect that any code for this challenge would leave an empty string empty... anyway, I don’t think it’s a test case that I’d use for this. \$\endgroup\$
    – AJFaraday
    Aug 6 '19 at 22:36

199 Answers 199

1
3 4 5 6
7
1
\$\begingroup\$

Python 3, 31 bytes

lambda s:"".join(c*2for c in s)

-4 bytes thank you to Redwolf Programs for suggesting lambda method.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Hi! Taking input by assuming a variable contains it isn't allowed, but you can use a lambda (and if you do, there's no need for a print!). \$\endgroup\$ Jan 29 at 15:32
1
\$\begingroup\$

VBScript, 88 bytes

s=wscript.arguments(0):k=""
for i=0 to len(s)-1
n=mid(s,i+1,1)
k=k&n&n&" "
next
msgbox k

First argument as the string

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

Duocentehexaquinquagesimal, 3 bytes

6÷V

Try it online!

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

Unlambda, 11 bytes

``ci`c`|`@|

Try it online!

On its own, `|`@| is an identity function with a side effect of reading a char from the input and printing it twice. `ci is a call-with-cc trickery that ends up applying its argument to itself, hence `c`|`@| is the same with the above-mentioned side effect; all in all, this will set up a loop.

(The solution is memory inefficient, as each iteration will create a new continuation, hence the space used will be linear or quadratic in the length of the input, depending on the implementation. This can be fixed at the expense of adding two more characters: ``ci`d`c`|`@|.)

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

Ly, 7 bytes

ir[:oo]

Try it online!

Oddly enough, this turned out to be similar to the Brainfuck answer someone else posted...

i        - reads in a line, adds each character to the stack as a codepoint
 r       - reverse the stack
  [   ]  - a "loop while stack not empty" construct
   :     - duplicate the top of the stack
    o    - pop the top of the stack and print codepoint as char
      o  - again... 
\$\endgroup\$
1
\$\begingroup\$

V (vim), 5 bytes

òylpl

Try it online!

òylpl
ò       " loop until error
 yl     " yank character under cursor
   p    " paste it
    l   " move right

Alt:

V (vim), 5 bytes

òälll

Try it online!

ä{motion} is a synonym for y{motion}P which is almost what we want but pastes backwards rather than forwards, so we need another l here, tying the vim-ier first solution.

Equivalent vim version:

Vim, 11 bytes

qqylpl@qq@q

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Another 5 byte V: Ó./&&. Ironically, this can be ported perfectly to vim and also ties your vim solution haha: :s/./&&/g<cr> \$\endgroup\$
    – DJMcMayhem
    Apr 20 at 15:33
1
\$\begingroup\$

<>^v, 29 bytes

.—""?§0 27 0«0 82!=0_~~?(___|

Explanation

.—""?§0 27 0«0 82!=0_~~?(___|

.                             Input string, push to stack
 —                            (not a minus) Reverse the string
  ""                          Push empty string to stack
    ?                         Swap top two elements
     §                        Split top of stack (now the user input) with second element of stack (`""`)
      0                       Push 0
        27                    Push 27
           0                  Push 0
            «                 Goto ; redirects program to the `|`
                            | Reverses instruction pointer direction, now going left
                           _  Pop stack
                          _   Pop stack (again)
                         _    For the third time, pop the stack
                        )     Decrement top of stack
                       ?      Swap top two elements
                      ~       Print top of stack without newline
                     ~        Print top of stack without newline (again, for the double character)
                    _         Pop stack
                   0          Push 0
                  =           Execute next instruction only if top two elements of the stack are equal
                 !            Exit, executed only if the top two elements of the stack are equal
               82             Push 28 (pointer is going left, so literal 82 -> value 28
             0                Push 0
            «                 Goto _
                          _   (Loop goes on, go back to the first "Pop stack")

It first reverses the string, then splits it into characters. Then it goes into a loop reverse-iterating the characters and printing them twice, popping them at the same time, each time decrementing the "characters left" counter which is at the top of the stack, and exits when "characters left" is 0.

Mostly inspired by this other answer I made

run online (> is the prompt, cannot remove it)

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

SM83, 8 bytes

Input string pointer in de, output string pointer in hl

1A 13 23 B7 C8 23 18 F8
dbl:
    ld a,(de)               // 1A       read
    inc de                  // 13       and increment
    ld (hl+),a              // 23       write and increment
    or a                    // B7       cheap test for zero
    ret z                   // C8       return if zero
    ld (hl+),a              // 23       write and increment again
    jr dbl                  // 18 F8    loop
\$\endgroup\$
1
\$\begingroup\$

A0A0, 165 bytes

A0A0
A0C3G1G1G1G1G1A0
A0I1A6V0P0G6C6A0
A0A1G-3G-3G-3G-3G-3A0
G-3

A0A0
C3G1G1A0C3G1G1A0

A0A1G-3G-3A0
G-3
A0A0
C3G1G1A0C3G1G1A0
G-10G1G1A0G-10G1G1A0
A0A1G-3G-3A0
G-3

The program consists of three loops right after one another. The first loop does the following:

I1A6V0P0G6C6
I1           ; take character input, store in V0
  A6         ; append this line to the line six below
    V0       ; operand, holds input
      P0     ; prints input
        G6   ; jumps to six lines below
          C6 ; removes all instructions on the six lines below

We duplicate the operand and print instructions a little lower, so we can print it twice. We then jump to there and the last C6 is to get rid of any other instructions that we didn't need afterwards.

The second loop is empty and is constructed from the first loop. The loop is also partially executed so we can enter the loop at the same place each time. This loop will print the character a second time. Because there is a nice G6 instruction inside the copied instructions that will also be executed, we use that as a way to escape from the infinite loop. This is because six lines below is yet another loop.

G-10G1G1
G-10     ; jumps 10 lines up, back to the first loop
    G1   ; no-op
      G1 ; no-op

This is a simple infinite loop (also partially executed) that takes us back to the first loop. It's padded with no-ops, since the loops needs at least three instructions. Because the first loop contains a C6, any redundant instructions in the second loop will be removed once we get back to the first loop.

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

Pxem, filename: 17 bytes.

\001 is such a byte of binary.

.w.o.o.i.c.c\001.+.a

How it works

Since this program is simple, here is verbose explaination.

XX.z
# while stack is empty or popped value is not zero; do
# NOTE stack is initially empty
.a.wXX.z
  # if not empty; then pop to output its character; fi
  # if not empty; then pop to output its character; fi
  .a.o.oXX.z
  # getchar and push its codepoint value
  # NOTE EOF is -1
  .a.iXX.z
  # if not empty; then duplicate; fi
  # if not empty; then duplicate; fi
  .a.c.cXX.z
  # push one
  # if stack has two or more items; then pop twice and push their sum; fi
  .a\001.+XX.z
# done
# NOTE reaching to end of filename implicitily terminates the program
.a.a

Try it online!

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

Arduino, 113 bytes

#define S Serial
int c;void setup(){S.begin(300);}void loop(){if(S.available()){S.write(c=S.read());S.write(c);}}

Pretty simple. If there are unread characters from the serial input, write them twice.

Explanation:

#define S Serial /* This abbreviation is worth it if it's used more than twice */

int c;

void setup() {
  S.begin(300); // begin serial communications
}

void loop() {
  if (S.available()) { // if there are bytes to be read
    S.write(
      c = S.read() // read, store, and write it
    );
    S.write(c); // write the stored value
  }
}
\$\endgroup\$
1
\$\begingroup\$

MineFriff, 4 bytes

:oo~

Try it online!

No, I totally didn't forget to add input to MineFriff. You have to supply the string to double speak in the header, which can be generated using this Vyxal snippet.

Explained

:oo~
:    { duplicate the top of the stack }
 oo  { print it twice }
   ~ { back to the start of the line }
\$\endgroup\$
1
\$\begingroup\$

Dis, 3 bytes.

}{{

Try it online!

How it works

} ( a=getchar;if a==EOF a=59048;)
 { ( if a==59048 exit; else putchar a%256; )
   { ( again)
( then keep program counter+=1 until 59048 where each cell from i=3 to 59048 is NOP;
   then pc=0 if pc is 59048) 
\$\endgroup\$
1
\$\begingroup\$

Ulpia, 20 bytes

<-0_0<?([hlt])0_->->

Explanation:

; start
    <-   ; get character from `stdin` and push it to the stack (index = 0)
    0 _  ; push stack[0], the character we read (index = 1)
    0    ; push 0 to the stack (index = 2)
    <    ; char_read < 0 => char_read == -1
    ?(   ; pop the condition and halt if it's true
        [hlt]
    )
    ; else
    0 _  ; push stack[0] again (index = 1)
    ->   ; pop and print the value from the top of the stack, once
    ->   ; ...twice, now stack is empty again
    ; The implementation implicitly adds a unconditional/absolute jump at
    ; the end of the program, back to the "start", thus we loop over.
\$\endgroup\$
1
\$\begingroup\$

Python, 67 bytes

from itertools import*
print(*chain(*[i*2for i in input()]),sep="")
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 4 2 bytes

2•

Try it Online!

Removed implicit input and output. Thanks @emanresu A

Doesn't beat Y but works nonetheless.

\$\endgroup\$
1
  • \$\begingroup\$ Nice! You don't need the ? or , due to implicit input/output. \$\endgroup\$
    – emanresu A
    Sep 16 at 9:01
1
\$\begingroup\$

Minim, 73 Bytes (44 w/o Output)

Program is run as follows:

C:\Users\chris> minim -a "Double speak!" -f golf.min

Arrays of values, and subsequently strings, cannot be returned or passed around to statements, and the interpreter uses only index 0 for a 'return/exit value'. Therefore I've opted to just print the modified string to the console.

[0]=L*2+1.[1:]=A.[[0]]=[[0]--/2]._^![0].C=1.[0]=2.$<[[0]++]._^![[0]].C=5.

With whitespace and comments:

;;; Insert string into memory and modify it

[0] = L * 2 + 1.         ; Set index 0 to twice the length of the program arguments plus 1
[1 :] = A.               ; Insert program arguments at index 1
    [[0]] = [[0]-- / 2]. ; Set index of index 0 to half of memory at index of index 0, and decrement index 0
    _^ ![0].             ; Skip the next statement if index 0 is 0
C = 1.                   ; Set program counter to 1

;;; Output modified string (no string/array return)

[0] = 2.        ; Set index 0 to 2.
    $< [[0]++]. ; Print index of index 0 as Unicode, and increment index 0
    _^ ![[0]].  ; Skip the next statement if index of index 0 is 0
C = 5.          ; Set program counter to 5

Without printing it, the program goes down to just 44 bytes.

[0]=L*2+1.[1:]=A.[[0]]=[[0]--/2]._^![0].C=1.

The modified string is now just in memory.

GitHub Repository

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

CLC-INTERCAL, 85 bytes.

DO;1<-#1DOCOMEFROM#2DOWRITEIN;1(1)DO:1<-;1SUB#1DDOCOMEFROM':1~:1'~#1(2)DOREADOUT;1+;1

Copy and paste to try it online!

With comments, though I don't know its necessity

DONOTE length of array
DO;1<-#1
DOCOMEFROM#2
DONOTE perform getchar() for a time to store to ;1SUB#1
DOWRITEIN;1
DONOTE EOF: zero; otherwise: at least 65536
(1)DO:1<-;1SUB#1
DONOTE die here
D
DONOTE ':1~:1'~#1: zero if EOF one otherwise
DOCOMEFROM':1~:1'~#1
DONOTE putchar() twice
(2)DOREADOUT;1+;1
\$\endgroup\$
0
\$\begingroup\$

Python 3, 31 24 bytes

-7 bytes by returning a list of characters instead of a string

lambda s:[c*2for c in s]

Returns a list of characters instead of a string.

Try it online!

\$\endgroup\$
1
3 4 5 6
7

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.