数组统一


24

介绍

考虑两个相同长度的数组,例如A = [0,1,0,2]B = [-1,1,2,2]。假设我们知道它们的内容在某种意义上是逐项相等的:

  • 0等于-1
  • 1等于1
  • 0等价于2
  • 2等同于2

等价是可传递的:-10等价,并且02等价,所以-12也等价。的统一AB是其中的每一个项目的阵列A(或B)已取代这相当于它的数量最多。在这种情况下,统一为[2,1,2,2]

任务

编写一个程序或函数,该程序或函数采用两个相等长度的非空整数数组,并输出它们的统一。您也可以就地修改输入之一,而不用返回。最低字节数获胜。

测试用例

[0] [0] -> [0]
[1] [2] -> [2]
[0,-1] [-1,-1] -> [0,0]
[0,1,0] [2,1,0] -> [2,1,2]
[1,2,3] [0,0,1] -> [3,3,3]
[0,1,0,2] [-1,1,2,2] -> [2,1,2,2]
[1,0,1,-4] [-3,-1,-2,2] -> [1,0,1,2]
[1,2,3,-2] [1,0,-3,-2] -> [1,2,3,-2]
[-3,-2,-1,0,1] [-1,-1,-1,-1,-1] -> [1,1,1,1,1]
[-3,-2,-1,0,1] [2,-1,0,1,-3] -> [2,2,2,2,2]
[-3,5,5,3,1] [4,2,3,1,2] -> [4,5,5,5,5]
[4,0,2,-5,0] [0,4,-5,3,5] -> [5,5,3,3,5]
[-2,4,-2,3,2,4,1,1] [-2,4,1,2,2,3,1,-2] -> [1,4,1,4,4,4,1,1]
[-10,-20,-11,12,-18,14,-8,-1,-14,15,-17,18,18,-6,3,1,15,-15,-19,-19] [-13,6,-4,3,19,1,-10,-15,-15,11,6,9,-11,18,6,6,-5,-15,7,-11] -> [-8,14,18,14,19,14,-8,-1,-1,15,14,18,18,18,14,14,15,-1,18,18]
[20,15,2,4,-10,-4,-19,15,-5,2,13,-3,-18,-5,-6,0,3,-6,3,-17] [-18,7,6,19,-8,-4,-16,-1,13,-18,8,8,-16,17,-9,14,-2,-12,7,6] -> [20,15,20,19,-8,-4,20,15,17,20,17,17,20,17,-6,14,15,-6,15,20]

3
我不太确定为什么您将这种操作称为统一。
致命

4
@Fatalize我受到类型统一的启发。
Zgarb '16

Answers:


6

JavaScript(ES6),100 90 110 102 96字节

a=>b=>a.map(v=>t[v],a.map((_,k)=>a.map((x,i)=>t[x]=t[y=b[i]]=Math.max(k?t[x]:x,k?t[y]:y)),t={}))

我最初的解决方案是90个字节:

a=>b=>a.map(v=>t[v],a.map(_=>a.map((x,i)=>t[x]=t[y=b[i]]=Math.max(t[x]||x,t[y]||y)),t={}))

尽管它通过了所有提供的测试用例,但由于以下原因而失败:

A = [0, -1], B = [-1, -1]

测试用例


太多了a.map……
ETHproductions'Nov

@ETHproductions是的。可能有更好的方法。有趣的事实:前两个a.map也可以替换b.map为。
Arnauld

我为您的情况添加了一个新的测试用例。
Zgarb '16

5

CJam,27个字节

l~_])\z_,*f{{_2$&,*|}/:e>}p

在线尝试! 测试套件。

说明

l~       e# Read and evaluate input, dumping arrays A and B on the stack.
_        e# Copy B.
])\      e# Wrap in array, pull off B, swap. Gives B [A B] on the stack.
z        e# Transpose the [A B] matrix to get a list of all equivalent pairs.
_,*      e# Repeat this list by the number of pairs. This is to ensure that the
         e# following procedure is applied often enough to allow transitive
         e# equivalences to propagate.
f{       e# Map this block over B, passing in the list of pairs each time...
  {      e#   For each pair...
    _2$  e#     Copy both the pair and the current value/list.
    &,   e#     Get the length of their intersection. If this is non-zero,
         e#     the current pair belongs to the current equivalence class.
    *    e#     Repeat the pair that many times.
    |    e#     Set union between the current value/list and the repeated pair.
         e#     This adds the pair to the current list iff that list already
         e#     contains one value from the pair.
  }/
  :e>    e#   Get the maximum value of this equivalence class.
}
p        e# Pretty print.


4

Python,86个字节

f=lambda a,b:a*(a==b)or f(*[map({x:y for x,y in zip(a,b)if x<y}.get,x,x)for x in b,a])

同时通过用第二个列表中的相应元素(如果较大)替换第一个列表中的每个值来更新两个列表。替换是使用map字典的getmethod完成的。然后,交换列表,并重复进行直到它们相等为止。


2

Pyth,13个字节

eMumS{s@#dGGC

在线尝试:演示

说明:

从每对开始。用重叠的列表迭代扩展每对(列表),对元素进行重复数据删除并排序。一旦此过程收敛,请停止。打印每个列表的最大值。


2

PHP,266个 241 213 200字节

解:

function u($x,$y){foreach($x as$i=>$j){$k[$y[$i]][]=$j;$k[$j][]=$y[$i];}$h=function($c,&$w)use($k,&$h){$w[]=$c;foreach($k[$c]as$u)!in_array($u,$w)&&$h($u,$w);return max($w);};return array_map($h,$x);}

用法:u([1,2,3], [0,0,1]);返回所需的数组。

不太打高尔夫球:

function unify($x, $y)
{
    foreach($x as $i=>$j) {
        $k[$y[$i]][] = $j;
        $k[$j][] = $y[$i];
    }

    $h = function ($c, &$w=[]) use ($k, &$h) {
        $w[] = $c;
        foreach($k[$c] as $u)
            !in_array($u, $w) && $h($u, $w);
        return max($w);
    };

    return array_map($h, $x);
}


1

Mathematica,56个字节

#/.($|##->Max@##&@@@ConnectedComponents@Thread[#<->#2])&

0

Java中,273个 263字节

interface Z{int z(int x);default Z g(int m,int n){return x->{for(int t;x!=(t=x==m?z(n):z(x));)x=t;return x;};}static void f(int[]a,int[]b){Z y=x->x;int i=0,v;for(int u:a){u=y.z(u);v=y.z(b[i++]);if(u<v)y=y.g(u,v);if(v<u)y=y.g(v,u);}i=0;for(int u:a)a[i++]=y.z(u);}}

该方法f(int[]a,int[]b)解决了挑战。

interface Z{

  //should return an "equivalent" integer
  int z(int x);

  //return a Z lambda where the 1st arg is "equivalent" to the 2nd arg
  default Z g(int m,int n){
    return x->{
      for(int t;x!=(t=x==m?z(n):z(x));) //always find the last equivalent number for x
        x=t;
      return x;
    };
  }

  //solve the challenge
  static void f(int[]a,int[]b){
    Z y=x->x; //start off with all numbers only equivalent only to themselves
    int i=0,v;
    for(int u:a){
      u=y.z(u); //get a's element's equivalent number
      v=y.z(b[i++]); //get b's element's equivalent number
      if(u<v)y=y.g(u,v); //if a's was smaller than b's, make a's equivalent to b's
      if(v<u)y=y.g(v,u); //if b's was smaller than a's, make b's equivalent to a's
    }
    i=0;
    for(int u:a) //overwrite a with each element's equivalent value
      a[i++]=y.z(u);
  }

}

首先遍历两个数组,并跟踪等效数字。然后修改第一个数组中的每个元素以存储等效数字。


0

Python,522字节

a = [-2,4,-2,3,2,4,1,1]
b = [-2,4,1,2,2,3,1,-2]
m = {}
visited = {}
for i in range(len(a)):
    if a[i] in m:
        if b[i] not in m[a[i]]:
            m[a[i]].append(b[i])
    else:
        l = []
        l.append(b[i])
        m[a[i]] = l
    if b[i] in m:
        if a[i] not in m[b[i]]:
            m[b[i]].append(a[i])
    else:
        l = []
        l.append(a[i])
        m[b[i]] = l

def dfs(v, maximum):
    if v > maximum:
        maximum = v
    visited[v] = True
    for n in m[v]:
        if not visited[n]:
            d = dfs(n, maximum)
            if d > v:
                maximum = d
    return maximum

result = []
for k in range(len(a)):
    for q in m:
        visited[q] = False
    result.append(max(dfs(a[k], a[k]), dfs(b[k], b[k])))

print(result)

说明

使在两个阵列(对应于每一个独特的元件值的表ab在这种情况下)。例如,如果

a = [0,1,0] 
b = [2,1,0]

那么该表将是:

0:[0,2]
1:[1]
2:[0]

然后应用深度优先搜索,因此,例如,假设我选择的a是值中最左边的元素,0并且0具有等价关系:02。既然0已经访问过,请转到2。2具有等效项:0。因此,选择中最左边的元素的最佳结果a2。这是树:

   0   
 /   \
0     2
       \
        0

并且您要在其中取最大值,因此结果是2


欢迎来到PPCG!在code-golf中,您旨在获得程序中可能的最短字节数。这意味着缩短函数和变量名并删除程序中不必要的空格。
Kritixi Lithos

您还应该将这两个数组作为用户输入,而不是对其进行硬编码。
Zgarb

0

PHP,132字节

function(&$a,$b){for(;$i<count($a);$i++){$r=$a[$i];$s=$b[$i];$r<$c[$s]?:$c[$s]=$r;$s<$c[$r]?:$c[$r]=$s;}foreach($a as&$v)$v=$c[$v];}

带有两个数组的匿名函数。

这是我对“在现场修改其中一个数组”的看法,正如挑战输出中所指定的那样。这将遍历两个数组中的每个数组,如果当前数组大于存储的数组,则记录其等效性,然后遍历第一个数组,并用它们的最大等效值替换所有值。第一个数组由引用获取(因此为&$a),因此传入的数组被“就地”修改。


0

Java,170字节

打高尔夫球

(a,b)->{int[]d=a.clone();for(int i=0,y;i<d.length;i++){y=0;for(int j=0;j<a.length;j++)if(a[j]==d[i]||b[j]==d[i])y=Integer.max(y,Integer.max(a[j],b[j]));d[i]=y;}return d;}

不打高尔夫球

(a, b) -> {                                        // Two argument lambda
    int[] d = a.clone();                           // We clone our first array for modification
    for (int i = 0,y; i < d.length; i++) {         // Going through the elements of d...
        y = 0;                                     // We initialize our 'highest' equivalent
        for (int j = 0; j < a.length; j++) {       // Going through each of our arrays (a and b)...
            if (a[j] == d[i] || b[j] == d[i]) {    // If either of them is the number we're trying to match for equivalence...
                y = Integer.max(y, Integer.max(a[j], b[j])); // We see if the new number is bigger than the largest we've found.
            }
        }
        d[i] = y;                                  // We then assign the largest equivalent number for the current position in our output array.
    }
    return d; // And return!
}

int[]带有两个s作为参数并返回的匿名函数int[]

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.