双工:重要的无用运算符


33

输入项

  • 由1到255个正整数(包括)组成的列表,每个整数的范围为1到2 32-1(包括)。
  • 您的输入格式不必与测试用例相同。
  • 必须接受不带前导零的输入。
  • 不需要接受前导零的输入。
  • 您可以在整数之间使用所需的任何定界符。
  • 整数可以用字符串表示,但是特定整数的各个数字必须是连续的。
  • 您可以选择对输入使用任何基准(包括二进制和一进制),前提是输出也在该基准中。

输出量

  • 一个整数。
  • 输出必须没有前导零。
  • 输出必须与输入具有相同的基数。
  • 可以用任何方式计算输出,但必须与以下计算结果匹配:

计算双工

  • 二进制表示形式的位从零开始从右开始编号,因此位i在表示2 i的列中。
  • BITSUM是的总和 每个输入数字的二进制表示的比特。
  • BITSUM最大的是由bitsums采取的最高值。
  • 所述BITSUM最小是最低非零由bitsums采取值。
  • 输出的二进制表示的数字是:
    • 如果第i 比特和等于最大比特和最小比特,则为1。
    • 否则为0。

工作的例子

本示例使用二进制作为输入和输出。

Input:    100110
         1101110
         1100101
         _______
Bitsums: 2301321

Output:   101101

最大位数为3,最小位数为1,因此,在每个位数为3或1的地方输出均为1,在其他地方均为0。


测试用例

测试用例的形式为:

Input => Output

二进制测试用例:

[1] => 1
[10] => 10
[1, 10, 101] => 111
[11111111111111111111111111111111] => 11111111111111111111111111111111
[10010010010010010010010010010010, 10101010101010101010101010101010, 11011011011011011011011011011011] => 11100011100011100011100011100011
[10001011100010100110100101001001, 10110000111110010000111110111010, 1101110001101101011010010100101, 1010101010001011101001001010101] => 11 

十进制相同的测试用例:

[1] => 1
[2] => 2
[1, 2, 5] => 7
[4294967295] => 4294967295
[2454267026, 2863311530, 3681400539] => 3817748707
[2341103945, 2969112506, 1849078949, 1430639189] => 3

排行榜

感谢Martin的排行榜摘要


在聊天中讨论之后,该操作员被命名为biplex,是二进制平面极值的缩写。


可以像示例中一样要求输入为二进制吗?
feersum 2015年

1
@feersum You may choose to use any base for input and output (including binary and unary), provided they are both in the same base。所以,是的,去吧:)
trichoplax

我们可以使用[[1,0,1],[1,1,0,0],[1,0,0,1]]这样的符号吗?
Akangka,2015年

可能不会。假设限制最严格。
Akangka,2015年

@ChristianIrwan输入应为整数形式(每个数字均由连续的数字组成)。整数可以表示为字符串,但数字之间不能有分隔符。
trichoplax

Answers:


1

果冻,无竞争

14个字节这个答案是非竞争性的,因为挑战早于创建Jelly。

BUSµḟ0Ṣ.ịe€@UḄ

在线尝试!

怎么运行的

BUSµḟ0Ṣ.ịe€@UḄ    Main link. Input: A (list)

B                 Convert each item in A to binary.
 U                Reverse each array of binary digits.
  S               Add them across columns.

   µ              Monadic chain. Argument: S (list of column sums)
    ḟ0            Remove all occurrences of 0.
      Ṣ           Sort the remaining sums.
       .ị         Take the elements with indices around 0.5.
                  Indices are 1-bases, so this selects the last and the first one.
             U    Yield S, reversed.
            @     Reverse the arguments.
         e€       Test each item in the left list for membership in the right one.
              Ḅ   Convert from binary to integer.                

尽管在如何判断上存在分歧,但这个元问题表明,在挑战早于创建语言之前,社区强烈赞成不再将答案标记为非竞争性。因此,我接受这个答案。
trichoplax

11

Pyth,26 25字节

JsM.T_MjR2Qi_}RhM_BS-J0J2

在线尝试:演示测试套件

说明

JsM.T_MjR2Q
       jR2Q      convert each input number to binary (lists of 1s and 0s)
     _M          reverse each list
   .T            transpose (with top justify)
 sM              sum up each list (numbers of 1s at each position)
J                store this list in J

i_}RhM_BS-J0J2
         -J0     J without 0s
        S        sorted
      _B         create the list [sorted, reverse(sorted)]
    hM           take the first elments of each (bitsum-min and bitsum-max)
  }R        J    check for each value in J, if it is part of ^
 _               reverse this list of booleans
i            2   convert from binary to base 10 and print

1
_B应用是辉煌
isaacg

9

J,31 30 24 23 21字节

+/(e.>./,<./@#~@)&.#:

这是一个默认的单调动词,它使用十进制整数列表并返回其十进制双工。

感谢@Zgarb的建议,该建议直接节省了4个字节,并为另外2个字节铺平了道路!

感谢@randomra多打了2个字节!

测试用例

   biplex =: +/(e.>./,<./@#~@)&.#:

   biplex ,1
1
   biplex ,2
2
   biplex 1 2 5
7
   biplex ,4294967295
4294967295
   biplex 2454267026 2863311530 3681400539
3817748707
   biplex 2341103945 2969112506 1849078949 1430639189
3

怎么运行的

                 &.    Dual. Apply the verb to the right, the verb to the left,
                       and finally the inverse of the verb to the right.
                   #:  Convert the input list from integer to base 2.
  (            @)      Define an adverb, i.e., an operator that takes a verb as
                       its left argument.
+/                     Call it with "reduce by sum". This calculates the sum of
                       the corresponding binary digits of all integers before
                       executing the remainder of the adverb's body, i.e, this:
             #~          Replicate the sum N a total of N times, i.e., turn
                         0 1 2 3 into 1 2 2 3 3 3. This eliminates zeroes.
         <./@            Calculate the minimum of the result.
     >./                 Calculate the maximum of the sums.
        ,                Append; wrap both extrema into a list.
   e.                    Check if each of the sums is in the list of extrema.
                         This yields 1 if yes and 0 if no.
                       (from &.) Convert from base 2 to integer.

9

Minkolang 0.10109个 79字节

(n1$(d2%$r2:d)99*I-DmI2:[+1R]$I)rI$d$(s0(x0gd,)Ik3R2g1-X0I3-[2*2gd0c=$r1c=++]N.

输入和输出均为十进制。在这里尝试。

说明

(                            $I)    Loop until input is empty
 n                                  Read in number from input
  1$(       d)                      Start a while loop with only the top of stack
     d2%                            Next least-significant bit (modulo by 2)
        $r                          Swap top two values
          2:                        Divide by 2
              99*I-D                Pad with 0s
                                    (when the while loop exits, top of stack is 0)
                    m               Merge (interleave) stack
                     I2:[   ]       For each pair...
                         +1R        Add and rotate stack to the right
                                    <<This does the work of computing bitsums>>

r       Reverse stack
 I      Push length of stack
  $d    Duplicate whole stack
        <<This lets me sort the bitsums without affecting the original order>>

$(                      Start while loop with <top of stack> elements
  s                     Sort
   0(     )             Push a 0 and start another while loop
     x                  Dump top of stack
      0g                Get front of stack and put it on top
        d,              Duplicate and <not> for end-while condition check
           Ik           Push length of stack and break out of while loop
             3R         Rotate [min, max, length] to front
               2g1-     Get length and put it on top, then subtract 1
                   X    Dump that many elements off the top of stack
                        <<Now I have min and max>>

0                        Initialize decimal with 0    
 I3-[               ]    For as many bits as there are...
     2*                  Multiply by 2
       2gd               Get the next most significant bit and put it on top
          0c=            Copy min and check for equality
             $r          Swap top two elements of stack
               1c=       Copy max and check for equality
                  ++     Add 1 if bitsum item is equal to min or max, 0 otherwise
N.                       Output as integer and stop

旧版:

$nI[(d2%i1+0a+i1+0A00ai`4&i00A2:d)x]00a1+[i1+0a]s0(x0gd,)rI2-[x]00a1+[i1+0ad0c=$r1c=+]0gx0gx0(xd,)0I1-[2*+]N.

在这里尝试!

说明

问题的关键在于,大量使用了数组功能(a A)来存储位和,找到其最小值和最大值,然后适当地输出1s和0s,并将前导0s放在两个位置。

$n                                      Read in whole input as integers
  I[(                             x]    Convert each number to binary
     d2%                                Get next least significant bit (modulo by 2)
        i1+0a                           Get current value in array for that position
             +                          Add
              i1+0A                     Put current value in array for that position
                   00ai`                Get first value of array (bitsum length)
                      i`                Greater than loop counter?
                        4&i00A          If not, put loop counter there
                              2:        Divide by 2
                                d)      If 0, exit loop

00a                Get first value of array (maximum length
   1+              Add one
     [i1+0a]       Get bitsum values from array and push on stack
            s      Sort
0(                 Push a 0 (for dump) and start a while loop -- dumps leading 0s
  x                Dump top of stack
   0g              Get bottom of stack
     d,            Duplicate and <not> it
       )           Exit loop when top of stack is non-zero (i.e., the minimum)
        r          Reverse stack (so now it's [min, max, <stuff>])
         I2-[x]    Dump everything else

00a1+[               ]    Get bitsum length and loop that many times
      i1+0a               Get bitsum value at current position
           d              Duplicate
            0c=           Copy front of stack and check for equality
               $r         Swap
                 1c=      Copy next-to-front of stack and check for equality
                    +     Add (so it's 1 if it's equal to min or max, 0 otherwise)

0gx0gx       Dump front two elements of stack (the min and max)
0(xd,)       Dump leading 0s
0            Push 0 for conversion to decimal
 I1-[   ]    For each bit...
     2*+     Multiply by 2 and add
N.           Output as integer and stop

从在线解释器中,这是否接受十进制输入并给出二进制输出?
trichoplax

是。我需要改变吗?
El'endia Starman

1
啊,我一直在寻找要点,但在第一句话中就错过了。我会在今天的某个时间修复它。
El'endia Starman

1
固定!而且只有两个额外的字节!:D
El'endia Starman 2015年

1
您已赢得我的+1;我很高兴了解Minkolang。
lirtosiast

8

Brainfuck,619字节

这里的第一答案太久了,所以我想我会做的很好!

>->->->>>,----------[--<++++++[>------<-]>>>>,----------]-<<<+[-<<<+]->>,<++++[>--------<-]>[<++++[>+++++<-]>++[--<++++++[>------<-]>>>+>]<-[+>-<<+[-<<<+]-<<+[->+[->[-[->>>+<<<]<<<+]+[-->>>++]-<+]<<<+[-[->+<]<<<+]->>+>]<-[+>->>>>+[->>>+]->+[-<[-[->>>+<<<]<<<+]+[-->>>++]->+]-<+<<<+[-[->+<]<<<+]->>]]>,<++++[>--------<-]>]>+[->>>+]>->->>-[+<<<<<<+[-<-<<+]->>[+>>>[>>>]>+[<<->>[->>>+]->>>[-]+<<<<<+[-<<<+]+>->]<]>->>+>-]<[<<<<<+[-<+<<+]->>[-]>+[->>>+]->>-]>-[+<<<<<<+[-<+<<+]->>[->>>[>>>]>+[<<<->>>[->>>+]->>>[-]+<<<<<<+[-<<<+]+>>+>]<]>->>>-]<<<<-<<<<+[->[-]<[->+<]<[->>[-]+<<]<<+]>-[+>>>-]++[->++++++[-<++++++++>]<.>>>+]

我赢了吗

该程序10在每个二进制数之后都需要换行符(ASCII ),并且需要一个空格(ASCII32在末尾)。

测试运行:

Me$ bf bpx.bf
1
10
101

111

Me$ bf bpx.bf
11111111111111111111111111111111

11111111111111111111111111111111

Me$ bf bpx.bf
10010010010010010010010010010010
10101010101010101010101010101010
11011011011011011011011011011011

11100011100011100011100011100011

Me$ bf bpx.bf
10001011100010100110100101001001
10110000111110010000111110111010
1101110001101101011010010100101
1010101010001011101001001010101

11

(在每种情况下, ++++++++++.添加并在程序末尾添加)

说明

仍在进行中,但进展缓慢。由于时间紧迫,我只复制了在编写此程序时所做的笔记,现在就可以解释一下:

[Biplex Calculator]

[

Bitsum writing philosophy: 

Start with [0 255 255 255 ...].
The 255s will be markers that we can 'search' for to get back to the start.
After that, the next byte should be a 0, fairly permanently. (The first reference 0)
It is useful to have 0-bytes handy for general calculations. 
The next byte is where we will read the first digit of any binary number. (The first read)
The next byte holds the first bitsum.
Afterward, the next byte is 0, the following byte is the place
to read the second binary digit, the following byte is the second bitsum,
and so on.
I'll use the terminology nth reference 0, nth read, and nth bitsum to refer to
each part of the triplet of bytes.

The location three places after the final bitsum will be 255,
and the location three places after the final read will be 255.
We can move entire numbers by lining up these 255s after every binary number.

]


>->->->>>~                  [0 255 255 255 0 0 0 0 0                ]

[From the first bitsum, read input to create an initial set of bitsums                  ]
,----------
[
    --<++++++[>------<-]            [Convert the input '1' or '0' into a bit        ]
    >>>>,---------- 
]~                      
-<<<+[-<<<+]->>                 [Mark 255 after the last bit, then go back to first read]

,<++++[>--------<-]             [Check for space, using the first reference 0       ]
>
[
    <++++[>+++++<-]             [Check for \n                       ]
    >++
    [
        --<++++++[>------<-]        [It wasn't \n, so convert the input into a bit      ]
        >>>+>               [Write 1 to the next reference 0, and go to next read   ]
    ]
    <-
    [
        +>              [It was \n                      ]
        -<              [Mark 255 on this (nth) read and go to (n-1)th bitsum   ]
        <+[-<<<+]           [Search for a 255 off to the left           ]

        [We've either struck our marker, or the last bitsum. We need to check.          ]
        -<<+
        [
            -           [We hit the last bitsum. Move the bitsums right.    ]
            >+
            [
                ->[-[->>>+<<<]<<<+] [Move until we reach the start          ]
                +[-->>>++]      [From the start, move to our last bitsum    ]
                [Repeat this move until the read here is a 255              ]
                -<+
            ]
            [We now need to go to the first reference zero, and put a 1 there       ]
            <<<+[-[->+<]<<<+]->>+>      [Add bits to bitsums and end on first read  ]
        ]
        <-
        [
            +>->>>>+[->>>+]     [Go to the 255 after our reads              ]
            ->+
            [
                -<[-[->>>+<<<]<<<+] [Move until we reach the start          ]
                +[-->>>++]      [From the start, move to the 255 after our reads]
                [Repeat this move until we see the 255 bitsum               ]
                ->+
            ]
            [We now need to go to the first read                        ]
            -<+<<<+[-[->+<]<<<+]->>     [Add bits to bitsums and end on first read  ]
        ]
        >>>>>>>>>>>>>>>>>>>>>>>>>~<<<<<<<<<<<<<<<<<<<<<<<<<
    ]

    [We're at the reference 0 to the left of our next read                      ]
    >,<++++[>--------<-]            [Check for space, using the first reference 0       ]
    >
]

[

Let BN be the nth bitsum. Then our situation at the moment is:

[0 255 255 255 0 0 B1 0 0 B2 ... 0 0 BN 0 0 255] 
                 ^
I'm just happy nothing's exploded yet.

]

[Next goal: Mark the minimums                                       ]

>+[->>>+]>->->>~            [Zero the 255 and put 2 255s after it, then move after it   ]

-[
    +<<<<<<
    +[-<-<<+]               [Subtract one from each bitsum              ]
    ->>
    [
        +>>>[>>>]           [Find the first 0 bitsum                ]
        >+
        [
            <<->>[->>>+]->>>[-]     [Mark the zero as a minimum, and set a flag     ]
            +<<<<<+[-<<<+]+>->
        ]
        <
    ]
    >->>+>-
]~

[Hey, that worked. Weird. Alright, moving on...                             ]
[Reset the bitsums to what they were before we messed them all up                   ]

<[<<<<<+[-<+<<+]->>[-]>+[->>>+]->>-]>~

[After the stuff up there, that's not too bad.                              ]
[Now it's time to mark the maximums, in virtually the opposite way we did the minimums.         ]

-[
    +<<<<<<
    +[-<+<<+]               [Add one to each bitsum                 ]
    ->>
    [
        ->>>[>>>]           [Find the first 0 bitsum                ]
        >+
        [
            <<<->>>[->>>+]->>>[-]   [Mark the zero as a maximum, and set a flag     ]
            +<<<<<<+[-<<<+]+>>+>
        ]
        <
    ]
    >->>>-
]~

[Alright, now OR the maxs and mins, take care of leading zeros, output, and cross fingers       ]
<<<<->>~<<<<<<~+[->[-]<[->+<]<[->>[-]+<<]<<+]>-[+>>>-]++[->++++++[-<++++++++>]<.>>>+]

换行符分隔并且空格终止是完全有效的。+1
trichoplax

7

CJam,27个字节

q~2fbWf%:.+_0-$(\W>|fe=W%2b

将输入作为10进制CJam样式列表。在这里测试。或者,运行所有测试用例(脚本会丢弃预期的输出,并根据需要转换输入格式)。

说明

q~    e# Read and evaluate input.
2fb   e# Convert each number to base 2.
Wf%   e# Reverse each digit list, to align digits in case there are numbers with different 
      e# widths.
:.+   e# Add up the bit planes.
_0-   e# Make a copy and remove zeroes.
$     e# Sort the bit plane sums.
(\W>| e# Get the first and last element. We use set union instead of addition so that the
      e# resulting array will contain only one value if min = max.
fe=   e# For each element in the bit plane sums, count how often it appears in the min/max
      e# array, which is either 1 or 0.
W%    e# Reverse the digits again (to undo the initial reversing).
2b    e# Interpret as base-2 digits.

输出不应该是最大和最小比特数吗?
DavidC 2015年

1
@DavidCarraher不,请参见示例。输出应该是一个数字,其二进制表示形式1在那些与出现最小值和最大值的位平面相对应的位中具有s。
马丁·恩德 Martin Ender)

6

的JavaScript(ES6),215个 185 176字节

f=a=>{s=[];for(i of a)for(b=i.toString(2),d=l=b.length;d--;)s[x=l-d-1]=(s[x]|0)+(b[d]|0);q=255;for(r of s)d=r>d?r:d,q=r<q?r||q:q;o="";s.map(r=>o=(r==q|r==d)+o);return+("0b"+o)}

用法

f([2454267026, 2863311530, 3681400539])
=> 3817748707

说明

f=a=>{

  // Create an array of bitsums
  s=[];                   // s = bitsums
  for(i of a)             // iterate through the passed array of numbers
    for(
      b=i.toString(2),    // b = number as binary string
      d=l=b.length;       // l = number of digits in b
      d--;                // iterate through each digit of the binary string
    )
      s[x=l-d-1]=         // add to the digit of the bitsum array
        (s[x]|0)+         // get the current value of the bitsum array (or 0 if null)
        (b[d]|0);         // add the result to the bitsum array

  // Get the maximum and minimum bitsums
  q=255;                  // q = min bitsum
  //d=0;                  // d = max bitsum
  for(r of s)             // iterate through the bitsums
    d=r>d?r:d,            // set d to maximum
    q=r<q?r||q:q;         // set q to minimum

  // Calculate the result
  // (unfortunately JavaScript only supports bitwise operations on signed 32-bit
  // integers so the temporary binary string is necessary)
  o="";                   // o = output
  s.map(r=>               // iterate through the bitsum digits
    o=(r==q|r==d)+o       // add a 1 to the string if the digit is equal to min or max
  );
  return+("0b"+o)         // convert the binary string to a number
}

3
欢迎来到PPCG!我看到可以缩短时间的几种方法。1)当使用一个参数定义箭头功能时,不需要括号。f=(a)=>{}= f=a=>{}2)您可以将for...in循环变成for...of循环,节省一些字节:for(i in a)for(b=a[i]...= for(i of a)for(b=i...for(i in s)r=s[i],d=...= for(r of s)d=...3)按位或|自动将true更改为1,将false更改为0,因此o+=r==q|r==d?1:0;与相同o+=r==q|r==d;
ETHproductions 2015年

1
最后,您可以进行以下操作:return parseInt(o,2)至:return+('0b'+o)
Downgoat 2015年

1
为了节省2个字节,可以替换s[l-d-1]=(s[l-d-1]|0)使用s[T=l-d-1]=(s[T]|0)。要剃除更多2,请替换b=i.toString(2),l=b.length,d=ld=l=(b=i.toString(2)).length
Ismael Miguel

4

朱莉娅,141个字节

A->(s=reduce(.+,map(i->digits(i,2,maximum(map(i->ndigits(i,2),A))),A));parse(Int,reverse(join([1(iextrema(filter(j->j>0,s)))for i=s])),2))

取消高尔夫:

function biplex(A::AbstractArray)
    # Get the maximum number of binary digits in each element
    # of the input array
    L = maximum(map(i -> ndigits(i, 2), A))

    # Create an array of arrays of binary digits corresponding
    # to the inputs
    B = map(i -> digits(i, 2, L), A)

    # Get the bitsums
    S = reduce(.+, B)

    # Replace the minimum and maximum bitsums with ones and
    # the rest with zeros
    s = [1 * (i in extrema(filter(j -> j > 0, S))) for i in S]

    # Join this into a string and parse it as a base 2 integer
    # Reverse is required because digits() returns the digits
    # in reverse order
    p = parse(Int, reverse(join(s)), 2)

    return p
end

3

单纯形v.0.7字节

简化的评论。以Binary输入,目前没有任何口译员。希望评论是足够的。

hj&=j&Lun?&RvRpp]{Ri}^XKRJ@jqLhR@LhuTo
h               ]                        ~~ define macro 0
 j&                                      ~~ insert a new byte and write register to it
   =                                     ~~ equal to the previous byte?
    j&                                   ~~ insert a new byte and write register to it
      L                                  ~~ return to equality
       u    v                            ~~ go up/down a strip
        n?&                              ~~ write the register iff byte = 0
           R                             ~~ go right
             Rpp                         ~~ remove two bytes
                 {Ri}                    ~~ take input until input = 0
                     ^X                  ~~ take the sum of the strip
                       K                 ~~ split that sum into digits
                        RJ@              ~~ take the max; write to register
                           jq            ~~ take the min
                             Lh          ~~ call macro 0
                               R@        ~~ set min to register
                                 Lh      ~~ apply macro 0
                                   uTo   ~~ output result 

3

八度,50字节

@(a)["" ((b=sum(a-48))==max(b)|b==min(b(b>0)))+48]

例:

octave:1> g = @(a)["" ((b=sum(a-48))==max(b)|b==min(b(b>0)))+48] ;
octave:2> g(["10010010010010010010010010010010"; "10101010101010101010101010101010"; "11011011011011011011011011011011"])
ans = 11100011100011100011100011100011

我希望看到对此的一种解释...
trichoplax

2
@trichoplax我对matlab / octave不太聪明,但是希望这会有所帮助:@(a)定义一个采用输入向量 的匿名函数a["" 强制输出成字符串(我相信)。(b=sum(a-48))定义b为包含位和的向量。如果位和最大,则将是在给定位置b==max(b)具有a的向量,否则返回。是的每一个元素的向量大于,因此是含有一个矢量时BITSUM为最小,并以其他方式。然后将其或在一起,并使用ASCII。10b(b>0)b0b==min(b(b>0))10+48
BrainSteel

@BrainSteel现在都很有意义-谢谢
trichoplax

有时我忘记了Octave和MATLAB的不同。我想我将来真的应该开始使用Octave打高尔夫球,我最短的MATLAB尝试是60字节,因为您不能使用内联分配并以function o=b(i)(newline)结尾r=sum(i>48);o=['' (r==max(r)|r==min(r))+48];
Sanchises

3

JavaScript(ES6),158

具有数字数组参数的函数,返回数字。使用相同的字节数,它可以获得一个字符串数组参数(包含以2为基的表示形式)并返回以2为基的字符串-只需将.toString(2)末尾的移到该位置即可r

f=l=>(l.map(v=>[...v.toString(2)].reverse().map((x,i)=>t[i]=~~t[i]-x),t=[]),t.map(v=>(r+=v==Math.min(...t)|v==Math.max(...t.filter(x=>x))&&b,b+=b),r=0,b=1),r)

// More readable

u=l=>(
  t=[],
  l.map(v =>
    [...v.toString(2)]
    .reverse()
    .map((x,i) => t[i] = ~~t[i] - x)
  ),
  r=0,b=1,
  t.map(v => (
    r += v==Math.min(...t) | v==Math.max(...t.filter(x=>x)) && b, b+=b
  )),
  r
)

// Test
console.log=s=>O.innerHTML+=s+'\n'

;[
 [[1], 1]
,[[2], 2]
,[[1, 2, 5], 7]
,[[4294967295], 4294967295]
,[[2454267026, 2863311530, 3681400539], 3817748707]
,[[2341103945, 2969112506, 1849078949, 1430639189], 3]
].forEach(t =>{ 
  r=f(t[0])
  x=t[1]
  console.log('Test '+(r==x?'OK':'Fail (Expected: ' + x +')')
  +'\nInput: '+t[0]+'\nResult: ' +r+'\n')                       
})  
<pre id=O></pre>


3

哈斯克尔,198 182 178 161字

我仍然是打高尔夫球的初学者。答案只有80个声誉。

m=map
v=reverse
(b:x)&(c:y)=(b+c):x&y
[]&a=a
b&_=b
l=(show=<<).v.(\a->m(fromEnum.(`elem`[maximum a,minimum$(replicate=<<id)=<<a]))a).foldl1(&).m(m(read.(:[])).v)

它是如何工作的。

我不使用填充,而是反转数组,然后使用用户定义的(&)添加。我不使用更短的foldl1(zipWith(+)),因为zipWith会删除多余的项。然后,我找到最大值和非零最小值,这也是用户定义的函数。然后我将项目与最大值和非零最小值进行匹配,如果匹配则为1,如果不匹配则为0。然后我们反转并将其转换为二进制数。

去做:

  1. 使用 Data.List

2
在的定义中kc仅使用一次,因此无需将其放在where子句中。直接使用:...||(x==a#b)...。为什么花括号?在lconcat.map showconcatMap show,甚至更好>>=l=(>>=show).v...。(注意:也有=<<相同的功能,但参数已翻转:)(show=<<).v...>>==<<在此列表环境工作做在功能上或其他情况下不同的东西。
nimi 2015年

2
您可以替换[]在最后的情况下&,并#_b&_=b_#l=l
nimi 2015年

2
(x==b)||(x==c)(或(x==b)||(x==a#b)删除时c)可以由elem x[b,c](resp:)代替elem x[b,a#b]
nimi 2015年

2
u=maximum并且k a=m(\x->fromEnum$elem x[u a,a#u a])a应该工作。
nimi 2015年

1
不要这样做_&a=a,它必须保留[]&a=a,否则如果第二个列表为空,则第一个列表将丢失。使用_只在最后一种情况-这也许就是你的错误。
nimi 2015年

3

Python 3中,181个 126 122字节

(我删除了大多数的字节数斜杠,因为它变得有点荒谬。)感谢Sp3000使它减少了 21个字节!

*z,=map(sum,zip(*[map(int,t.zfill(99))for t in input().split()]))
k=0
for x in z:k=k*10+(x in[min(z)or 1,max(z)])
print(k)

略少打高尔夫球:

s=input().split()
k=list(map(lambda t:list(map(int,t))[::-1],s))
z=list(map(sum,zip(*k)))
y=z[:]
while min(y)==0:y.remove(0)
a,b=min(y),max(y)
f=''.join(map(lambda x:str(1*(x in[a,b])),z[::-1]))
print(int(f))

期望以二进制形式输入,只有空格分隔数字。输出也是二进制的。


3

Javascript,154150字节

t=i=>(m=0,r=[],i.map(n=>{for(j=31;j+1;r[k=31-j]=(r[k]|0)+((n>>>j)%2),--j);}),r.map(n=>m=(n==Math.min(...r.filter(x=>x))|n==Math.max(...r)?1:0)+m*2),m)

感谢edc65方法进行最小/最大计算,它将我的代码缩短了4个字节。

说明

t=i=>(
    m=0,//m=>output
    r=[],//Bitsum then final number in binary
    i.map(
        n=>
        {
            //Decimal to binary + bitsum
            for(j=31;j+1;r[k=31-j]=(r[k]|0)+((n>>>j)%2),--j);
        }),
    //Output formatting
    r.map(
        n=>
            m=(n==Math.min(...r.filter(x=>x))|n==Math.max(...r)?1:0)+m*2
        ),
    m
)

+1好。位算术远远胜过了字符串转换。我看到了两种方法来进一步缩短它。
edc65

132:f=i=>(m=0,r=[],i.map(n=>{for(k=32;k--;n=n/2|0)r[k]=~~r[k]+n%2}),r.map(n=>m+=m+(n==Math.min(...r.filter(x=>x))|n==Math.max(...r))),m)
edc65

更好!我需要了解有关打高尔夫球的更多信息,但仍然不能使用hijeck进行说明。
Naouak 2015年

3

ShapeScript,186字节

'"1
"$0?_1-"+"*":2"@~@"0
"~"0
"$"
"~+'1?_*!"2"$"0"~"
"$""~":"1?'@1?"+"$_1>"+"*+@1?"0"+$""~'1?_*!#"0"+@":+"'1?1?"0"+$_2<"+"*+'1?_*!"0"+$"1"~@$"1"~":"$""~"+"$""~'"
"@+"
0"$""~'1?_*!"
"$""~

重要的无用运算符的重要无用语言。

I / O是二进制的。程序期望每个数字在单独的行上,每个以换行符结尾。

在线尝试!

测试用例

$ echo -e '1' | shapescript biplex.shape; echo
1
$ echo -e '10' | shapescript biplex.shape; echo
10
$ echo -e '1\n10\n101' | shapescript biplex.shape; echo
111
$ echo -e '11111111111111111111111111111111' | shapescript biplex.shape; echo11111111111111111111111111111111
$ echo -e '10010010010010010010010010010010\n10101010101010101010101010101010\n11011011011011011011011011011011' | shapescript biplex.shape; echo
11100011100011100011100011100011
$ echo -e '10001011100010100110100101001001\n10110000111110010000111110111010\n1101110001101101011010010100101\n1010101010001011101001001010101' | shapescript biplex.shape; echo
11

我不知道这个垃圾叫什么语言,但是我喜欢它!
阶段

2

APL,27个字节

{2⊥S∊(⌈/,⌊/)0~⍨S←+/⍵⊤⍨32⍴2}

这是一个单子函数,它接受一个十进制整数列表并返回其十进制双工。

ngn / apl演示中在线尝试

怎么运行的

                     32⍴2  Create a list of 32 2's.
                  ⍵⊤⍨      Base-encode the right argument.
                +/         Add the corresponding binary digits.
              S←           Save the sums in S.
           0~⍨             Remove 0's.
    (⌈/,⌊/)                Apply minimum (⌊/), maximum (⌈/) and concatenate.
  S∊                       Check which binary digits are in the extrema list.
2⊥                         Convert from base 2 to integer.

2

Wolfram语言,113字节

此版本从弹出窗口中输入内容,数字以“ {x,y,z,...}”的形式输入(无引号)。

FromDigits[#/.{Max[#]->1,Min[#/.{0->Nothing}]->1,_Integer->0},2]&@Total[PadLeft[IntegerDigits[#,2],33]&/@Input[]]

输入:

动态弹出窗口

输出:

输出

将动态操纵中的输入作为形式为“ {x,y,z}”的字符串使用,其中x,y和z为基数10的整数(带或不带前导零)。输出也以10为底。

Manipulate[FromDigits[#/.{Max[#]->1,Min[#/.{0->Nothing}]->1,_Integer->0},2]&@Total[PadLeft[IntegerDigits[#,2],33]&/@ToExpression@i],{i,"{1}"}]

例

还有其他输入方式可以节省字符数,但我认为此解决方案很好地使用了执行计算的动态GUI

如果您想在云中运行它,我们可以使用CloudDeploy:

o=CloudDeploy[FormPage[{"i"->"String"},(FromDigits[#/.{Max[#]->1,Min[#/.{0->Nothing}]->1,_Integer->0},2]&@Total[PadLeft[IntegerDigits[#,2],33]&/@ToExpression@#i])&]];SetOptions[o,Permissions->{All->{"Read","Execute"},"Owner"->{"Read","Write","Execute"}}];o

云部署使字符数增加到256,但是...

FormPage的输入也是字符串“ {x,y,z}”,其中x,y和z为基数10的整数。

我自己进行了云部署,您可以在https://www.wolframcloud.com/objects/97b512df-64f8-4cae-979b-dba6d9622781进行尝试

云示例


我已经测试了没有前导零的云链接,并且它仍然可以正确输出
3。– trichoplax

如果您的代码较短但没有GUI,则可以将其编辑为该答案以提高得分,并且仍将GUI版本保留在答案中,以显示可能的结果。两全其美...
trichoplax

@trichoplax注意,我用Input []添加了我认为是最短的版本。也可以假设数字将存储在变量中,这也可以减少字符数,但是我认为您如何输入输入内容还是模棱两可的
Ian Johnson

将数字存储在变量中不是默认的输入方法之一 ...因此无法执行其他步骤,但是到目前为止您所拥有的非常棒:)
trichoplax

1

Python 3、197

import itertools
o='0'
d=[sum(map(int,n))for n in itertools.zip_longest(*map(reversed,input().split(' ')),fillvalue=o)]
m=max(d),min(d)or 1
print(''.join((o,'1')[g in m]for g in d[::-1]).lstrip(o))

它接受以空格分隔的二进制数。

非高尔夫版本:

import itertools
nums = map(reversed, input().split(' '))
digits = []
for num in itertools.zip_longest(*nums, fillvalue='0'):
    current_digit = sum(map(int,num))
    digits.append(current_digit)
bit_max = max(digits)
bit_min = min(digits) or min(digits) + 1
print(''.join(('0','1')[digit in(bit_max,bit_min)]for digit in digits[::-1]).lstrip('0'))

1

C#,255

完整的程序,以命令行参数(空格分隔)输入(十进制)。

using System.Linq;class P{static void Main(string[]l){var n=new uint[32];uint i,r=0,b;foreach(var v in l)for(i=0,b=uint.Parse(v);b!=0;b/=2)n[i++]+=b&1;b=1;foreach(var v in n){r+=(v==n.Max()|v== n.Min(x=>x>0?x:n.Max()))?b:0;b+=b;}System.Console.Write(r);}}

更具可读性:

using System.Linq;

class P
{
    static void Main(string[] l)
    {
        var n = new uint[32];
        uint i, r = 0, b;

        foreach (var v in l)
            for (i = 0, b = uint.Parse(v); b != 0; b /= 2) n[i++] += b & 1;
        b = 1;
        foreach (var v in n)
        {
            r += (v == n.Max() | v == n.Min(x => x > 0 ? x : n.Max())) ? b : 0;
            b += b;
        }
        System.Console.Write(r);
    }
}

1

Ruby,127个字节

def f l;a=[];32.times{|i|a[i]=0;l.each{|n|;a[i]+=n[i]}};a.map{|b|a.reject{|c|c==0}.minmax.index(b)?1:0}.reverse.join.to_i 2;end

将数组作为输入。


1

CoffeeScript,194个字节

将输入作为逗号分隔的二进制数,将输出二进制。

a=(s.split('').reverse()for s in prompt().split(','))
for i in b=[0..31]
 b[i]=0
 b[i]+=+c[31-i]||0for c in a
m=1/M=0
for d in b
 M=d if d>M;m=d if m>d&&d
alert +(+(d in[m,M])for d in b).join ''

试试吧。


1

GolfScript,46个字节

~{2base-1%}%zip{{+}*}%-1%.$0-)\1<|`{&,}+%2base

Web GolfScript上在线尝试。

测试用例

$ golfscript biplex.gs <<< '[1]'
1
$ golfscript biplex.gs <<< '[2]'
2
$ golfscript biplex.gs <<< '[1 2 5]'
7
$ golfscript biplex.gs <<< '[4294967295]'
4294967295
$ golfscript biplex.gs <<< '[2454267026 2863311530 3681400539]'
3817748707
$ golfscript biplex.gs <<< '[2341103945 2969112506 1849078949 1430639189]'
3

1

C ++,192字节

接受输入一个32位无符号整数数组,以及该数组中的项数。

typedef unsigned long U;U b(U*a,int c){U x[32],t,i,j,M=0,m=c,r=0;for(i=0;i<32;x[i++]=t,m=t&&m>t?t:m,M=M<t?t:M)for(t=j=0;j<c;)t+=a[j++]>>i&1;for(i=0;i<32;++i)r|=(x[i]==m||x[i]==M)<<i;return r;}

松散

unsigned long b(unsigned long*a, int c){
    unsigned long x[32],t,i,j,M=0,m=c,r=0;
    for(i=0;i<32;x[i++]=t){
        m = (t && m > t)?t:m;
        M = M < t ? t:M;
        for(t=j=0;j<c;){
            t+=a[j++]>>i&1;
        }
    }
    for(i=0;i<32;++i)
        r|=(x[i]==m||x[i]==M)<<i;
    return r;
}

1
@trichoplax我更改了它。
MegaTom,2015年
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.