验证拓扑


25

挑战

给定T有限集的子集S={1,2,3,...,n},确定是否T拓扑

说明

某个集合的 P(S)S是的所有子集的集合S。一些例子:

S = {}, P(S) = {{}}
S = {1}, P(S) = {{}, {1}}
S = {1,2}, P(S) = {{}, {1}, {2}, {1,2}}
S = {1,2,3}, P(S) = {{}, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}}

拓扑 T上的集S的一个子集P(S)与以下属性:

  • {}TST
  • 如果AB在,T那么他们的交集A ∩ B
  • 如果AB在,T那么他们的联合也是A ∪ B*

*这个定义不是很正确,但是对于有限集却是正确的,对于这个挑战的目的就足够了。实际的公理也将允许无限的并集,但这与有限的情况无关。

细节

  • 您可以假定S = {1,2,...,n}(或另选S = {0,1,...,n})where n是出现在中的最大整数T
  • 输入格式灵活:您可以使用字符串,列表列表或列表集,也可以使用语言可以处理的任何类似格式。您也可以使用集(例如S = {0,1,...,n}更方便)。
  • 输出必须是trueey或falsey。
  • 允许您接受n(或替代n+1n-1)作为附加输入。
  • 如果使用有序列表,则可以假定集合中的数字已排序。您还可以假定列表具有特定顺序(例如,字典顺序)。
  • 当我们表示集合时,可以假定它们的列表表示中没有两个条目相等。

例子

拓扑结构

{{}}  over {}
{{},{1}} over {1}
P(S) over S (see in the explanation)
{{},{1},{1,2}} over {1,2}
{{},{1},{2,3},{1,2,3}} over {1,2,3}
{{1}, {1,2,3}, {1,4,5,6}, {1,2,3,4,5,6}, {}, {2,3}, {4,5,6}, {2,3,4,5,6}}
{{}, {1}, {2,3}, {2}, {4,5,6}, {5,6}, {5}, {2,5,6}, {2,5}, {1,5}, {1,2,3,4,5,6}, {1,2,3}, {1,2}, {1,4,5,6}, {1,5,6}, {1,2,5,6}, {2,3,4,5,6}, {2,3,5,6}, {2,3,5}, {1,2,3,5}, {2,4,5,6}, {1,2,5}, {1,2,3,5,6}, {1,2,4,5,6}}
{{}, {1}, {1,2}, {1,2,3}, {1,2,3,4}, {1,2,3,4,5}, {1,2,3,4,5,6}, {1,2,3,4,5,6,7}, {1,2,3,4,5,6,7,8}, {1,2,3,4,5,6,7,8,9}}
{{}, {1}, {1,2,3}, {1,2,3,4,5}, {1,2,3,4,5,6,7}, {1,2,3,4,5,6,7,8,9}}

非拓扑

{{1}} because {} is not contained
{{},{2}} because {1,2} is not contained
{{},{1,2},{2,3}} because the union {1,2,3} is not contained
{{},{1},{1,2},{2,3},{1,2,3}} because the intersection of {1,2} and {2,3} is not contained
{{},{1},{2},{3},{1,2},{2,3},{1,2,3}} because the union of {1} and {3} is not contained
{{}, {1}, {2,3}, {2}, {4,5,6}, {5,6}, {5}, {2,5,6}, {2,5}, {1,5}, {1,2,3,4,5,6}, {1,2,3}, {1,2}, {1,4,5,6}, {1,5,6}, {1,2,5,6}, {2,3,4,5,6}, {2,3,5,6}, {2,3,5}, {2,4,5,6}, {1,2,5}, {1,2,3,5,6}, {1,2,4,5,6}} because {1,2,3,5} is missing
{{}, {1}, {2}, {1,2,3}, {1,2,3,4,5}, {1,2,3,4,5,6,7}, {1,2,3,4,5,6,7,8,9}} because {1,2} is missing 

2
这个问题似乎有很多答案会在输入{{},{2}}上出现,因为它们没有显式检查S是否在集合中,而对于该输入,S被隐式假定为{1, 2}。这是对规格的有效阅读,还是我缺少什么?
Carmeister '17

@Carmeister抱歉让您感到困惑,是的,您的解释是正确的!
瑕疵

输入是否可以是二进制矩阵,其中每一行是一个集合,每一列是一个元素,并且值指示该元素是否在集合中?
路易斯·门多

是的,我认为这是可以接受的。
瑕疵

因为T是一个集合,所以我认为假设输入中没有子集被重复(即{{}, {1,2}, {1,2}}无效输入)是合理的。您可以肯定地或否定地在挑战中澄清这一点吗?
路易斯·门多

Answers:


7

Python 2中117个 99 97 91字节

n,x=input();sum(set.union(*x))!=n*-~n/2>q
[map(x.index,(i-i,i|j,i&j))for i in x for j in x]

在线尝试!

通过退出代码输出


5

Haskell95 89 74 78字节

import Data.List
t#n=all(`elem`t)$sort<$>[1..n]:[]:([union,intersect]<*>t<*>t)

在线尝试!

说明:

                              ([union,intersect]<*>t<*>t) -- create all unions & intersections 
                    [1..n]:[]:                            -- add the empty list and the full list
             sort<$>                                --sort them all
                                -- (as 'union' does not necessarily produce sorted outputs)
all(`elem`t)$                   -- check whether they are all already contained

[[],[2]]呢 这是一个拓扑,但不超出隐含的设置(“您可以假设...”)。
Christian Sievers

@ChristianSievers已更正!
瑕疵

5

Mathematica,87 73 66 63字节

Outer[{#⋂#2,#⋃#2}&,#,#,1]~Flatten~2⋃{{},Range@#2}==#⋃#&

注意到[T, n]作为输入。

说明

{#⋂#2,#⋃#2}&

返回输入的交集和并集的函数

Outer[ ... ,#,#,1]

将该功能映射到级别1的输入列表上。

... ~Flatten~2

展平结果(该Outer部分返回一堆嵌套List的)。

... ⋃{{},Range@#2}

在扁平的清单和清单之间进行合并{{}, S}。这消除了重复,并增加{}S所得到的名单。

... ==#⋃#

检查上面的列表是否等于输入的排序版本。


4

MATL,38字节

!t~hXAs1>GXBXH2Z^!"@Z}Z|1MZ&hHm]vAGn~+

输入是一个二进制矩阵,其中每一行是一个集合,每一列是一个元素,每个条目表示成员资格。例如,{{},{1},{1,2}}表示为[0 0;1 0;1 1]。使用链接的Octave程序转换为这种格式。

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

说明

!        % Implicit input. Transpose
t~       % Push a negated copy
h        % Horizontally concatenate both matrices
XA       % All: true for columns containing only 1
s        % Sum
1>       % Does it exceed 1? If so, both the empty set and the total
         % set are in the input
G        % Push input again
XB       % Convert each row from binary to decimal. This gives a column
         % vector of numbers that encode each set's contents. Union and
         % intersection will be done as bitwise XOR and AND
XH       % Copy to clipboard H
2Z^!     % Cartesian square transposed: gives all pairs of numbers as
         % columns of a matrix
"        % For each column
  @      %   Push current column
  Z}     %   Split into the two numbers
  Z|     %   Bitwise XOR
  1M     %   Push the two numbers again
  Z&     %   Bitwise AND
  h      %   Concatenate the two results horizontally
  Hm     %   Are they members of the vector of encoded sets? This gives
         %   a row vector with the two results
]        % End
v        % Concatenate all stack contents into a vertical vector
A        % Does it only contain ones? This is the main result: true iff
         % input is a non-empty topology. The empty input gives false,
         % and so it needs to be special cased
G        % Push input again
n~       % Is it empty?
+        % Add thw two results. Implicit display

1
D:您的程序比标题占用更多的空间!
瑕疵

3

Python 2中92个71 122字节

  • 非常感谢@ovs大量减少了19个字节:&以及|set操作的简写形式。
  • 感谢@notjagan 5个字节
  • 感谢@ovs 2个字节:set()asi-i

将集合列表作为输入并返回True / false的Lambda。只需检查是否存在一个空集,并在给定的集列表中存在每个集的并集和交集(两个集迭代为ij)。

lambda x:x.sort()or all(k in x[-1]for k in range(1,max(x[-1])))and all(a in x for i in x for j in x for a in[i-j,i|j,i&j])

在线尝试!



@ovs非常感谢,不知道速记!
Officialaimm

@ovs其实我明确地把从列表项input(),以set()在页脚。
Officialaimm


1
您可以替换set()i-ii^i
ovs '17

2

CJam(23字节)

{[,M2$2m*{_~&\~|$}/]^!}

在线测试套件。这是一个匿名块(函数)。我认为S = {0,1,...,n}; 该块采用一个已排序数组的数组,并n+1作为参数并离开01在堆栈上。在这种情况下{{}},代码和测试框架假定n+1 = 0


2

Pyth,24个 23字节

q@aasm,@Fd{Ssd*QQYJUEyJ

测试套件

该程序将输入作为有序列表的有序列表。内部列表必须按升序排列,并且该列表必须按长度排序,然后按字典顺序排序。我已经确认这是允许的输入格式。数字从0开始,并且N + 1也作为输入。

至于它是如何工作的,我们过滤掉P(S)中没有的任何东西,然后将[]每对的交点与每对的并集的S,相加,重复数据删除,并检查结果是否等于输入。


0

公理,358字节

t(a,s)==(aa:=sort(removeDuplicates(a));ss:=sort(removeDuplicates(s));a:=sort(a);s:=sort(s);a~=aa or s~=ss=>false;a:=map(sort, a);~member?([],a) or ~member?(s,a)=>false;for x in a repeat(for j in x repeat if ~member?(j,s) then return false;for y in a repeat if ~member?(sort(setIntersection(x,y)),a) or ~member?(sort(setUnion(x,y)),a) then return false);true)

脱节和结果:

-- all the List have to be sorted because in list [1,2]~=[2,1]
-- So here "set" means: "List without duplicate, sorted with sort() function"
-- Return true if 
-- 1) a,s are set for above definition
-- 2) a is in P(s) 
-- 3) s,[] are in a
-- 4) for all x and y in a => xUy is in a and x intersect y is in a
t1(a:List List INT, s:List INT):Boolean==
    aa:=sort(removeDuplicates(a));ss:=sort(removeDuplicates(s))
    a :=sort(a);                  s :=sort(s)
    a~=aa          or s~=ss        =>false   -- they are not sets but list with element duplicate
    a:=map(sort, a)                          -- subset of a has to be sorted too
    ~member?([],a) or ~member?(s,a)=>false
    for x in a repeat
       for j in x repeat if ~member?(j,s) then return false 
       for y in a repeat if ~member?(sort(setIntersection(x,y)),a) or ~member?(sort(setUnion(x,y)),a) then return false
    true

(4) -> t([[]], [])
   (4)  true
                                                            Type: Boolean
(5) -> t([[],[1]], [1])
   (5)  true
                                                            Type: Boolean
(6) -> t([[],[1],[2,1]], [1,2])
   (6)  true
                                                            Type: Boolean
(7) -> t([[],[1],[2,3],[1,2,3]], [3,1,2])
   (7)  true
                                                            Type: Boolean
(8) -> t([[1], [1,2,3], [1,4,5,6], [1,2,3,4,5,6], [], [2,3], [4,5,6], [2,3,4,5,6]], [1,2,3,4,5,6])
   (8)  true
                                                            Type: Boolean
(9) -> t([[], [1], [2,3], [2], [4,5,6], [5,6], [5], [2,5,6], [2,5], [1,5], [1,2,3,4,5,6], [1,2,3], [1,2], [1,4,5,6], [1,5,6], [1,2,5,6], [2,3,4,5,6], [2,3,5,6], [2,3,5], [1,2,3,5], [2,4,5,6], [1,2,5], [1,2,3,5,6], [1,2,4,5,6]], [1,2,3,4,5,6])
   (9)  true
                                                            Type: Boolean
(10) -> t([[], [1], [1,2], [1,2,3], [1,2,3,4], [1,2,3,4,5], [1,2,3,4,5,6], [1,2,3,4,5,6,7], [1,2,3,4,5,6,7,8], [1,2,3,4,5,6,7,8,9]], [1,2,3,4,5,6,7,8,9])
   (10)  true
                                                            Type: Boolean
(11) -> t([[], [1], [1,2,3], [1,2,3,4,5], [1,2,3,4,5,6,7], [1,2,3,4,5,6,7,8,9]], [1,2,3,4,5,6,7,8,9])
   (11)  true
                                                            Type: Boolean
(2) -> t([[1]], [1])
   (2)  false
                                                            Type: Boolean
(4) -> t([[],[2]], [1,2])
   (4)  false
                                                            Type: Boolean
(5) -> t([[],[1,2],[2,3]], [1,2,3])
   (5)  false
                                                            Type: Boolean
(6) -> t([[],[1],[1,2],[2,3],[1,2,3]], [1,2,3])
   (6)  false
                                                            Type: Boolean
(7) -> t([[],[1],[2],[3],[1,2],[2,3],[1,2,3]] , [1,2,3])
   (7)  false
                                                            Type: Boolean
(8) -> t([[], [1], [2,3], [2], [4,5,6], [5,6], [5], [2,5,6], [2,5], [1,5],[1,2,3,4,5,6], [1,2,3], [1,2], [1,4,5,6], [1,5,6], [1,2,5,6], [2,3,4,5,6], [2,3,5,6], [2,3,5], [2,4,5,6], [1,2,5], [1,2,3,5,6], [1,2,4,5,6]], [1,2,3,4,5,6])
   (8)  false
                                                            Type: Boolean
(9) -> t([[], [1], [2], [1,2,3], [1,2,3,4,5], [1,2,3,4,5,6,7], [1,2,3,4,5,6,7,8,9]] , [1,2,3,4,5,6,7,8,9])
   (9)  false
                                                            Type: Boolean
(10) -> t([[], [1], [2,3], [2], [4,5,6], [5,6], [5], [2,5,6], [2,5], [1,5],[1,2,3,4,5,6], [1,2,3], [1,2], [1,4,5,6], [1,5,6], [1,2,5,6], [2,3,4,5,6], [2,3,5,6], [2,3,5], [2,4,5,6], [1,2,5], [1,2,3,5,6], [1,2,4,5,6]], [1,2,3,4,5,6])
   (10)  false
                                                            Type: Boolean
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.