\begingroup

你是一名建筑师,你的任务是审查同事的摩天大楼设计。在你的工作中,摩天大楼只是一座积木塔。仅此而已。

这意味着你的工作非常简单!你可以用一串数字来描述摩天大楼,例如11102。数字从左到右读取,每个数字指定该位置的块是什么类型的材料。

然而,摩天大楼也需要稳定。通过确保满足以下所有条件,你可以实现稳定性:

  • 0不能夹在两个相加为偶数的材料之间。如果0位于摩天大楼的顶部/底部,则地面/天空也应如此0
  • 12两侧均不得触碰它。
  • 2只能在摩天大楼中占据偶数位置。

假设底部块(即最左边的数字)位于位置/索引000

输入

您将收到摩天大楼的设计,即数字字符串/数组。您可以以任何适合您的方式接收输入。

输出

以你喜欢的格式输出所有违反条件的块的位置。这些位置不必按顺序排列,但你必须正确找到所有位置。

规则

这是,因此最短的字节数获胜。

测试用例

101          [1] (0 is sandwiched between 1+1=2)
201102       [5] (the rightmost 2 is at an odd position)
1012         [1, 2, 3] (0 is sandwiched, rightmost 1 touching 2, 2 at odd position)
0            [0] 0 is sandwiched between the ground and sky (0+0)
20101010002  [3, 5, 8, 9]
111020100102 [11]
20110010201  []
01102011100111100111 []
20111100102011102010011 []

\endgroup

6

  • 2
    \begingroup
    我们可以输出 1 索引的索引吗?
    \endgroup


    – 

  • \begingroup
    @emanresuA 是的,没问题 – 只要在你的回答中这么说就可以了。
    \endgroup


    – 

  • \begingroup
    如果我们使用 1 索引输出,那么即使对于 ,这是否会改变考虑的位置2?换句话说,对于 0 索引20将是有效的。对于 1 索引,我们是否认为它是无效的并且是02有效的?
    \endgroup


    – 

  • 1
    \begingroup
    索引是否需要按顺序输出?
    \endgroup


    – 

  • 1
    \begingroup
    @Xcali 不,不应该有变化。不过,您可以按任何顺序输出索引,编辑了原始帖子
    \endgroup


    – 


11 个回答
11

\begingroup

,21

2e;SḂj0Ḋƭ
Ø0jÇ3Ƥ⁸ịƑ"T

一个完整的程序,接受块列表(来自{ 0 , 1 , 2 }{012}\{0,1,2\}) 并打印错误块的1 索引列表

或者参见。(这会为每个输入调用两次代码,只是为了确保ƭ在奇数长度输入之后处于正确状态(因此上面有“完整程序”的限定))

如何?

2e;SḂj0Ḋƭ - Helper link: three-slice of blocks, T
2e        - does two exist in {T}?
   S      - sum {T}
  ;       - concatenate -> [2 in T?, sum T]
    Ḃ     - mod two -> [2 in T? % 2, sum T % 2]  (Note: 2 in T? % 2 = 2 in T)
        ƭ - on successive calls alternate between:
      0   -   zero
       Ḋ  -   dequeue {T}
     j    - join -> [2 in T?, 0 / 2nd element, ...junk...,  Sum T % 2]
                       ^        ^                             ^
                    (1-case   2-case                        0-case)

Ø0jÇ3Ƥ⁸ịƑ"T - Main Link: list of {0,1,2}, Blocks
Ø0          - [0,0]
  j         - join with {Blocks} -> 0-padded-blocks
    3Ƥ      - for each three-slice, from left to right:
   Ç        -   call the helper link -> LookupArray
      ⁸     - chain's left argument -> Blocks
         "  - zip with - f(Block, respective LookupArray):
        Ƒ   -   is {Block} invariant under?:
       ị    -     {Block} index into {LookupArray} (1-indexed & modular)
          T - truthy indices

\endgroup

\begingroup

Uiua,43 个字符

⊚↥⊃(≡(⨬(¬◿2/+|/↥⌕2|0)⊸⊡1)◫3↻1⊂0_0|↧◿2⇡⧻⤙=2)

\endgroup

\begingroup

55 48 43 字节

^|$
0
Iv`101|[02]0[02]|21|.12|(?<!^(..)*).2

在单独的行上输出不稳定位置,但链接指向测试套件,为了方便起见,该套件以逗号连接。说明:0为了方便起见,摩天大楼被包裹在 s 中,然后I命令会自动列出匹配的位置,而v修饰符允许匹配重叠,从而避免向前看,因此只需匹配不稳定位置,从现在的前一个字符开始,该字符位于所需位置。

\endgroup

\begingroup

47 43

⎕IO=0

{⍸((⍵=2)×2|⍳⍴⍵)∨{(~2|+/⍵)(2∊⍵)0[⍵[1]]}⌺3⊢⍵}

在每个元素周围创建一个 3 情况模板(这些元素用零填充)。索引到数组中,计算 3 的总和是否为奇数(0 是这里的单位元素),对于 0,检查是否有任何元素为 2(我们知道中间的元素是 1)。在此阶段不对 2 进行检查(此时索引不可用)。然后OR使用第一个检查进行第二个检查,我们计算索引的范围,并保留 2 的位置。

一旦我们有了布尔向量,就可以使用⍸来获取真实位置。

-4 感谢@asherbhs。

\endgroup

1

  • \begingroup
    默认 1-索引
    \endgroup


    – 


\begingroup

JavaScript(ES10),60 字节

期望一个数字数组。

a=>a.flatMap(p=(v,i)=>[~p^(q=a[i+1]),(p|q)/2,i][p=v]&1?i:[])

评论

a =>                // a[] = input array
a.flatMap(p =       // initialize p to a zero'ish value
(v, i) =>           // for each value v at index i:
  [                 //   lookup array:
    ~p ^            //     0: (NOT predecessor) XOR
    (q = a[i + 1]), //        successor
    (p | q) / 2,    //     1: (predecessor OR successor) / 2
    i               //     2: just use i
  ][p = v]          //   get the result at index v and copy v to p
  & 1               //   test the LSB
  ?                 //   if it's set:
    i               //     return the position
  :                 //   else:
    []              //     discard this entry
)                   // end of flatMap()

\endgroup

\begingroup

-n 5,79 字节

($&-2||pos()%2)&&say pos while /2\K1|1(?=2)|([02]|^\K)0(?=[20]|$)|1\K0(?=1)|2/g

输出为 1 索引

\endgroup

\begingroup

,20字节

0pǏ3lƛ∑₂n2c¥&¬W;¨£iT

0pǏ                  # Prepend and append a 0
   3lƛ         ;     # Over the subarrays of length 3...
              W      # Construct a list of
      ∑₂             # 0 -> Sum even
        n2c          # 1 -> Contains a 2
           ¥&¬       # 2 -> Whether the current index is even
                ¨£i  # Index the original values into each list
                   T # And get the truthy indices of this.

\endgroup

\begingroup

,33字节

I⌕AEEθ✂⪫00θκ⁺³κ¹&¹§⟦⊕Σι⁼2⌈ικ⟧§θκ¹

链接为代码的详细版本。说明:@Arnauld 的 JavaScript 答案的移植。

     θ                              Input string
    E                               Map over characters
        00                          Literal string `00`
       ⪫                            Joined with
          θ                         Input string
      ✂        ¹                    Sliced from
           κ                        Current index to
              κ                     Current index
            ⁺                       Plus
             ³                      Literal integer `3`
   E                                Map over windows
                      ι             Current window
                     Σ              Sum of digits
                    ⊕               Incremented
                          ι         Current window
                         ⌈          Maximum
                       ⁼            Equals
                        2           Literal string `2`
                           κ        Current index
                   ⟦        ⟧       Make into list
                  §                 Indexed by
                              θ     Input string
                             §      Indexed by
                               κ    Current index
                &                   Bitwise And
                 ¹                  Literal integer `1`
 ⌕A                                 Find indices of
                                ¹   Literal integer `1`
I                                   Cast to string
                                    Implicitly print

\endgroup

\begingroup

2,10491字节

b=input()+[-2]*2
for i,j in enumerate(b):l=b[i-1:i+2];print`i`*[~sum(l)&1,2in l,i&1,0,0][j]

\endgroup

1

  • 1
    \begingroup

    \endgroup


    – 

\begingroup

398 183168 字节

节省了 230 字节,感谢 @Malo


高尔夫版。

def p(b):
 a=[]
 b=[0,0]+b+[0]
 for i,v in enumerate(b[2:-1]):
  n=b[i+3]
  p=b[i+1]
  if v==0:c=(~p^n)
  elif v==1:c=(p|n)//2
  elif v==2:c=i
  if c&1:a+=[i]
 return a

非高尔夫版本。

def process_array(input_array):
    # Use list comprehension with conditional filtering (similar to flatMap)
    result = []
    for current_index, current_value in enumerate(input_array):
        # Get the next value in the array (None if beyond array bounds)
        next_value = input_array[current_index + 1] if current_index + 1 < len(input_array) else None
        
        # Store the previous value for the next iteration
        previous_value = 0 if current_index == 0 else input_array[current_index - 1]
        
        # Determine which operation to perform based on current value
        if current_value == 0:
            # NOT of previous value XOR with next value
            # Using bitwise operators and handling None case
            next_val = 0 if next_value is None else next_value
            computed_result = (~previous_value ^ next_val)
        elif current_value == 1:
            # (previous value OR next value) divided by 2
            next_val = 0 if next_value is None else next_value
            computed_result = (previous_value | next_val) // 2
        elif current_value == 2:
            # Just return the current index
            computed_result = current_index
        else:
            computed_result = 0
        
        # Check if the least significant bit is set (result is odd)
        if computed_result & 1:
            result.append(current_index)
            
    return result

# Test cases
test_cases = [
    {"input": "101", "expected": [1]},
    {"input": "201102", "expected": [5]},
    {"input": "1012", "expected": [1, 2, 3]},
    {"input": "0", "expected": [0]},
    {"input": "20101010002", "expected": [3, 5, 8, 9]},
    {"input": "111020100102", "expected": [11]},
    {"input": "20110010201", "expected": []},
    {"input": "01102011100111100111", "expected": []},
    {"input": "20111100102011102010011", "expected": []}
]

# Run test cases
for case in test_cases:
    # Convert string to array of numbers
    input_array = [int(x) for x in case["input"]]
    
    # Process the array
    result = process_array(input_array)
    
    # Compare with expected result
    is_correct = result == case["expected"]
    
    # Output result and validation
    print(f"{result} {'✔️' if is_correct else '❌'}")

\endgroup

2

  • \begingroup
    183 字节: – 将 n .. else None 替换为 n .. else 0 。 Final else:c=0 没用。 将 elif v==2 替换为 else 。 将 a.append(i) 替换为 a+=[i]
    \endgroup


    – 


  • \begingroup
    168字节:
    \endgroup


    – 

\begingroup

,22

0.øü3εOÈy2åNÉ)yÅsè}ƶ0K

输入为数字列表;输出列表以 1 为基础。

解释:

0.ø            # Surround the (implicit) input-list with leading/trailing 0
   ü3          # Pop and get all overlapping triplets of this list
     ε         # Map over each triplet:
      O        #  Sum the triplet
       È       #  Check whether this sum is even
      y        #  Push the triplet again
       2å      #  Pop and check whether it contains a 2
      N        #  Push the 0-based map-index
       É       #  Check whether this index is odd
         )     #  Wrap all three checks into a list
          y    #  Push the triplet again
           Ås  #  Pop and leave its middle
             è #  Use that to 0-based index into the earlier list
     }ƶ        # After the map: multiple all checks by their 1-based index
       0K      # Then remove all 0s
               # (after which this is output implicitly as result)

\endgroup