Answers:
s/./$&x(s!$&!$&!g>1)/eg
用法:
> perl -pe 's/./$&x(s!$&!$&!g>1)/eg'
helloworld
llool
起初,我以为可以使用负前瞻性和负后瞻性来做到这一点,但事实证明,负前瞻性必须具有固定的长度。所以我改为使用嵌套的正则表达式。感谢暴民的$&
小费。
-CDS
选项的perl
$1
,$&
您可能会丢失一对括号。
:;{.;?);>?)},
GolfScript不是前20名之一,而是没有GolfScript的代码高尔夫球...(自己运行)
先前版本:(运行脚本)
1/:;{;\-,;,(<},
:;
?您是在故意混淆新手,不是吗?;)
)
-这样会使它变得面带笑容:)
。不幸的是,我什至找不到消除数字1的方法。(GolfScript新手注意:您可以用(或其他任何字母或数字-或脚本中未使用的任何字符)替换;
代码中的x
任何数字。在这种特殊情况下,;
它只是一个变量名-并没有“弹出并丢弃”的含义。在GolfScript中,几乎所有标记都是变量,并且使用预定义符号是使脚本更不易被外人阅读的好方法;-)。
:a{]a.@--,(},
gets.chars{|c|$><<c if$_.count(c)>1}
s
并$_
用于第二次出现,则可以节省4个字符(然后可以使用之前的空格)。
我没想到它会那么短。
2.7: a=raw_input();print filter(lambda x:a.count(x)>1,a)
3.0: a=input();print''.join(i for i in a if a.count(x)>1)
raw_input()
:将输入存储为字符串(input()
= eval(raw_input())
)
(Python 3.0:input()
已变成raw_input()
)
filter(lambda x:a.count(x)>1,a)
:a
如果a
一次或多次发现(a.count(x)>1
),则过滤掉其中的所有字符。
input()
而不是raw_input()
。虽然你必须添加一个字符一个右括号,因为print
在Python 3的功能
''.join(...)
当然这不是TIOBE列表的一部分,但是很有趣(-:
<<<$s sed 's/./&\n/g'|head -c -1|sort|uniq -c|sed -n 's/^ *1 (.*)/\1/p'|tr -d '\n'|sed 's:^:s/[:; s:$:]//g\n:'|sed -f - <(<<<$s)
取消高尔夫版本:
s=helloworld
<<< $s sed 's/./&\n/g' \
| head -c -1 \
| sort \
| uniq -c \
| sed -n 's/^ *1 (.*)/\1/p' \
| tr -d '\n' \
| sed 's:^:s/[:; s:$:]//g\n:' \
| sed -f - <(<<< $s)
第一个sed将输入转换为每行一个字符。第二个sed查找仅出现一次的字符。第三sed编写一个sed脚本,该脚本删除唯一字符。最后的sed执行生成的脚本。
⊇.oḅlⁿ1∧
功能提交。从技术上讲是非竞争性的,因为该问题对允许使用哪种语言进行了限制(但是,其他几个答案已经忽略了该限制)。
⊇.oḅlⁿ1∧
⊇ Find {the longest possible} subset of the input
o {for which after} sorting it,
ḅ and dividing the sorted input into blocks of identical elements,
lⁿ1 the length of a resulting block is never 1
. ∧ Output the subset in question.
@x=split//,<>;$s{$_}++for@x;for(@x){print if($s{$_}>1)}
从stdin读取。
使用ExtLib的ExtString.String
open ExtString.String
let f s=let g c=fold_left(fun a d->a+Obj.magic(d=c))0 s in replace_chars(fun c->if g c=1 then""else of_char c)s
非高尔夫球版
open ExtString.String
let f s =
let g c =
fold_left
(fun a c' -> a + Obj.magic (c' = c))
0
s
in replace_chars
(fun c ->
if g c = 1
then ""
else of_char c)
s
该函数g
返回字符串s中c的出现次数。函数f
根据出现的次数用空字符串或包含该字符的字符串替换所有字符。编辑:我通过滥用bool的内部表示将代码缩短了6个字符:-)
哦,在TIOBE索引上ocaml为0 ;-)
s->{for(char c=96;++c<123;s=s.matches(".*"+c+".*"+c+".*")?s:s.replace(c+"",""));return s;}
说明:
s->{ // Method with String as both parameter and return-type
for(char c=96;++c<123; // Loop over the lowercase alphabet
s=s.matches(".*"+c+".*"+c+".*")?
// If the String contains the character more than once
s // Keep the String as is
: // Else (only contains it once):
s.replace(c+"","")); // Remove this character from the String
return s;} // Return the modified String
"$args"-replace"[^$($args|% t*y|group|?{$_.Count-1}|% n*)]"
少打高尔夫球:
$repeatedСhars=$args|% toCharArray|group|?{$_.Count-1}|% name
"$args"-replace"[^$repeatedСhars]"
注意:$repeatedChars
是一个数组。默认情况下,Powershell在将数组转换为字符串的同时,使用空格字符连接数组元素。因此,正则表达式包含空格(在本示例中为[^l o]
)。空格不会影响结果,因为输入字符串仅包含字母。
s=>[...s].filter(c=>s.match(c+'.*'+c)).join``
p=>[...p].map((v,i,a)=>a.filter(f=>f==v).length).reduce((a,c,i)=>c>1?a+=p[i]:a,[])
.join``
代替.join("")
。
码
implode('',array_intersect(str_split($text),array_flip(array_filter(array_count_values(str_split($text)),function($x){return $x>=2;}))));
普通码
$text = 'helloworld';
$filter = array_filter(array_count_values(str_split($text)), function($x){return $x>=2;});
$output = implode('',array_intersect(str_split($text),array_flip($filter)));
echo $output;
string s;cin>>s;string w{s}; auto l=remove_if(begin(s),end(s),[&w](auto&s){return count(begin(w),end(w),s)==1;});s.erase(l,end(s));cout<<s;
松开
#include <algorithm>
#include <string>
#include <iostream>
int main() {
using namespace std;
string s;
cin >> s;
const string w{s};
auto l = remove_if(begin(s), end(s), [&w](auto& s) {
return count(begin(w), end(w), s) == 1;
});
s.erase(l, end(s));
cout << s;
return 0;
}