Full width text is text that has a space after every character, including the last one. For instance, the first sentence of this question becomes:
F u l l w i d t h t e x t i s t e x t t h a t h a s a s p a c e a f t e r e v e r y c h a r a c t e r , i n c l u d i n g t h e l a s t o n e .
Write a program that takes a line in text from standard input and outputs it as full-width text to standard out.
Leaderboard
var QUESTION_ID=75979,OVERRIDE_USER=52353;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.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(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;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="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <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><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><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>
-
8\$\begingroup\$ Usually you should allow functions too, or you exclude a lot of languages (e.g. JavaScript). \$\endgroup\$wizzwizz4– wizzwizz42016年03月22日 18:00:20 +00:00Commented Mar 22, 2016 at 18:00
-
10\$\begingroup\$ We have a few defaults for I/O that are based on community consensus. While you are entitled to override them, insisting on STDIN/STDOUT for I/O invalidates a bunch of answers (which assumed that the defaults apply) and make the task downright impossible in other languages (they don't have standard streams). \$\endgroup\$Dennis– Dennis2016年03月22日 21:22:58 +00:00Commented Mar 22, 2016 at 21:22
-
65\$\begingroup\$ That is not what fullwidth text is. \$\endgroup\$BlueRaja - Danny Pflughoeft– BlueRaja - Danny Pflughoeft2016年03月22日 21:53:15 +00:00Commented Mar 22, 2016 at 21:53
-
4\$\begingroup\$ @BlueRaja-DannyPflughoeft is right. Full Width text is about underlying character encoding ( 2 bytes encoded ) required by some language ( i.e. ideograms ). In Unicode the notion of half and full size is called Unicode block \$\endgroup\$Ludovic Frérot– Ludovic Frérot2016年03月23日 09:27:15 +00:00Commented Mar 23, 2016 at 9:27
-
3\$\begingroup\$ @LudovicFrérot Actually, these are not ideograms, these are chinese english letters. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年07月01日 08:29:17 +00:00Commented Jul 1, 2016 at 8:29
151 Answers 151
Go, (削除) 154 (削除ここまで) 120 bytes
package main
import(
."fmt"
."os"
."strings")
func main(){s:=Join(Args[1:], " ")
for _,c:=range s{Print(string(c)," ")}}
Saved a few bytes with command line args.
LiveScript, 17.
->"#{it/''*' '} "
Scala, 35 bytes
Console.readLine.map(_+" ")mkString
Example:
scala> Console.readLine.map(_+" ")mkString
res1: String = "H e l l o S c a l a ! "
PS. A function would be slightly shorter (32 bytes):
(s:String)=>s.map(_+" ")mkString
Grond, (削除) 22 (削除ここまで) 21 chars
a(p().sp('').jn(' '))
Edit: removed semicolon
compiler in js below
String.prototype.reverse = function() {
return this.split("").reverse().join("");
}
function compile(source){
var wc = source; // wc stands for working copy
var lastWC;
while (true) {
lastWC = wc;
wc = wc.reverse();
wc = wc.replace(/\(a(?!\w)/, "(trela"); //alert ,a
wc = wc.replace(/\(f(?!\w)/, "(rof"); // for, f
wc = wc.replace(/\(w(?!\w)/, "(elihw"); //while,w
wc = wc.replace(/\(p(?!\w)/, "(tpmorp"); //prompt,p
wc = wc.replace(/\(vr\./, "(esrever."); //reverse, rv
wc = wc.replace(/\(tm\./, "(chtam."); // match, mt
wc = wc.replace(/\(nj\./, "(nioj."); //join, .jn
wc = wc.replace(/\(pr\./, "(ecalper."); //replace, .rp
wc = wc.replace(/\(ls\./, "(.ecils"); //slice, sl
wc = wc.replace(/\(lt\./, "(esaCrewoLot."); //toLowerCase, tl
wc = wc.replace(/\(ut\./, "(esaCreppUot."); //toUpperCase, tu
wc = wc.replace(/\(ps\./, "(tilps."); //split, sp
wc = wc.reverse();
wc = wc.replace(/\.l(?!\w)/, ".length"); // .length, .l
if (lastWC === wc)
break;
}
// quine builtin commented out because it messes things up TODO: Fix it.
wc = " String.prototype.la=function(h){var x=this.split(''); x[h] = x[h].toLowerCase(); return x.join('');};String.prototype.ca=function(h){var x=this.split(''); x[h] = x[h].toUpperCase(); return x.join('');};String.prototype.reverse=function(){return this.split('').reverse().join('');}; \n var h = ['hello, world', 'Hello, World', 'Hello, World!', 'hello, world!', 'hello world', 'hello world!', 'Hello World', 'Hello World!', 'Hello, world.', 'Hello, World.' ,'hello world.' , 'Hello World.']; /* var q ='" + source + "';*/ " + wc;
return wc;
}
eval(compile("a(p().sp('').jn(' '));"));//source
Ruby, 41 bytes
puts gets.chomp.chars.map{|e|e+" "}.join
chomp eats trailing newlines.
-
1\$\begingroup\$ There is no requirement to output the whole result at once. If you output it piece-by-piece, you can spare the
.joincall. Even more, if you not need to return the pieces from the code block, then you can spare the.mapcall too, as.charsalso accepts code block. And there is no restriction against adding a space after the trailing newline too, if there is one.gets.chars{|e|$><<e+" "}\$\endgroup\$manatwork– manatwork2016年03月23日 10:47:51 +00:00Commented Mar 23, 2016 at 10:47
Mathematica, 42 bytes
Print@@(#<>" "&)/@Characters@InputString[]
Pretty self-explanatory. InputString takes a line from STDIN, Characters converts it to a list of characters, (#<>" "&)/@ produces a new string with spaces after those characters, and Print prints the result.
Reng v.1.2, 15 bytes
Try it out here! The input is given as "input".
ai p|$raoWo?!|~
a is a one-way mirror from the left. ai p| gets all input, $ drops the residual -1 not found input, r reverses the stack, and aoWo?!|~ is an output loop. W pushes 32, the space, and outputs it after outputting the character. ?! breaks out if not true, and ~ ends the program.
Jolf, 7 bytes
p4i*li♣
p4 interweave
i the input
*li♣ with a string of spaces with = length to the input
RETURN, 10 bytes
`"" ̈{' °},
Takes input from STDIN. It's just take STDIN, split along chars, join with space, output.
Clojure, 34 bytes
#(apply str(mapcat(fn[x][x" "])%))
Clojure is amazingly fun to work with but annoying to golf. Usually there's a really nice idiomatic solution to these challenges that isn't that many identifiers, but it's massive thanks to things like the identity function being identity (not fully golfed in these forms):
(defn fw [s] (apply str (interleave s (repeat " "))))
or
(defn fw [s] (apply str (mapcat (juxt identity (constantly " ")) s)))
Convex, 3 bytes
Convex is a new language that I am developing that is heavily based on CJam and Golfscript. The interpreter and IDE can be found here. Input is an integer into the command line arguments. Indexes are one-based. Uses the CP-1252 encoding.
S*S
Explanation:
# Implied input
S* # Join by spaces
S # Add a space at the end
# Implied output
Additionally, @DonMuesli's CJam answer without the l (so Sf+) will also work for 3 bytes.
-
\$\begingroup\$ Update this post later? \$\endgroup\$CalculatorFeline– CalculatorFeline2016年03月23日 02:25:09 +00:00Commented Mar 23, 2016 at 2:25
-
\$\begingroup\$ @CatsAreFluffy oh, my bad, I forgot, will do now. \$\endgroup\$GamrCorps– GamrCorps2016年03月23日 02:38:27 +00:00Commented Mar 23, 2016 at 2:38
O 1.2, 6 bytes
I' ]^o
Sample run:
bash-4.3$ java xyz.jadonfowler.o.O <(echo "I' ]^o") <<< 'Full width text.'
F u l l w i d t h t e x t .
This could be 5 characters in O 2 as IT]^o, but sadly it not seems to fully support the ^ command yet.
Perl 5, (削除) 11 (削除ここまで) 10 + 1 flags = 11 bytes
really, the same as the Perl 6 one, but the sed syntax changed.
s/./$& /g
example:
perl -pe "s/./$& /g" <<< abc
(削除)
old:
perl -pne "s/(.)/1円 /g" <<< abc
(削除ここまで)
-
\$\begingroup\$ Can you save a byte by using
s/./$& /gp? \$\endgroup\$Neil– Neil2016年03月22日 22:01:34 +00:00Commented Mar 22, 2016 at 22:01 -
1\$\begingroup\$ The
-nflag is unnecessary, isn't it? \$\endgroup\$Doorknob– Doorknob2016年03月22日 22:40:25 +00:00Commented Mar 22, 2016 at 22:40 -
\$\begingroup\$ @Doorknob is indeed correct, you can omit
-p, also, how flexible is the output format? If a leading space is acceptables// /gworks too! \$\endgroup\$Dom Hastings– Dom Hastings2016年03月23日 08:47:45 +00:00Commented Mar 23, 2016 at 8:47 -
\$\begingroup\$ I don't think I'm allowed to have a leading space :). Thanks @Doorknob \$\endgroup\$Ven– Ven2016年03月23日 09:15:08 +00:00Commented Mar 23, 2016 at 9:15
ESMin, 5 chars / 7 bytes
ᴉü⬭+⬭
Short and sweet. Get input split along chars and join with space.
-
\$\begingroup\$ This doesn't produce a trailing space. \$\endgroup\$Conor O'Brien– Conor O'Brien2016年03月23日 15:48:04 +00:00Commented Mar 23, 2016 at 15:48
-
\$\begingroup\$ I think I fixed it, but I'm on my phone. \$\endgroup\$Mama Fun Roll– Mama Fun Roll2016年03月23日 16:12:49 +00:00Commented Mar 23, 2016 at 16:12
PowerShell, 32 bytes
-join([char[]]$args[0]|%{"$_ "})
Explanation
Take in the input to the function and convert it to a char array. Add one space to each character and rejoin. Nothing really fancy here.
-
\$\begingroup\$ Save a couple bytes by just joining it directly and encapsulating in a script block in a string --
"$([char[]]$args[0]-join' ') "\$\endgroup\$AdmBorkBork– AdmBorkBork2016年07月26日 16:02:15 +00:00Commented Jul 26, 2016 at 16:02
Prelude, 11 bytes
?(!8^+^+!?)
This needs a spec-compliant interpreter which reads and writes character codes (instead of integers).
The program is fairly simple.
?(...?)
This sets up a while(read() != EOF) loop. Then we simply have:
! Print input character.
8 Push 8.
^+ Duplicate, add. Gives 16.
^+ Duplicate, add. Gives 32.
! Print space.
Pip, 5 bytes
_.sMq
The special variable q reads a line of stdin. We Map to it the lambda function _.s, which concatenates a space to each character. By default, the resulting list is concatenated and autoprinted.
Ouroboros, 11 bytes
i.0<5*(o32o
Ouroboros is an esolang in which every line of code represents a snake eating its tail. It draws a fair amount of inspiration from 2D languages like Befunge and><>.
The i command reads a character, or -1 for EOF. We duplicate this, check if it's less than 0, and multiply the resulting 1 (true) or 0 (false) by 5.
- If we have reached EOF, the top of the stack is now
5. The(command tells the snake to eat that many characters of its tail. This results in swallowing the execution pointer (currently on(, the fifth-last character), which terminates the program. - Otherwise,
0characters are eaten, and execution continues. We have a copy of the input ASCII code on the stack, which we output viao; then output a space (ASCII 32) and loop back to the beginning.
Try it out
// Define Stack class
function Stack() {
this.stack = [];
this.length = 0;
}
Stack.prototype.push = function(item) {
this.stack.push(item);
this.length++;
}
Stack.prototype.pop = function() {
var result = 0;
if (this.length > 0) {
result = this.stack.pop();
this.length--;
}
return result;
}
Stack.prototype.top = function() {
var result = 0;
if (this.length > 0) {
result = this.stack[this.length - 1];
}
return result;
}
Stack.prototype.toString = function() {
return "" + this.stack;
}
// Define Snake class
function Snake(code) {
this.code = code;
this.length = this.code.length;
this.ip = 0;
this.ownStack = new Stack();
this.currStack = this.ownStack;
this.alive = true;
this.wait = 0;
this.partialString = this.partialNumber = null;
}
Snake.prototype.step = function() {
if (!this.alive) {
return null;
}
if (this.wait > 0) {
this.wait--;
return null;
}
var instruction = this.code.charAt(this.ip);
var output = null;
console.log("Executing instruction " + instruction);
if (this.partialString !== null) {
// We're in the middle of a double-quoted string
if (instruction == '"') {
// Close the string and push its character codes in reverse order
for (var i = this.partialString.length - 1; i >= 0; i--) {
this.currStack.push(this.partialString.charCodeAt(i));
}
this.partialString = null;
} else {
this.partialString += instruction;
}
} else if (instruction == '"') {
this.partialString = "";
} else if ("0" <= instruction && instruction <= "9") {
if (this.partialNumber !== null) {
this.partialNumber = this.partialNumber + instruction; // NB: concatenation!
} else {
this.partialNumber = instruction;
}
next = this.code.charAt((this.ip + 1) % this.length);
if (next < "0" || "9" < next) {
// Next instruction is non-numeric, so end number and push it
this.currStack.push(+this.partialNumber);
this.partialNumber = null;
}
} else if ("a" <= instruction && instruction <= "f") {
// a-f push numbers 10 through 15
var value = instruction.charCodeAt(0) - 87;
this.currStack.push(value);
} else if (instruction == "$") {
// Toggle the current stack
if (this.currStack === this.ownStack) {
this.currStack = this.program.sharedStack;
} else {
this.currStack = this.ownStack;
}
} else if (instruction == "s") {
this.currStack = this.ownStack;
} else if (instruction == "S") {
this.currStack = this.program.sharedStack;
} else if (instruction == "l") {
this.currStack.push(this.ownStack.length);
} else if (instruction == "L") {
this.currStack.push(this.program.sharedStack.length);
} else if (instruction == ".") {
var item = this.currStack.pop();
this.currStack.push(item);
this.currStack.push(item);
} else if (instruction == "m") {
var item = this.ownStack.pop();
this.program.sharedStack.push(item);
} else if (instruction == "M") {
var item = this.program.sharedStack.pop();
this.ownStack.push(item);
} else if (instruction == "y") {
var item = this.ownStack.top();
this.program.sharedStack.push(item);
} else if (instruction == "Y") {
var item = this.program.sharedStack.top();
this.ownStack.push(item);
} else if (instruction == "\\") {
var top = this.currStack.pop();
var next = this.currStack.pop()
this.currStack.push(top);
this.currStack.push(next);
} else if (instruction == "@") {
var c = this.currStack.pop();
var b = this.currStack.pop();
var a = this.currStack.pop();
this.currStack.push(c);
this.currStack.push(a);
this.currStack.push(b);
} else if (instruction == ";") {
this.currStack.pop();
} else if (instruction == "+") {
var b = this.currStack.pop();
var a = this.currStack.pop();
this.currStack.push(a + b);
} else if (instruction == "-") {
var b = this.currStack.pop();
var a = this.currStack.pop();
this.currStack.push(a - b);
} else if (instruction == "*") {
var b = this.currStack.pop();
var a = this.currStack.pop();
this.currStack.push(a * b);
} else if (instruction == "/") {
var b = this.currStack.pop();
var a = this.currStack.pop();
this.currStack.push(a / b);
} else if (instruction == "%") {
var b = this.currStack.pop();
var a = this.currStack.pop();
this.currStack.push(a % b);
} else if (instruction == "_") {
this.currStack.push(-this.currStack.pop());
} else if (instruction == "I") {
var value = this.currStack.pop();
if (value < 0) {
this.currStack.push(Math.ceil(value));
} else {
this.currStack.push(Math.floor(value));
}
} else if (instruction == ">") {
var b = this.currStack.pop();
var a = this.currStack.pop();
this.currStack.push(+(a > b));
} else if (instruction == "<") {
var b = this.currStack.pop();
var a = this.currStack.pop();
this.currStack.push(+(a < b));
} else if (instruction == "=") {
var b = this.currStack.pop();
var a = this.currStack.pop();
this.currStack.push(+(a == b));
} else if (instruction == "!") {
this.currStack.push(+ !this.currStack.pop());
} else if (instruction == "?") {
this.currStack.push(Math.random());
} else if (instruction == "n") {
output = "" + this.currStack.pop();
} else if (instruction == "o") {
output = String.fromCharCode(this.currStack.pop());
} else if (instruction == "r") {
var input = this.program.io.getNumber();
this.currStack.push(input);
} else if (instruction == "i") {
var input = this.program.io.getChar();
this.currStack.push(input);
} else if (instruction == "(") {
this.length -= Math.floor(this.currStack.pop());
this.length = Math.max(this.length, 0);
} else if (instruction == ")") {
this.length += Math.floor(this.currStack.pop());
this.length = Math.min(this.length, this.code.length);
} else if (instruction == "w") {
this.wait = this.currStack.pop();
}
// Any unrecognized character is a no-op
if (this.ip >= this.length) {
// We've swallowed the IP, so this snake dies
this.alive = false;
this.program.snakesLiving--;
} else {
// Increment IP and loop if appropriate
this.ip = (this.ip + 1) % this.length;
}
return output;
}
Snake.prototype.getHighlightedCode = function() {
var result = "";
for (var i = 0; i < this.code.length; i++) {
if (i == this.length) {
result += '<span class="swallowedCode">';
}
if (i == this.ip) {
if (this.wait > 0) {
result += '<span class="nextActiveToken">';
} else {
result += '<span class="activeToken">';
}
result += escapeEntities(this.code.charAt(i)) + '</span>';
} else {
result += escapeEntities(this.code.charAt(i));
}
}
if (this.length < this.code.length) {
result += '</span>';
}
return result;
}
// Define Program class
function Program(source, speed, io) {
this.sharedStack = new Stack();
this.snakes = source.split(/\r?\n/).map(function(snakeCode) {
var snake = new Snake(snakeCode);
snake.program = this;
snake.sharedStack = this.sharedStack;
return snake;
}.bind(this));
this.snakesLiving = this.snakes.length;
this.io = io;
this.speed = speed || 10;
this.halting = false;
}
Program.prototype.run = function() {
this.step();
if (this.snakesLiving) {
this.timeout = window.setTimeout(this.run.bind(this), 1000 / this.speed);
}
}
Program.prototype.step = function() {
for (var s = 0; s < this.snakes.length; s++) {
var output = this.snakes[s].step();
if (output) {
this.io.print(output);
}
}
this.io.displaySource(this.snakes.map(function (snake) {
return snake.getHighlightedCode();
}).join("<br>"));
}
Program.prototype.halt = function() {
window.clearTimeout(this.timeout);
}
var ioFunctions = {
print: function (item) {
var stdout = document.getElementById('stdout');
stdout.value += "" + item;
},
getChar: function () {
if (inputData) {
var inputChar = inputData[0];
inputData = inputData.slice(1);
result = inputChar.charCodeAt(0);
} else {
result = -1;
}
var stdinDisplay = document.getElementById('stdin-display');
stdinDisplay.innerHTML = escapeEntities(inputData);
return result;
},
getNumber: function () {
while (inputData && (inputData[0] < "0" || "9" < inputData[0])) {
inputData = inputData.slice(1);
}
if (inputData) {
var inputNumber = inputData.match(/\d+/)[0];
inputData = inputData.slice(inputNumber.length);
result = +inputNumber;
} else {
result = -1;
}
var stdinDisplay = document.getElementById('stdin-display');
stdinDisplay.innerHTML = escapeEntities(inputData);
return result;
},
displaySource: function (formattedCode) {
var sourceDisplay = document.getElementById('source-display');
sourceDisplay.innerHTML = formattedCode;
}
};
var program = null;
var inputData = null;
function showEditor() {
var source = document.getElementById('source'),
sourceDisplayWrapper = document.getElementById('source-display-wrapper'),
stdin = document.getElementById('stdin'),
stdinDisplayWrapper = document.getElementById('stdin-display-wrapper');
source.style.display = "block";
stdin.style.display = "block";
sourceDisplayWrapper.style.display = "none";
stdinDisplayWrapper.style.display = "none";
source.focus();
}
function hideEditor() {
var source = document.getElementById('source'),
sourceDisplay = document.getElementById('source-display'),
sourceDisplayWrapper = document.getElementById('source-display-wrapper'),
stdin = document.getElementById('stdin'),
stdinDisplay = document.getElementById('stdin-display'),
stdinDisplayWrapper = document.getElementById('stdin-display-wrapper');
source.style.display = "none";
stdin.style.display = "none";
sourceDisplayWrapper.style.display = "block";
stdinDisplayWrapper.style.display = "block";
var sourceHeight = getComputedStyle(source).height,
stdinHeight = getComputedStyle(stdin).height;
sourceDisplayWrapper.style.minHeight = sourceHeight;
sourceDisplayWrapper.style.maxHeight = sourceHeight;
stdinDisplayWrapper.style.minHeight = stdinHeight;
stdinDisplayWrapper.style.maxHeight = stdinHeight;
sourceDisplay.textContent = source.value;
stdinDisplay.textContent = stdin.value;
}
function escapeEntities(input) {
return input.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
function resetProgram() {
var stdout = document.getElementById('stdout');
stdout.value = null;
if (program !== null) {
program.halt();
}
program = null;
inputData = null;
showEditor();
}
function initProgram() {
var source = document.getElementById('source'),
stepsPerSecond = document.getElementById('steps-per-second'),
stdin = document.getElementById('stdin');
program = new Program(source.value, +stepsPerSecond.innerHTML, ioFunctions);
hideEditor();
inputData = stdin.value;
}
function runBtnClick() {
if (program === null || program.snakesLiving == 0) {
resetProgram();
initProgram();
} else {
program.halt();
var stepsPerSecond = document.getElementById('steps-per-second');
program.speed = +stepsPerSecond.innerHTML;
}
program.run();
}
function stepBtnClick() {
if (program === null) {
initProgram();
} else {
program.halt();
}
program.step();
}
function sourceDisplayClick() {
resetProgram();
}
.container {
width: 100%;
}
.so-box {
font-family:'Helvetica Neue', Arial, sans-serif;
font-weight: bold;
color: #fff;
text-align: center;
padding: .3em .7em;
font-size: 1em;
line-height: 1.1;
border: 1px solid #c47b07;
-webkit-box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
text-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
background: #f88912;
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
}
.control {
display: inline-block;
border-radius: 6px;
float: left;
margin-right: 25px;
cursor: pointer;
}
.option {
padding: 10px 20px;
margin-right: 25px;
float: left;
}
h1 {
text-align: center;
font-family: Georgia, 'Times New Roman', serif;
}
a {
text-decoration: none;
}
input, textarea {
box-sizing: border-box;
}
textarea {
display: block;
white-space: pre;
overflow: auto;
height: 50px;
width: 100%;
max-width: 100%;
min-height: 25px;
}
span[contenteditable] {
padding: 2px 6px;
background: #cc7801;
color: #fff;
}
#stdout-container, #stdin-container {
height: auto;
padding: 6px 0;
}
#reset {
float: right;
}
#source-display-wrapper , #stdin-display-wrapper{
display: none;
width: 100%;
height: 100%;
overflow: auto;
border: 1px solid black;
box-sizing: border-box;
}
#source-display , #stdin-display{
font-family: monospace;
white-space: pre;
padding: 2px;
}
.activeToken {
background: #f93;
}
.nextActiveToken {
background: #bbb;
}
.swallowedCode{
color: #999;
}
.clearfix:after {
content:".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {
display: inline-block;
}
* html .clearfix {
height: 1%;
}
.clearfix {
display: block;
}
<!--
Designed and written 2015 by D. Loscutoff
Much of the HTML and CSS was taken from this Befunge interpreter by Ingo Bürk: http://codegolf.stackexchange.com/a/40331/16766
-->
<div class="container">
<textarea id="source" placeholder="Enter your program here" wrap="off">i.0<5*(o32o</textarea>
<div id="source-display-wrapper" onclick="sourceDisplayClick()"><div id="source-display"></div></div></div><div id="stdin-container" class="container">
<textarea id="stdin" placeholder="Input" wrap="off">Input text...</textarea>
<div id="stdin-display-wrapper" onclick="stdinDisplayClick()"><div id="stdin-display"></div></div></div><div id="controls-container" class="container clearfix"><input type="button" id="run" class="control so-box" value="Run" onclick="runBtnClick()" /><input type="button" id="pause" class="control so-box" value="Pause" onclick="program.halt()" /><input type="button" id="step" class="control so-box" value="Step" onclick="stepBtnClick()" /><input type="button" id="reset" class="control so-box" value="Reset" onclick="resetProgram()" /></div><div id="stdout-container" class="container"><textarea id="stdout" placeholder="Output" wrap="off" readonly></textarea></div><div id="options-container" class="container"><div class="option so-box">Steps per Second:
<span id="steps-per-second" contenteditable>20</span></div></div>
0815, 26 bytes
<:20:~>}: :!~$#:
:{~$>^: :
Tcl, 44 bytes
puts [regsub -all {(.)} [gets stdin] {1円 }]
-
\$\begingroup\$ You're byte count is wrong! It is 43! \$\endgroup\$sergiol– sergiol2017年01月12日 03:22:43 +00:00Commented Jan 12, 2017 at 3:22
Lua, 31 Bytes
Saved 1 byte thanks to @LeakyNun
A simple chained substitution, replace each character by itself and a space.
s=(...):gsub(".","%1 ")print(s)
An other solution with the same byte count:
print((...):gsub(".","%1 "),'')
-
\$\begingroup\$ @manatwork I don't see how I did to miss this! Print automatically had a tab between each of it's parameters, totally forgot that point. Thanks for the comment! \$\endgroup\$Katenkyo– Katenkyo2016年04月04日 07:41:26 +00:00Commented Apr 4, 2016 at 7:41
-
\$\begingroup\$ Can you change
arg[1]to...? \$\endgroup\$Leaky Nun– Leaky Nun2016年07月01日 07:57:08 +00:00Commented Jul 1, 2016 at 7:57
Swift 2.2, (削除) 48 (削除ここまで) 46 bytes
for c in i.characters{print(c,terminator:" ")}
Hey, at least it's shorter than Java... Saved 2 chars by using a for in loop instead of a forEach closure.
Batch, 87 bytes
@set/pt=&set w=
:l
@if not "%t%"=="" set w=%w%%t:~,1% &set t=%t:~1%&goto l
@echo(%w%
Conveniently taking input from STDIN is the same length as taking it from program arguments (set t=%*). The space between the % and & on the third line is significant, of course. The ( after the echo simply prevents it from erroring out if the input is empty.
TI-84 BASIC, 44 bytes
Pretty straightforward approach:
Input Str1
sub(Str1,1,1
For(A,2,length(Str1
Ans+" "+sub(Str1,A,1
End
Ans+"
The only size optimizations I could come up with were:
- leaving off closing quotes and parentheses at the end of lines
- using
Ansinstead of a string variable (theAnstoken is 1 byte,StrNare 2, and assignment toAnsis implicit) - printing the result by placing it on the last line of the program (no
Dispcommand needed)
This program wouldn't work correctly for an empty input string, since it unconditionally stores the first character of Str1 in Ans. (This is necessary because concatenation with an empty string raises an error!) Fortunately, the Input command doesn't accept an empty string in the first place.
The size calculation was informed by TI-Basic Developer's token size charts.
OIL, (削除) 52 (削除ここまで) 49 bytes noncompeting
Note: The second line contains a space character.
5
12
0
23
10
24
3
7
10
4
24
4
1
8
11
1
11
6
6
5
I explode the input string, then print each character and then a space.
Lua, 57 bytes
t=""io.read():gsub(".",function(c)t=t..c.." "end)print(t)
SmileBASIC 3, 44 bytes
INPUT A$FOR I=0TO LEN(A$)-1?A$[I]+" ";
NEXT
This doesn't leave a new line after the output, so from direct you might get
?HELLO
H E L L O OK
but hopefully it's not an issue.
-
\$\begingroup\$ You can make the loop shorter by doing:
WHILE""<A$?SHIFT(A$);" ";:WEND\$\endgroup\$12Me21– 12Me212017年04月21日 14:17:40 +00:00Commented Apr 21, 2017 at 14:17
Vim, (削除) 12 (削除ここまで) 10 keystrokes
:s/./& /g<CR>
1 byte longer:
qqa <Esc>l@qq@q
-2 bytes thanks to @KritixiLithos
-
\$\begingroup\$ You don't need to delete the last space and
:s/./0円 /g<CR>$xcan become just:s/./& /g<CR>at 10 bytes. \$\endgroup\$user41805– user418052017年06月05日 04:05:26 +00:00Commented Jun 5, 2017 at 4:05