打印不可见的文字


38

给定一个字符串作为输入,输出等于该字符串长度的多个空格字符(0x0A和0x20)。

例如,给定字符串,Hello, World!您的代码将只需要输出13个空格字符,而无需输出其他任何字符。这些可以是空格和换行符的任意组合。

您的代码不应输出任何其他尾随换行符或空格。

测试用例:

     Input      -> Amount of whitespace to output
"Hello, World!" -> 13
"Hi"            -> 2
"   Don't
Forget about
Existing
Whitespace!   " -> 45
""              -> 0
"             " -> 13
"
"               -> 1

得分:

这是因此最少的字节数获胜!


1
我不明白“ 0x0A”的意思。应该在哪里输出?应该保留它,以便“a␠b␊c”变成“ becomes”吗?
manatwork

1
@manatwork 0x0A0x20分别是换行符和空格字符的十六进制值
Skidsdev

1
“输出多个空白字符(0x0A和0x20)” –这些换行符应在输出中的什么位置?
manatwork

3
These can be any mix of spaces and newlines您的输出可以是空格和换行符的任何混合,您可以像其他所有人一样仅输出空格,也可以只输出换行符。由您决定
Skidsdev

1
得到它了。谢谢。
manatwork

Answers:


137

空格311 150 77 68 65 46 41 38个字节

-3个字节归功于Kevin Cruijssen
-27个字节归功于Ephphatha


  
   	 	 
 
  
 	
	 			
	  
	
  
 


在线尝试!

可见格式

'\n  \n   \t \t \n \n  \n \t\n\t \t\t\t\n\t  \n\t\n  \n \n\n'

说明(s =空格,t =制表符,n =换行)

nssn     # label(NULL) - loop start
ssststsn # push 10 in the stack -> [10]
sns      # duplicate the top of the stack -> [10, 10]
sns      # duplicate the top of the stack -> [10, 10, 10]
tnts     # read a single char from input, pop the stack and store at that address -> [10, 10] [10:<char that was read>]
ttt      # pop the stack and put the value at that adress on stack -> [10,<char>] [10:<char>]
ntssn    # jump to label(0) - since label(0) is not defined, the interpreter jumps to the end of the program - if the top of the stack (char) is 0 -> [10] [10:<char>]
tnss     # pop the top of the stack and print as ascii -> [] [10:<char>]
nsnn     # go back to the label(NULL)

25
假设这确实有效,那么这绝对会赢得我对最具创意的答案的投票
Skidsdev

24
等待答案在哪里?它也是看不见的吗?
暴民埃里克(Erik the Outgolfer)'17年

16
这是什么黑魔法。您的代码甚至不存在!-1
Christopher

28
@Christopher更像WHITEspace MAGIC
Rod

12
我知道有人会用空格程序回答这个问题
Draco18s

59

Japt,1个字节

ç

在线尝试!


22
japt对此是否有内置功能?该死的…
Skidsdev

22
@Mayube好了,它有一个内置函数,可以用另一个字符串替换字符串中的所有字符,默认替换是空格;)
Tom

4
非常好!对于运行该程序的用户,可以将-Q标志添加到输入中,以在输出两边加上引号。TIO
Oliver

38

Haskell,7个字节

(>>" ")

在线尝试!用法:(>>" ") "Hello, world!"

给定两个列表(字符串是Haskell中的字符列表), >>操作员将重复第二个列表,与第一个列表具有元素的次数相同。设置" "为第二个参数意味着我们将连接输入字符串长的任意多个空格。


备用(相同字节数):

(' '<$)

在线尝试!用法:(' '<$) "Hello, world!"

给定一些值和一个列表,<$操作员用给定值替换列表中的每个值。因此5 <$ "abc"导致[5,5,5],和' ' <$ "abc"" "

(<$)' '如果您想在我的代码中找到更多海洋生物,则该函数可以等效地表示为 。


18
它就像一条可爱的小无鳍鱼
泰勒·斯科特



17

视网膜,3 4字节

S\`.

旧版本无效,因为Retina会打印尾随的feed。

。
 

(第二行包含一个空格)。


2
视网膜TIO非常易于使用。这是您的答案
数字创伤

2
不幸的是,Retina默认情况下会打印尾随换行符。您需要先添加\`以避免这种情况。然后,它使用起来会更短一些S\`.,它将每个字符替换为一个换行符(因为它将输入拆分为每个字符)。
Martin Ender

@MartinEnder Ahhh不知道这是视网膜还是TIO。感谢您在其中保存字节的帮助!
TheLethalCoder



11

C#,28个 24字节

s=>"".PadLeft(s.Length);

使用string构造函数的旧版本为28个字节:

s=>new string(' ',s.Length);

3
想要做的完全一样
LiefdeWen

1
@StefanDelport当我在身边时,可以快速使用C#:)有Linq的方法可以完成相同的工作,但是它们的使用时间更长……
TheLethalCoder


9

Mathematica,21个字节

StringReplace[_->" "]

1
如果允许字符列表输入,则可以为#/._->" "&。可悲的是,输入是一个字符串,而Characters []使它比您的解决方案长一个字节:(
CalculatorFeline

1
这不需要a #和a &吗?例如StringReplace[#,_->" "]&
伊恩·米勒

3
@IanMiller不是数学中的10.4或11 reference.wolfram.com/language/ref/StringReplace.html
alephalpha

2
喔好吧。我只有10.3。也许是时候升级了……
伊恩·米勒

8

JavaScript ES6,22个字节

a=>a.replace(/./g," ")

f=a=>a.replace(/./g," ");

var test = f("Hello, World!");
console.log(test, test.length);


3
呵呵,我以为“该死,它必须s=>s.replace(/[^]/g," ")比其他解决方案长一个字节”。在我看来,输出中不允许换行符:P
ETHproductions

8

C,31个字节

f(char*c){puts(""),*c++&&f(c);}

1
这与您的其他C答案有何不同?显然,这是较短的,但您是否应该仅编辑另一个?它应该是两种解决方案的一个答案吗?
塔斯州

4
@Tas首先,我认为从某种意义上说,尽管它虽然更短,但还是不如另一个好,因为它实际上并没有按原样编译。它只是一个函数,因此您需要围绕它编写一些主例程。但是,它更短的和其他人似乎刚刚发布功能。显然,这是两个非常不同的解决方案。一个不是对另一种的提炼,因此对我来说应该是两个不同的答案。但是,我是这个社区的新手。是否只有一位用户发布一个答案的共识?如果是这样,我下次会做。
sigvaldm '17

逗号真的应该是逗号而不是分号吗?
奥斯卡·斯科格

1
@OskarSkog好,在这种情况下,没关系,因为没有lhs

1
@OskarSkog是的,应该是逗号。就像@cat所说的,在这种情况下并不重要,但是我选择了逗号作为变体:)逗号运算符计算两个表达式(例如i++, j++,在for循环中)并返回最右边的一个。一个重要的细节是,递归必须以某种方式停止。&&如果lhs为假,则不评估其为rhs。*c++当它指向字符串的空终止时,计算结果为false。
sigvaldm '17


7

Excel VBA,17 15字节

匿名VBE立即窗口功能,它从单元格获取输入,并将输入[A1]长度的空间输出到VBE立即窗口

?Spc([Len(A1)])

7

05AB1E,3个字节

vð?

在线尝试!

v   # For each character...
 ð? #    Output a space without a newline

其他3个字节的解决方案(其中大多数感谢Magic Octopus UrnKevin Cruijssen

v¶? # For each character print a newline (without adding a newline)
võ, # For each character print the empty string with a newline
gð× # Get the length, concatenate that many copies of space
g¶× # Get the length, concatenate that many copies of newline
Sð: # Split, push a space, replace each char in input with a space
ðs∍ # Push ' ', swap, make the string of spaces as long as the input was
võJ # For each char, push a space and ''.join(stack)
v¶J # For each char, push a newline and ''.join(stack)
€ðJ # For each char, push a space. Then ''.join(stack)
ۦJ # For each char, push a newline. Then ''.join(stack)

1
Other solution: gð×, the rest I came up with were above 3 like: õ‚.B¤
Magic Octopus Urn

2
Another fun one: Sð:
Magic Octopus Urn

1
More fun: ðs∍
Magic Octopus Urn

Some more alternative 3-byters: võJ/v¶J; €ðJ/€¶J. And since a sequence of characters as I/O is allowed by default when strings I/O is asked, some 2-byte versions are possible: €ð/€¶/εð/ε¶ and ð:/¶:. Although since this is a pretty old challenge and all other answers use actual strings, I could understand if you kept it as string I/O.
Kevin Cruijssen



6

C, 45 bytes

Using main. Compile with gcc, ignore warnings.

main(c,v)char**v;{while(*(v[1]++))puts("");}

Usage:

$./a.out "Hello, World!"

1
Any reason why you can't put char**v in main(c,v)?
CalculatorFeline

@CalculatorFeline At least GCC 6.3.1 compiling simply with gcc main.c doesn't seem to allow mixing ANSI function definition with K&R function definition, so main(c,char**v) won't compile. I either have to do main(int c,char**v) or main(c,v)char**v; of which the latter is 3 bytes shorter. You wouldn't by chance know any flag or something which allows mixing these styles?
sigvaldm

3
No, you can't mix 'em. There's no flag that allows that. K&R style is long obsolete, used only for code golfing and obfuscation purposes.
Cody Gray

And I'm guessing removing char**v entirely doesn't compile either.
CalculatorFeline

@CalculatorFeline If you omit char** entirely the compiler will interpret it as int. If I'm not mistaken you get an error trying to dereference an int and even if you didn't the program wouldn't do what you expected it to do since an int consumes several chars and therefore you never get a NULL value.
sigvaldm



5

><>, 7 bytes

i0(?;ao

The program is a loop

i         //Push a character from the input onto the stack
 0        //Add a 0 to the stack
  (       //Pop the top two values of the stack, and push a 1 if the second is less than the first (In this case, the input has all been read), else push a 0
   ?      //Pop the top of the stack. If the value is a 0, skip the next instruction
    ;     // Terminate the program
     a    // Add a newline to the stack
      o   // Pop the top character of the stack and print it


5

Hexagony, 12 11 bytes

-1 byte thanks to Martin Ender

,<.;.M@.>~8

Try it online!

Here is the expanded hex:

  , < . 
 ; . M @
. > ~ 8 .
 . . . .
  . . .

While there is input, this code runs:

,        # Get input
 <       # Turn right (SE) if we didn't get EOF
  M8     # Set the memory edge to 778 which is 10 (mod 256)
    ;    # Print as a character (newline)
     >   # Redirect East
      ~  # Multiply by -1. This makes the pointer go to the top when it runs off the edge
       8 # Effectively a no-op.

When EOF is reached:

,    # Get input
 <   # Turn left (NE)
  8  # Effectively a no-op
   @ # End program

You can print a linefeed in three bytes with M8; (which gives 778 = 10 (mod 256)). That should allow you to move the ~ where the ; is right now, saving a byte.
Martin Ender


5

Python 2, 25 bytes

exec'print;'*len(input())

-2 bytes thanks to Loovjo
-2 bytes in the invalid code thanks to totallyhuman :p
-3 bytes


1
You can remove the parens after exec since it's a keyword in Python 2
Loovjo

1
@Loovjo Oh right, Python 2. Thanks!
HyperNeutrino

I know this is old and stuff but exec'print;'*len(input()) works.
totallyhuman

1
@totallyhuman oh true, thanks :P
HyperNeutrino

1
@TheMatt it's probably not in the problem specs but it's one of the default acceptable input methods. Try looking on meta, I don't want to go looking for it right now
HyperNeutrino


4

Cubix, 6 bytes

Wahoo a 6 byter!

wi?@oS

Cubified

  w
i ? @ o
  S
  • i gets input
  • ? test top of stack
    • if negative (EOI) redirect onto w lane change which umps to the @ halt
    • if 0 (null) halt this shouldn't be hit
    • if positive Sow push space to the stack, output and change lane onto i

Try it online!


1
Sweet, it's not too often a Cubix program is this short :-)
ETHproductions

4

C, 32 bytes

Try Online modifying characters into spaces

f(char*t){(*t=*t?32:0)&&f(t+1);}

C, 37 bytes

Try Online Left-padding the end-of-string with its length

f(char*t){printf("%*c",strlen(t),0);}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.