映射作弊者!


10

提交所有作业后,将创建一个字典,将学生编号映射到其文件的哈希。

该字典,哈希表或映射(无论您的语言叫什么)将如下所示:

{100: "aabb", 104: "43a", 52: "00ab", 430: "aabb", 332: "43a"}

关键是学生编号,而值是哈希。

我们的任务是挑选作弊者!作弊者是具有相同哈希值的作弊者。

给定输入后{100: "aabb", 104: "43a", 52: "00ab", 430: "aabb", 332: "43a"},该函数应返回(或打印)以下文本:

100 has identical files to 430

104 has identical files to 332

请注意,没有提到哈希唯一的文件。

另外,顺序在这里很重要

{100: "aabb", 202: "aabb", 303: "ab", 404: "aabb"} 应该返回(打印)以下文本:

100 has identical files to 202,404

打印以下任何内容均不正确

202 has identical files to 100,404

100 has identical files to 404, 202

您应该按照它在字典中的显示方式进行打印。在某些语言中,字典是随机的,因此在这种特殊情况下,您可以更改输入法,以便可以有序地进行输入。

更多示例:

{} # prints nothing

{100: "ab", 303: "cd"} # prints nothing again

{100: "ab", 303: "cd", 404: "ab"}

100 has identical files to 404

{303: "abc", 304: "dd", 305: "abc", 405: "dd", 606: "abc"}

303 has identical files to 305,606

304 has identical files to 405

最短的代码胜出!


“您应该按照它在字典中的显示方式进行打印”-我不确定这是什么意思。否则我喜欢挑战。
朱塞佩

3
我还建议在发布到主站点之前使用沙盒吗?在发布前对问题进行修订总是很有帮助的,而不是在主要问题上得到一百万条评论以澄清问题:-)
朱塞佩

1
在发现多组作弊者的情况下,各组之间是否有要求的顺序?例如,在最后一个测试用例中,可以在“ 303 has ...”之前打印“ 304 has ...”吗?
卡米尔·德拉卡里

2
我们可以输出303 has identical files to [305, 606]而不是303 has identical files to 305,606吗?
凯文·克鲁伊森

1
在不存在字典,地图或哈希图类型的语言中,是否允许使用元组列表(或等效列表)?

Answers:


2

JavaScript(Babel节点),113字节

将输入作为[key, value]格式为数组的数组。Go go小工具double flatMap!

o=>o.flatMap(([x,h],i)=>(a=o.flatMap(([y,H],j)=>j>i&H==h?(o[j]=[,j],[y]):[]))+a?x+' has identical files to '+a:a)

在线尝试!


JavaScript(Babel节点),114字节

将输入作为本机JS对象。

o=>Object.keys(o).flatMap((x,i,a)=>(a=a.filter(y=>i--<0&o[y]==o[x]&&(o[y]=y)))+a?x+' has identical files to '+a:a)

在线尝试!


1
非常好!很大的文件,但是我再一次没想到这个问题会像其他问题一样容易。做得好!我会对此进行更多研究flatMap
K Split X

@KSplitX flatMap尚未得到广泛支持。我敢肯定有更短的方法,但是已经很晚了,我再也想不起来了。:p
Arnauld


1

视网膜0.8.2,71字节

+m`((:.+)$(¶|.)+?)^(.+)\2$
,$4$1
:.*

G`,
%1`,
 has identical files to 

在线尝试!在单独的行上进行输入,但链接包含测试套件,可为您拆分示例。说明:

+

重复此比赛,直到无法进行替换为止。

m`((:.+)$(¶|.)+?)^(.+)\2$
,$4$1

查找成对的匹配哈希值,然后用逗号分隔符将第二个匹配项的键附加到第一个匹配项的键之后。

:.*

删除所有哈希。

G`,

仅使用逗号分隔行。

%1`,
 has identical files to 

用所需的文本(包括尾随空格)替换每行的第一个逗号。


1

[R 145个 132 129 126 124字节

function(m,`!`=names)for(e in !(t=table(m))[t>1])cat(el(n<-!m[m==e]),'has identical files to',paste(n[-1],collapse=','),'
')

在线尝试!

它以命名矢量作为输入(名称是键)

  • -2个字节,感谢Giuseppe

如果", "在多次重复的情况下允许使用分隔符(逗号后面有空格),则可以使用此代码并节省10个字节:

R,114字节

function(m,`!`=names)for(e in !(t=table(m))[t>1])cat(el(n<-!m[m==e]),'has identical files to',toString(n[-1]),'
')

在线尝试!


124字节,尽管我的直觉告诉我采用另一种方法可能会产生115范围内的某些数据……
Giuseppe

0

05AB1E,34 个字节

Σθ}.γθ}vyg1›iy€нć“ÿ€°Ê¼‡œ€„ “?',ý,

在线尝试验证所有测试用例

说明:

Σθ}                   # Sort the (implicit) input by the string
.γθ}                  # Then group it by the string
v                     # Loop `y` over each grouped inner list
 yg1i                #  If the group contains more than 1 key-value pairs:
      y€н             #   Only leave the keys
      ć               #   Pop and push the head and rest of the list separately
                      #   (with the head being at the top of the stack now)
       “ÿ€°Ê¼‡œ€„    #   Compressed string "ÿ has identical files to "
                      #   where the "ÿ" is automatically replaced with the top of the stack
                   ?  #   Print it (without trailing newline)
       ',ý           '#   Join the remaining numbers by a comma
          ,           #   And output it as well (with trailing newline)

见我这个05AB1E答案(部分如何使用字典?理解为什么“ÿ€°Ê¼‡œ€„ “"ÿ has identical files to "


0

红宝石98 96字节

->h{h.group_by{|k,v|v}.map{|k,v|x,*y=v.to_h.keys;p"#{x} has identical files to #{y*?,}"if y[0]}}

在线尝试!

将输入作为Ruby Hash,通过打印返回。



0

C#(Visual C#交互式编译器),130字节

a=>a.GroupBy(x=>x.Value,x=>x.Key).Where(x=>x.Count()>1).Select(x=>x.First()+" has identical files to "+String.Join(",",x.Skip(1)))

在线尝试!

关于这个问题的奇怪之处在于,示例以JSON格式作为键/值对给出,这通常意味着它们是无序的。但是,在这种情况下,顺序很重要。因此,我使用元组列表作为输入,使用字符串列表作为输出。

// a is a list of tuples
// (student #, hash)
a=>a
  // group by hash
  // grouped items are the student #'s
  .GroupBy(x=>x.Value,x=>x.Key)
  // remove single student groups
  .Where(x=>x.Count()>1)
  // format the output strings
  .Select(x=>x.First()+
    " has identical files to "+
    String.Join(",",x.Skip(1)))


0

Japt,34个字节

üÌl>1 ®mgîÎ+` •s ÅÁÈól fÅC ‘ `+ZÅ

在线尝试!

现在,它在行​​的顺序上有点不一致,但是在一行中它可以正确输出。如果输出的行需要按特定顺序排列,则将需要几个字节。输入只是[id, hash]成对的数组

说明:

üÌ                                    :Group by hash
  l>1                                 :Remove the ones that are unique
      ®mgà                            :Get just the Ids
          ®                           :Generate a string for each hash:
           Î                          : The first Id with that hash
            +` •s ÅÁÈól fÅC ‘ `       : Plus " has identical files to " compressed
                               +ZÅ    : Plus the remaining Ids
                                      : Implicitly comma delimited

0

Perl 6的115个 110 103字节

-2个字节,感谢Jo King

{unique map {.[0]~" has identical files to "~join ',',.skip},grep *>1,.map:{.grep(*{*}eq$^p{*})>>.key}}

在线尝试!

由于哈希值是无序的,因此使用成对列表。一个由两个元素组成的列表的列表将节省一些字节,但是看起来很简单。返回行列表。

如果结果中的行顺序无关紧要,则为 95 88字节

*.classify(*{*}){*}>>.key.grep(*>1).map:{.[0]~" has identical files to "~join ',',.skip}

在线尝试!


.[1..*].skip
约翰·金
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.