独特的筛子


17

给定一个整数列表,请创建一个布尔掩码,以便可以使用真实索引来过滤列表中的不同值。只要为对应于相同值的每组索引选择一个索引,选择哪个索引才是真正的索引就没有关系。

输入将是适合您的语言的格式的非负整数的非空列表,而输出将是遵循上述规范的布尔值列表。您可以在输出列表中使用自己的真实值和虚假值定义。

在下面的示例中,我定义1为真实且0为虚假。

[5, 4, 7, 1]  Input
[1, 1, 1, 1]  Output
              Select only the values with with true indicies in the sieve
[5  4  7  1]  Contains zero duplicate values

[5, 9, 7, 5, 6, 0, 5]
[0, 1, 1, 1, 1, 1, 0]
[   9, 7, 5, 6, 0   ]

测试用例

当存在时or,表示存在多个有效输出。如果有一个尾随省略号...or,就意味着不是所有的可能的输出的被列出。

[0] = [1]

[55] = [1]

[32, 44] = [1, 1]

[0, 0] = [1, 0] or [0, 1]

[9001, 9001, 9001] = [1, 0 , 0] or [0, 1, 0] or [0, 0, 1]

[5, 4, 7, 1] = [1, 1, 1, 1]

[1, 2, 3, 4, 3, 5] = [1, 1, 1, 1, 0, 1] or
                     [1, 1, 0, 1, 1, 1]

[5, 9, 7, 5, 6, 0, 5] = [1, 1, 1, 0, 1, 1, 0] or
                        [0, 1, 1, 1, 1, 1, 0] or
                        [0, 1, 1, 0, 1, 1, 1]

[0, 8, 6, 6, 3, 8, 7, 2] = [1, 1, 1, 0, 1, 0, 1, 1] or
                           [1, 0, 0, 1, 1, 1, 1, 1] or
                           [1, 0, 1, 0, 1, 1, 1, 1] or
                           [1, 1, 0, 1, 1, 0, 1, 1]

[45, 47, 47, 45, 24, 24, 24, 8, 47, 41, 47, 88]
= [1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1] or ...

[154, 993, 420, 154, 154, 689, 172, 417, 790, 175, 790, 790, 154, 172, 175, 175, 420, 417, 154, 175, 172, 175, 172, 993, 689, 993, 993, 790]
= [1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] or ...

规则

  • 这是因此最短的解决方案是成功的。
  • 允许内置函数!
  • 您可以在输出列表中使用自己的真实值和虚假值定义。如果您选择这样做,请说明您的定义。
  • 输入将是非负整数的非空列表。
  • 您可以选择只输出其中一个筛子,还是输出多个甚至全部筛子。只要每个筛子都有效,它就会被接受。

2
对于[0, 8, 6, 6, 3, 8, 7, 2],应[1, 0, 0, 1, 1, 1, 1, 1]添加到有效输出列表中吗?
Atlasologist '16

您对真实值和虚假值的定义是否引用了该语言,还是我们可以自由选择?他们必须保持一致吗?
丹尼斯

@atlasologist感谢您捕捉错字
英里

@Dennis您可以自由定义自己的布尔值,它们不必与您选择的语言相同,但是必须与自己的定义保持一致。在我的示例中,如果不是true和false分别为1和0,我可以将false值设置为false,将非负值(零或正值)设置为true。
英里

好的,谢谢您的澄清。一致的是,我的意思是必须有一个真实值,或者必须有多个。
丹尼斯

Answers:


11

MATL,7 6 4字节

@Luis节省了1个字节@Dennis节省了
2个字节

&=Rs

我们定义1为真实,所有其他值定义为虚假

在线尝试

所有测试用例

说明

    % Implicitly grab input array
&=  % 2D array of equality comparisons
R   % Get the upper triangular portion
s   % Sum down the columns
    % Implicitly display the result

少1字节:&=Rs1=
Luis Mendo

@LuisMendo哈,我实际上只是在玩这种方法!
Suever '16

2
OP澄清了此挑战中真实与虚假的含义。如果将1定义为真,将其他所有定义为虚假,则可以删除l=
丹尼斯

很好用。目的是避免必须添加过滤步骤,现在您已经匹配了Dennis的Jelly解决方案。
2016年

9

果冻,4 字节

ĠṪ€Ṭ

支持最后一次出现。在线尝试!验证所有测试用例

怎么运行的

ĠṪ€Ṭ  Main link. Argument: A (array)

Ġ     Group; paritition the indices of A according to their corresponding values.
 Ṫ€   Tail each; select the last index of each group.
   Ṭ  Untruth; generate a Boolean array with 1's at the specified indices.

难道这不会砍掉零吗?
Leaky Nun

2
末尾不能为零,因为我们选择每个唯一整数的最后一次出现。
丹尼斯

那很聪明。
Leaky Nun

8

Python 3,47 35 39 36字节

lambda n:[n.pop(0)in n for x in n*1]

从列表中弹出第一项,检查它是否在列表中的其他位置,然后插入TrueFalse插入新列表。

对于此函数,False表示一个不同的值,True否则为(True=0False=1

感谢Dennis的大量字节

原始的47个字节:

lambda n:[(1,0)[n.pop()in n]for x in[1]*len(n)]

尝试一下


lambda n:[1-(n.pop()in n)for x in n*1]保存一些字节。
丹尼斯

3
OP已阐明,真实值实际上不一定是真实值,因此lambda n:[n.pop()in n for x in n*1]也是如此。
丹尼斯

新版本让我迷失了一下,直到我意识到它使用了xnor的否定值,例如xnor的真实性和虚假性。
英里

您需要这样做.pop(0)或面罩反过来了。
xnor

那不是xnor的意思。.pop()首先处理最后一个元素,因此的顺序相反。
丹尼斯

7

Pyth,6个字节

.eqxQb

输出布尔值(TrueFalse)的列表。检查输入中的每个元素的索引是否等于该值首次出现的索引。换句话说,这是在检查每个元素是否是第一次出现。

在pythonic伪代码中:

.e      enumerated_map(lambda b,k:    # maps with b as value and k as index
  q       equal(
   xQb      Q.index(b),
            k),                       # implicit lambda variable
          Q)                          # implicit argument to map

在这里测试。


6

J,2个字节

~:

这就是提出这一挑战的想法的来源。内建~:函数Nub-Sieve在J中调用,并创建一个布尔列表,该列表执行质询中描述的操作。在这里,1代表true0代表false


6

05AB1E,8个字节

码:

v¹ykN>Qˆ

说明:

y         # For each in the array
 ¹yk      # Get the index of that element in the array
    N>Q   # And see if it's equal to the index
       ˆ  # Add to the global array and implicitly output

使用CP-1252编码。在线尝试!


4

APL,6个字节

⍳∘⍴∊⍳⍨

尝试一下

说明:

   ⍳⍨  For each character in the string, get the index of its first occurrence
⍳∘⍴     Make a list 1 .. length of input
  ∊    Check if each index is present

4

C#,63个字节

int[]l(List<int> i)=>i.Select((m,n)=>i.IndexOf(m)-n).ToArray();

我还可以使它返回1或0,然后使rby使参数和返回类型相同,从而允许我自己使之成为lambda表达式?

一些指导将不胜感激

相同类型的代码

    public static List<int>l(List<int>i){
        return i.Select((m,n)=>i.IndexOf(m)==n?1:0).ToList();
    }

如果您将true定义为0,将falsy定义为其他任何值,则可以将== n替换为-n并返回int []
raggy

多数民众赞成在一个不错的主意
downrep_nation

还使用表达式函数功能int [] l(List <int> i)=> i.Select((m,n)=> i.IndexOf(m)-n).ToArray();
2015年

哦,天哪,从现在开始,这可以节省很多字节。非常感谢您
downrep_nation

您可以在.NetFiddle上提供示例吗?
aloisdg说恢复莫妮卡

3

Python,35个字节

f=lambda l:l and[l.pop(0)in l]+f(l)

用途True为falsy价值,False为truthy值。标记每个元素的最后出现。

仅当第一个元素没有出现在其余元素中时,才选择它,然后只要它是非空的,就递归到列表的其余部分。在l.pop(0)提取第一元件,同时也将其移除。


3

视网膜,23字节

(\d+)((?!.* \1\b))?
$#2

输入是一个以空格分隔的列表。(实际上,[1, 2, 3]只要每个数字前面都有一个空格(第一个数字除外),其他类似格式也可以使用。)

在线尝试!(一次处理多个换行分隔的测试用例。)

我们只是简单地将每个元素转换为输入,0如果稍后在输入中存在另一个副本,则转换为1否则。


2

PowerShell v2 +,40个字节

$a=@();$args[0]|%{(1,0)[$_-in$a];$a+=$_}

创建一个空数组$a。然后,我们通过输入清单,$args[0]并将其输送到循环中|%{...}。每次迭代时,我们都会根据当前元素是否在其中来选择伪三元数10从伪三元数中选择$a。这些选择留在管道上。然后,将当前元素添加到数组中$a。收集管道元素,并隐式输出为数组。

例:

(此处输出的是换行符,因为这.ToString()是数组的默认值)

PS C:\Tools\Scripts\golfing> .\distinct-sieves.ps1 1,2,3,4,1,3,5,7
1
1
1
1
0
0
1
1

1

JavaScript(ES6),31个字节

f=a=>a.map((e,i)=>i-a.indexOf(e))

零是真实的,其他数字是虚假的。


1

Mathematica,53岁 31个字节

感谢Miles给我一个节省22个字节的想法。

s[[;;x++]]~FreeQ~#&/@(x=0;s=#)&

如何使用MapIndexed以前的子列表?MapIndexed[s[[;;#-1&@@#2]]~FreeQ~#&,s=#]&占用41个字节。
2016年

@miles哦,这要好得多了(我对此有所改善;))
Martin Ender

噢,这是缩短MapIndexed这种情况的好方法,您甚至不必提取或减少索引
英里

1

Perl 5

push@o,map{$b=pop@a;(grep{/^$b$/}@a)?1:0}(1..~~@a);

1. ..提供标量上下文,因此您不需要~~。2. grep返回“真/假”,因此您不需要?1:0。3. grep/.../,@a比短grep{/.../}@a。4.您不需要决赛;。5.您不需要在圆括号内1..@a。6.您不会显示输入来自哪里或输出将去哪里:请参阅meta.codegolf.stackexchange.com/q/2447
msh210 '16

1

Java,96个字节

void s(int[]a){for(int i=0,j,n=a.length,b=1;i<n;a[i++]=b,b=1)for(j=i+1;j<n;)b=a[i]==a[j++]?0:b;}

就地修改数组。支持最后一次出现。

真值是,1而假值是0

验证所有测试用例

取消高尔夫:

void sieve(int[]a){
    int n = a.length;
    for(int i=0;i<n;i++){
        int b = 1;
        for(int j=i+1;j<n;j++){
            if(a[i] == a[j]){
                b = 0;
            }
        }
        a[i] = b;
    }
}

1

其实11个位元组

;╗ñ`i@╜í=`M

在线尝试!

说明:

;╗ñ`i@╜í=`M
;╗           save a copy of input in reg0
  ñ          enumerate
   `i@╜í=`M  for each (index, value) pair:
    i@         flatten, swap
      ╜í       first index in input of value
        =      compare equality


1

C ++,242字节

公认的过分解决方案,因为它适用于任何订购类型的任何标准容器:

#include<algorithm>
#include<list>
#include<set>
template<class T>auto f(T a){using V=typename T::value_type;std::set<V>s;std::list<bool>r;std::transform(a.begin(),a.end(),std::back_inserter(r),[&](V m){return s.insert(m).second;});return r;}

取消高尔夫:

(并进一步概括)

template<class T>
auto f(T a)
{
    using std::begin;
    using std::end;
    using V=typename T::value_type;
    std::set<V>s;
    std::list<bool>r;
    std::transform(begin(a),end(a),std::back_inserter(r),[&](V m){return s.insert(m).second;});
    return r;
}

测试套件:

int test(const std::list<bool>& expected, const auto& x) { return f(x) != expected; }
#include<array>
#include<chrono>
#include<forward_list>
#include<initializer_list>
#include<string>
#include<vector>
using namespace std::literals::chrono_literals;
int main()
{
    return 0
        + test({},            std::vector<short>{})
        + test({1},           std::array<int,1>{})
        + test({1},           std::vector<char>{55})
        + test({true,true},   std::vector<unsigned>{32,44})
        + test({1,0},         std::list<std::string>{"zero", "zero"})
        + test({1,0,0},       std::vector<long>{9001,9001,9001})
        + test({1,1,1,1},     std::array<char,4>{5,4,7,1})
        + test({1,1,1,1,0,1}, std::initializer_list<std::string>{"one","two","three","four","three","five"})
        + test({1,0,1,0,0},   std::forward_list<std::chrono::seconds>{60s, 1min, 3600s, 60min, 1h});
}

1

TSQL 52字节

DECLARE @ TABLE(i int identity, v int)
INSERT @ values(1),(2),(3),(4),(3),(5)

SELECT i/max(i)over(partition by v)FROM @ ORDER BY i

小提琴


1

PHP,66 62 39字节

  • 接受所有原子值(布尔值,整数,浮点数,字符串),
    但值取值为false(假,0,“”)和数字字符串(“ 1”等于1)除外
  • 标志第一次出现

新版本(程序,37 + 2字节)
击败了Java和(现在再次成为)C#。现在甚至几乎击败了Python。快乐。

<?foreach($a as$v)$u[$v]=print$u[$v]|0;
  • PHP> = 5.4时为+6,函数为+ 16-3
  • 版画,未定的列表0(真)和1(假)
    插入!print反转
  • 用法:设置register_globals=Onshort_open_tags=On然后error_reporting=0php.inifor中php-cgi
    调用php-cgi -f <filename> a[]=<value1> a[]=<value2> ...;echo""
  • 对于PHP> = 5.4:$a$_GET[a](+6)替换,设置short_open_tags=Onerror_reporting=0
  • 或替换$aarray_slice($argv,1)(+19),删除<?(-2)
    并致电php -d error_reporting=0 -r '<code>' <value1> <value2> ...;echo""

旧版本(功能,62字节)

function f($a){foreach($a as$v)$u[$v]=1|$m[]=$u[$v];return$m;}
  • 返回falsetrue和truefalse的数组;(输出中作为空字符串或1
    插入!$m[]=以反转
  • 具有55个字节的合格函数还有另一种方法。

测试(在旧版本上)

function out($a){if(!is_array($a))return$a;$r=[];foreach($a as$v)$r[]=out($v);return'['.join(',',$r).']';}
function test($x,$e,$y){static $h='<table border=1><tr><th>input</th><th>output</th><th>expected</th><th>ok?</th></tr>';echo"$h<tr><td>",out($x),'</td><td>',out($y),'</td><td>',out($e),'</td><td>',(strcmp(out($y),out($e))?'N':'Y'),"</td></tr>";$h='';}
$samples=[
    [0],[1],    [55],[1],    [32,44],[1,1],    [9001,9001,9001],[1,false,false],
    [5,4,7,1],[1,1,1,1],    [1,2,3,4,3,5],[1,1,1,1,false,1],
    [5,9,7,5,6,0,5],[1,1,1,false,1,1,false],    [0,8,6,6,3,8,7,2],[1,1,1,false,1,false,1,1],
    [45,47,47,45,24,24,24,8,47,41,47,88],[1,1,'','',1,'','',1,'',1,'',1],
    [154,993,420,154,154,689,172,417,790,175,790,790,154,172,175,
        175,420,417,154,175,172,175,172,993,689, 993,993,790],
        array_merge([1,1,1,false,false,1,1,1,1,1],array_fill(0,18,false))
];
for($i=count($samples);$i--;--$i)for($j=count($samples[$i]);$j--;)$samples[$i][$j]=!$samples[$i][$j];
while($samples)
{
    $a=array_shift($samples);
    $em=array_shift($samples);
    test($a,$em,$ym=s($a));
    $eu=[];foreach($em as$i=>$f)if($f)$eu[]=$a[$i];
    $yu=[];foreach($ym as$i=>$f)if($f)$yu[]=$a[$i];
#   sort($eu); sort($yu);
    test('unique values',$eu,$yu);
}
echo '</table>';

1

Haskell,29 27字节

f a=[elem x t|x:t<-tails a]

用途False为真,True假值:

λ> let f a=[elem x t|x:t<-tails a] in f [5, 9, 7, 5, 6, 0, 5]
[True,False,False,True,False,False,False]

您可能必须import Data.List使用,tails但是 tryhaskell.org仍按原样运行代码。


无需外部括号。\a->[...]是适当的功能。如有疑问,请为其命名:f a=[...]
nimi

没有括号,@ nimi无法调用它。但是给它起个名字很有效,非常感谢。
尼斯(Ness Ness)

我不太喜欢省略的理由import Data.List一种。这是一个非常滑的坡度,因为您可以在GHCi配置中放置任何数量的导入(甚至定义!)。b。将tryhaskell.org视为Haskell语言的权威实现,但实际上并不是一种。(再说一次,如果我创建自己的try-Haskell-online环境,高尔夫球手可能想要的所有导入和定义都附带这个环境?那真的还是“ Haskell”吗?)
Lynn

曾经有人告诉我,如果有任何平台按原样运行代码,那么该代码是可以接受的。我不知道确切的规则是什么,我按所告诉的去做。我想是的,如果您的网站24/7全天候可用,并且运行标准的Haskell,为什么不这样做。但是您对第一个是正确的,我删除了它。谢谢。
Will Ness

1

Perl 5 + Perligata,343字节

315个字节,外加28个字节 -MLingua::Romana::Perligata

用作perl -MLingua::Romana::Perligata foo.pl; 输入(从stdin)和输出(到stdout)是用下划线分隔的十进制整数字符串。在Perligata 0.6版草莓5.20.2上测试;我不知道它是否适用于Perligata 0.50版。

huic vestibulo perlegementum da.qis _ scindementa da.dum qis fac sic
ao qis decumulamentum da.ao aum tum nullum addementum da.meo io.meo ro.per ium in qis fac sic
si ium tum aum aequalitas fac sic ro I da cis cis
ro nullum tum non rum addementum da.capita bis rum cis
per in bis fac sic hoc tum _ egresso scribe cis

显然,这很明显。如果它不是,与运行它-MLingua::Romana::Perligata=converte,而不是-MLingua::Romana::Perligataperl意志,而不是运行该脚本,输出翻译成普通的Perl:

 $_ = Lingua::Romana::Perligata::getline (*STDIN );
 @q = split ( '_');
while (@q) { $a = pop (@q );
 $a =  ($a + 0);
my $i ;
my $r ;
for $i (@q) {if ( ($i eq $a)) { $r = 1}
}
;
 $r =  (0 +  ! ($r));
unshift (@b, $r)}
;
for $_ (@b) {print (STDOUT $_, '_')}

要进行逐令牌分析,请使用-MLingua::Romana::Perligata=discribe


高尔夫注意事项:

  • 无证件(但不足为奇),之后不需要空格 .
  • (也毫不奇怪,)scinde不需要第二个参数,并使用hoc
  • 我不得不使用,ao aum tum nullum addementum da因为我无法morde上班。
  • 同样,我用per ium in qis... ro I da因为我无法vanne上班而使用。
  • 相反的huic vestibulo perlegementum da,我试过-pMLingua::Romana::Perligata,但它不能去工作,无论是。

只是为了踢(尽管整个答案只是为了踢):

  • 清理完毕后Huic vestibulo perlegementum da. Qis lacunam scindementa da. Dum qis fac sic ao qis decumulamentum da. Ao aum tum nullum addementum da. Meo io. Meo ro. Per ium in qis fac sic si ium tum aum aequalitas fac sic ro I da cis cis. Ro nullum tum non rum addementum da. Capita bis rum cis. Per in bis fac sic hoc tum lacunam egresso scribe cis.,Google Translate给出This court perlegementum grant. QIS gap scindementa grant. While QIS QIS decumulamentum do so ao da. Ao sum and no addementum grant. My io. My ro. Through ium in QIS do so if the sum ium equality do so ro 1 from cis. Ro was not any rum addementum grant. The heads of the bis side. Write, do so as soon as he at that time that in the gap by the Kish was taken.
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.