集合的第N个子集


13

任务

鉴于设置

S=[1,2,3,4,5,6,7,8]

和一个整数

0N<2|S|

找到第N个子集。

输入输出

N在stdin上以无符号整数形式给出。您必须打印适合您的语言格式的第N子集(这可能包括[1,2,3]{1,2,3}[1, 2, 3]1 2 31,2,3等,只要它是人类可读的文本格式)。

关于子集的一点

在基数二中,子集和数字之间存在关系。每个数字 指定该集合的i个元素是否在子集中。例如,00000000是空集,而10000001是包含(最后一个元素和第一个元素)的子集。通过将数字转换为基数2得到第N个子集,然后该子集包含所有元素。因此,第三个子集(3 = 00000011)包含。最右边的数字是数字#0。可以打印。该集不必排序。

di
d i > 0[1,8]
di>0
[1,2][2,1]

附录:

是的,集合固定为1..8。该集合不是输入的一部分。输入只是ñ

是的,您可以使用其他输入形式。

所有N的所有预期输出:https : //tio.run/##SyotykktLixN/f/fyNS02qIoP8soJd1CwSAg2kY32LPWPaoqs7jg/38A


1
是一组专门18,或者是任何一套?
Jo King

2
令我惊讶的是,以前没有人问过:您是否愿意允许作为输入的函数使用输入作为参数,而不是强迫语言使用stdin(有些则不能使用)?问题是关于子集,而不是摆弄输入。
ბიმო

5
您无需告诉所有人他们的解决方案是否正确,您可以限制自己告诉何时不正确。
ბიმო

1
由于该集合限制为1..8,因此输出"123"将是明确的。有效吗
Arnauld

2
我们可以使用索引为0的[0,7]代替[1,8]吗?
Erik the Outgolfer

Answers:


17

果冻,3个字节

BUT

在线尝试!

怎么运行的

BUT  Main link. Argument: n

B    Binary; convert n to base 2.
 U   Upend; reverse the resulting array, so it starts with the LSB.
  T  Truth; find all 1-based indices of set bits.

5
但是,但是,但是...?!
Arnauld

2
@Arnauld但是一切都是谎言!您认为一切都是Binary,是吗?嗯...这颠覆是真理!因此,不,并非所有内容都是Binary。欢迎来到灰色地带!
暴民埃里克(Erik the Outgolfer)





2

K4,7个字节

解:

1+&|2\:

例:

前十...

q)k)(1+&|2\:)@'!10
`long$()
,1
,2
1 2
,3
1 3
2 3
1 2 3
,4
1 4

说明:

1+&|2\: / the solution
    2\: / convert to base-2
   |    / reverse
  &     / indices where true
1+      / add 1


2

Japt,7个字节

ì2 Ôi ð

尝试一下

ì2          :Convert to base-2 digit array
   Ô        :Reverse
    i       :Prepend null element
      ð     :0-based indices of truthy elements

¤¬²Ôð¥1

尝试一下

¤           :Convert to base-2 string
 ¬          :Split
  ²         :Push 2
   Ô        :Reverse
    ð       :0-based indices of elements
     ¥1     :  Equal to 1

1

外壳,5个字节

`fN↔ḋ

将输入作为命令行参数而不是在stdin输入我希望这是可以的),请在线尝试!

说明

`fN↔ḋ  -- example input: 6
    ḋ  -- convert to binary: [1,1,0]
   ↔   -- reverse: [0,1,1]
`      -- flip the arguments of
 fN    -- | filter the natural numbers by another list
       -- : [2,3]

1

Haskell55 54字节

s n=[x|(x,d)<-zip[8,7..]$mapM(pure[0,1])[1..8]!!n,d>0]

以相反的顺序输出设置,在线尝试!

普通版,56字节

{i}i=18

s n=[x|(x,d)<-zip[n,n-1..]$mapM(pure[0,1])[1..n]!!n,d>0]

在线尝试!

说明

该术语mapM (pure [0,1]) [1..n]生成列表(n=4[[0,0,0,0],[0,0,0,1],[0,0,1,0],..,[1,1,1,1]]-即 的二进制表示形式[0..2^n-1]。用索引它n给我们的二进制表示形式n

现在我们可以将zip其与反转的数字一起使用,[1..n]并且只保留二进制数字非零的元素:

 [ x | (x,digit) <- zip [n,n-1,..1] binaryRepN, digit > 0 ]

1

木炭,11字节

↓⭆⮌↨N²×ιI⊕κ

在线尝试!链接是详细版本的代码。如果水平打印答案而没有空格是可以接受的,则可以删除第一个字符。说明:

    N       Input as a number
   ↨        Converted to base
     ²      Literal 2
  ⮌         Reversed
 ⭆          Map over bits and join
          κ Current index (0-indexed)
         ⊕  Incremented
        I   Cast to string
       ι    Current bit
      ×     Repeat string
↓           Print vertically








0

Python 3.6,58个字节

f=lambda n:[8-v for v,i in enumerate(f'{n:08b}')if int(i)]



0

APL + WIN,13个字节

提示输入N:

((8⍴2)⊤⎕)/⌽⍳8

在线尝试!由Dyalog Classic提供

说明:

((8⍴2)⊤⎕) prompt for N and convert to binary

/⌽⍳8 generate a vector from 1 to 8, reverse and select integers according to 1s in binary

以相反的顺序返回子集



0

Oracle SQL,77个字节

select*from(select rownum r,value(p)from t,table(powermultiset(x))p)where:n=r

在SQL Plus中测试

SQL> var n number
SQL> exec :n:=67;

PL/SQL procedure successfully completed.

SQL> with t as (select ku$_vcnt(1,2,3,4,5,6,7,8) x from dual)
  2  select*from(select rownum r,value(p)from t,table(powermultiset(x))p)where:n=r
  3  /
        67
KU$_VCNT('1', '2', '7')

0

MathGolf,8个字节

â^mÉ┤\*─

在线尝试!

说明

â         Convert first input to binary list
 ^        Zip with [1,2,3,4,5,6,7,8] (other input)
  mÉ      Map 2D array using the next 3 instuctions
    ┤     Pop from right of array
     \*   Swap top two elements and repeat array either 0 or 1 times
       ─  Flatten to 1D array

备用输出格式

使用更灵活的输出格式(我个人认为看起来不错),我可以得出一个6倍的结果:

â^É┤\*

我使用隐式的for-each而不是映射,并且跳过了展平。输出看起来像这样:

[1][2][][4][5][6][7][]



0

05AB1E,6 个字节

bRSƶ0K

在线尝试验证所有可能的测试用例

说明:

b         # Convert the (implicit) integer input to binary
          #  i.e. 22 → "10110"
 R        # Reverse it
          #  i.e. "10110" → "01101"
  S       # Convert it to a list of 0s and 1s
          #  i.e. "01101" → ["0","1","1","0","1"]
   ƶ      # Multiply each with its 1-indexed index
          #  i.e. ["0","1","1","0","1"] → [0,2,3,0,5]
    0K    # Remove all 0s (and output implicitly)
          #  i.e. [0,2,3,0,5] → [2,3,5]

0

Java 8,58字节

n->{for(int i=0;i<8;)if((1&n>>i++)>0)System.out.print(i);}

在线尝试。

说明:

n->{                        // Method with integer as parameter and no return-type
  for(int i=0;i<8;)         //  Loop `i` in the range [0,8):
    if((1&n>>i++)>0)        //   If 1 AND `n` bitwise right-shifted to `i` is larger than 0
                            //   (with `i` increased by 1 afterwards with `i++`)
      System.out.print(i);} //    Print `i+1`
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.