Code Golf:字符串中的重复字符删除
挑战:按字符数计算最短的代码,检测并删除字符串中的重复字符。删除包括重复字符的所有实例(因此,如果找到 3 个 n,则所有三个都必须删除),并且需要保留原始字符顺序。
示例输入 1:
nbHHkRvrXbvkn示例输出 1:
RRX
示例输入 2:
nbHHkRbvnrXbvkn示例输出 2:
RRX
(第二个示例删除出现三次的字母;某些解决方案未能解决此问题)
(这是基于 我的另一个问题,我需要在 C# 中以最快的方式执行此操作,但我认为它可以在跨语言中实现良好的 Code Golf。)
The challenge: The shortest code, by character count, that detects and removes duplicate characters in a String. Removal includes ALL instances of the duplicated character (so if you find 3 n's, all three have to go), and original character order needs to be preserved.
Example Input 1:
nbHHkRvrXbvknExample Output 1:
RrX
Example Input 2:
nbHHkRbvnrXbvknExample Output 2:
RrX
(the second example removes letters that occur three times; some solutions have failed to account for this)
(This is based on my other question where I needed the fastest way to do this in C#, but I think it makes good Code Golf across languages.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
LabVIEW 7.1
ONE 字符,即程序框图中的蓝色常量“1”。
我发誓,输入是复制粘贴的;-)
http://i25.tinypic.com/hvc4mp.png
< a href="http://i26.tinypic.com/5pnas.png">http://i26.tinypic.com/5pnas.png
LabVIEW 7.1
ONE character and that is the blue constant '1' in the block diagram.
I swear, the input was copy and paste ;-)
http://i25.tinypic.com/hvc4mp.png
http://i26.tinypic.com/5pnas.png
Perl
Perl 的 21 个字符,31 个要调用的字符,总共 36 次击键(计算移位和最后的回车):
Perl
21 characters of perl, 31 to invoke, 36 total keystrokes (counting shift and final return):
Ruby —
61 53 51 563561 个字符,标尺说。 (给了我另一个代码高尔夫的想法......)
... 35 by Nakilon
Ruby —
61 53 51 563561 chars, the ruler says. (Gives me an idea for another code golf...)
... 35 by Nakilon
APL
23 个字符:
我是 APL 新手(昨天才学的),所以要友善 - 这当然不是最有效的方法。我很遗憾我没有远远击败 Perl。
话又说回来,也许它说明了一些问题,对于新手来说,在 APL 中解决这个问题的最自然的方式仍然比迄今为止任何语言的任何其他解决方案都更简洁。
APL
23 characters:
I'm an APL newbie (learned it yesterday), so be kind -- this is certainly not the most efficient way to do it. I'm ashamed I didn't beat Perl by very much.
Then again, maybe it says something when the most natural way for a newbie to solve this problem in APL was still more concise than any other solution in any language so far.
Python:
这是一个完整的工作程序,从控制台读取和写入。单行版本可以直接从命令行使用
Python:
This is a complete working program, reading from and writing to the console. The one-liner version can be directly used from the command line
J (
1612 个字符)例:
只需加括号即可默认执行。如果放入动词中,实际代码本身将是 14 个字符。
当然有更聪明的方法可以做到这一点。
编辑:所讨论的更聪明的方式:
12 个字符,如果设置在动词中则只有 10 个字符。我仍然讨厌这样的事实:它要遍历列表两次,一次是为了计数(#/.),另一次是为了返回唯一值(nub 或 ~.),但即使是 nubcount(“misc”库中的标准动词)也会执行两次。
J (
1612 characters)Example:
It only needs the parenthesis to be executed tacitly. If put in a verb, the actual code itself would be 14 characters.
There certainly are smarter ways to do this.
EDIT: The smarter way in question:
12 characters, only 10 if set in a verb. I still hate the fact that it's going through the list twice, once to count (#/.) and another to return uniques (nub or ~.), but even nubcount, a standard verb in the 'misc' library does it twice.
Haskell
在 Haskell 中肯定有更短的方法来做到这一点,但是:
忽略 let,因为它只在 GHCi 中的函数声明中需要,我们有 hy=[x|x<-y,(<2).length$ filter(==x)y],为 37 个字符(这与
"".join(c for c in s if s.count(c)<2) 的当前“核心”Python 相关)
,而且它实际上是相同的代码)。如果你想用它制作一个完整的程序,
或者我们可以这样删除一个字符:
它在标准输入的所有上运行,而不是逐行运行,但这在我看来是可以接受的。
Haskell
There's surely shorter ways to do this in Haskell, but:
Ignoring the let, since it's only required for function declarations in GHCi, we have
h y=[x|x<-y,(<2).length$filter(==x)y]
, which is 37 characters (this ties the current "core" Python of"".join(c for c in s if s.count(c)<2)
, and it's virtually the same code anyway).If you want to make a whole program out of it,
Or we can knock off one character this way:
It operates on all of stdin, not line-by-line, but that seems acceptable IMO.
C89(106 个字符)
这个使用了与我原来的答案完全不同的方法。有趣的是,写完后,然后查看另一个答案,我看到方法非常相似。感谢咖啡馆在我之前提出了这个方法。
在一行中,它是 58+48 = 106 字节。
C89(173 个字符)
这是我原来的答案。正如评论中所说,它工作得不太好......
在两行中,它是 17+1+78+77 = 173 字节。
C89 (106 characters)
This one uses a completely different method than my original answer. Interestingly, after writing it and then looking at another answer, I saw the methods were very similar. Credits to caf for coming up with this method before me.
On one line, it's 58+48 = 106 bytes.
C89 (173 characters)
This was my original answer. As said in the comments, it doesn't work too well...
On two lines, it's 17+1+78+77 = 173 bytes.
C#
65 个字符:
67 个重新分配的字符:
C#
65 Characters:
67 Characters with reassignment:
C#
71 个字符
C#
71 characters
PHP(136 个字符)
在一行中,它是 5+1+65+65 = 136 字节。使用 PHP 5.3,您可以节省一些字节,使函数匿名,但我现在无法测试。也许类似于:
5+1+66+59 = 131 字节。
PHP (136 characters)
On one line, it's 5+1+65+65 = 136 bytes. Using PHP 5.3 you could save a few bytes making the function anonymous, but I can't test that now. Perhaps something like:
That's 5+1+66+59 = 131 bytes.
另一个 APL 解决方案
作为动态函数(18 个字符)
行,假设输入位于变量 x(16 个字符)中:
another APL solution
As a dynamic function (18 charachters)
line assuming that input is in variable x (16 characters):
VB.NET
诚然,VB 不是尝试保存字符的最佳语言,但该行有 98 个字符。
VB.NET
Granted, VB is not the optimal language to try to save characters, but the line comes out to 98 characters.
PowerShell
61 个字符。其中
$s="nbHHkRvrXbvkn"
和$a
是结果。功能齐全的参数化脚本:
PowerShell
61 characters. Where
$s="nbHHkRvrXbvkn"
and$a
is the result.Fully functioning parameterized script:
C:83
899399101个字符#include
-ing
(花费 18 个字符)使得gets< 的返回类型/code> 被解释为
int
并砍掉一半的地址位)。。
(这个类似的 82-chars 版本通过命令行获取输入
:)
C: 83
899399101characters#include
-ing<stdio.h>
(costs 18 chars) making the return type ofgets
being interpreted as anint
and chopping off half of the address bits)..
(and this similar 82-chars version takes input via the command line:
)
Golfscript(符号) - 15
Golfscript(sym) - 15
Haskell
(只是删除了 Mark Rushakoff 的几个字符,我宁愿将其作为对他的评论发布)
这是更好的 Haskell 习惯用法,但对于非 Haskell 人来说可能比这更难理解:
编辑为 hiena 和其他人添加解释:
我假设您理解 Mark 的版本,所以我只介绍更改。 Mark 的表达式:
过滤
y
以获取== x
的元素列表,找到该列表的长度并确保它小于 2。 (事实上,它的长度必须为一,但==1
比<2
长)我的版本:执行相同的过滤器,然后放入结果列表到列表中作为唯一元素。现在箭头(看起来像集合包含!)表示“依次对于 RHS 列表的每个元素,调用该元素
[z]
”。[z]
是包含单个元素z
的列表,因此元素“filter(==x)y
”只能称为“[z]
" 如果它只包含一个元素。否则它会被丢弃并且永远不会用作z
的值。因此,z
(在列表理解中返回于|
的左侧)正是x
的组成部分filter
返回长度为 1 的列表。这是我的第二个版本,我的第一个版本返回
x
而不是z
- 因为它们无论如何都是相同的 - 并将z
重命名为>_
这是 Haskell 符号,表示“这个值不会被使用,所以我不会通过给它一个名字来使我的代码复杂化”。Haskell
(just knocking a few characters off Mark Rushakoff's effort, I'd rather it was posted as a comment on his)
which is better Haskell idiom but maybe harder to follow for non-Haskellers than this:
Edit to add an explanation for hiena and others:
I'll assume you understand Mark's version, so I'll just cover the change. Mark's expression:
filters
y
to get the list of elements that== x
, finds the length of that list and makes sure it's less than two. (in fact it must be length one, but==1
is longer than<2
) My version:does the same filter, then puts the resulting list into a list as the only element. Now the arrow (meant to look like set inclusion!) says "for every element of the RHS list in turn, call that element
[z]
".[z]
is the list containing the single elementz
, so the element "filter(==x)y
" can only be called "[z]
" if it contains exactly one element. Otherwise it gets discarded and is never used as a value ofz
. So thez
's (which are returned on the left of the|
in the list comprehension) are exactly thex
's that make thefilter
return a list of length one.That was my second version, my first version returns
x
instead ofz
- because they're the same anyway - and renamesz
to_
which is the Haskell symbol for "this value isn't going to be used so I'm not going to complicate my code by giving it a name".Javascript 1.8
或类似的 python 示例:
Javascript 1.8
or alternately- similar to the python example:
TCL
123 个字符。也许可以缩短它,但这对我来说已经足够了。
TCL
123 chars. It might be possible to get it shorter, but this is good enough for me.
C
完整的 C 程序,141 字节(包括换行符)。
C
Full program in C, 141 bytes (counting newlines).
Scala
54 个字符仅用于方法主体,66 个字符用于(静态类型)方法声明:
Scala
54 chars for the method body only, 66 with (statically typed) method declaration:
Ruby
63 个字符。
Ruby
63 chars.
VB.NET / LINQ
完整工作语句 96 个字符
Dim p=New String((From c In"nbHHkRvrXbvkn"Group c By c Into i=CountWhere i=1 Select c).ToArray)
完成工作语句,带有原始字符串和关闭 VB 特定的“漂亮列表(代码重新格式化”),长度为 96 个字符,非工作语句,没有原始字符串,长度为 84 个字符。
(请在回答之前确保您的代码可以工作。谢谢。)
VB.NET / LINQ
96 characters for complete working statement
Dim p=New String((From c In"nbHHkRvrXbvkn"Group c By c Into i=Count Where i=1 Select c).ToArray)
Complete working statement, with original string and the VB Specific "Pretty listing (reformatting of code" turned off, at 96 characters, non-working statement without original string at 84 characters.
(Please make sure your code works before answering. Thank you.)
C
(第一个版本:112 个字符;第二个版本:107 个字符)
那是
因为 getchar() 返回 int 而 putchar 接受 int,因此可以“安全”删除 #include。
如果没有包含,则未定义 EOF,因此我使用 -1 代替(并获得了一个字符)。
该程序仅适用于少于 100000 个字符的输入!
版本 2,感谢 strager
107 个字符
C
(1st version: 112 characters; 2nd version: 107 characters)
That's
Because getchar() returns int and putchar accepts int, the #include can 'safely' be removed.
Without the include, EOF is not defined, so I used -1 instead (and gained a char).
This program only works as intended for inputs with less than 100000 characters!
Version 2, with thanks to strager
107 characters
Javascript 1.6
比之前发布的 Javascript 1.8 解决方案更短(71 个字符与 85 个字符)
Javascript 1.6
Shorter than the previously posted Javascript 1.8 solution (71 chars vs 85)
汇编器
使用 WinXP DOS 框 (cmd.exe) 进行测试:
汇编为 53 字节。读取标准输入并将结果写入标准输出,例如:
Assembler
Tested with WinXP DOS box (cmd.exe):
Assembles to 53 bytes. Reads standard input and writes results to standard output, eg:
PHP
118 个字符的实际代码(加上 PHP 块标记的 6 个字符):
PHP
118 characters actual code (plus 6 characters for the PHP block tag):
C#(53 个字符)
其中 s 是您的输入字符串:
或重新赋值后的 59:
C# (53 Characters)
Where s is your input string:
Or 59 with re-assignment:
Haskell Pointfree
整个程序有 97 个字符,但真正的内容只有 23 个字符。剩下的就是导入并将函数带入 IO monad 中。在加载了模块的 ghci 中,它只是
采用更荒谬的 pointfree 风格(毫无意义的风格?):
虽然函数本身有 26 个字符,但它有点长。
Haskell Pointfree
The whole program is 97 characters, but the real meat is just 23 characters. The rest is just imports and bringing the function into the IO monad. In ghci with the modules loaded it's just
In even more ridiculous pointfree style (pointless style?):
It's a bit longer though at 26 chars for the function itself.
Shell/Coreutils,37 个字符
Shell/Coreutils, 37 Characters