ऐरे एकीकरण


24

परिचय

एक ही लंबाई के दो सरणियों पर विचार करें, कहते हैं A = [0,1,0,2]और B = [-1,1,2,2]। मान लीजिए कि हम जानते हैं कि उनकी सामग्री कुछ अर्थों में समान है, आइटम द्वारा आइटम:

  • 0के बराबर है -1,
  • 1के बराबर है 1,
  • 0के बराबर है 2, और
  • 2के बराबर है 2

तुल्यता सकर्मक है: -1और 0बराबर हैं, और 0और 2इसलिए बराबर हैं, -1और 2भी बराबर हैं। एकीकरण की Aऔर Bसरणी जहां से प्रत्येक मद है 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
मुझे पूरा यकीन नहीं है कि आपने उस ऑपरेशन को एकीकरण क्यों कहा।
9

4
@ फ़ेटलाइज़ मैं टाइप एकीकरण से प्रेरित हुआ ।
जर्बर्ग

जवाबों:


6

जावास्क्रिप्ट (ईएस 6), 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

@ETHproductions यप। एक बेहतर तरीका हो सकता है। हल्के से दिलचस्प तथ्य: पहले दो a.mapको भी साथ b.mapही बदला जा सकता है ।
Arnauld

मैंने आपकी स्थिति के लिए एक नया परीक्षण मामला जोड़ा।
जर्बग

5

सीजेएम , 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

पायथन, 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 एक शब्दकोश getविधि के । फिर, सूचियों को स्वैप करता है, और तब तक दोहराता है जब तक वे समान न हों।


2

पायथ, 13 बाइट्स

eMumS{s@#dGGC

इसे ऑनलाइन आज़माएँ: प्रदर्शन

स्पष्टीकरण:

प्रत्येक जोड़ी से शुरू करें। अतिव्यापी सूचियों के साथ प्रत्येक जोड़ी (सूची) का विस्तार करें, तत्वों को काटें और सॉर्ट करें। एक बार इस प्रक्रिया को परिवर्तित करने से रोकें। प्रत्येक सूची का अधिकतम प्रिंट लें।


2

पीएचपी, 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

मैथेमेटिका, 56 बाइट्स

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

0

जावा, 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

पायथन, 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)

व्याख्या

दोनों सरणियों ( aऔर bइस मामले में) में प्रत्येक अद्वितीय तत्व के अनुरूप मूल्यों की एक तालिका बनाएं । उदाहरण के लिए यदि

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

तब तालिका यह होगी:

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

फिर गहराई से पहले खोज लागू करें, इसलिए, उदाहरण के लिए, मान लें कि मैं aमूल्य में सबसे बाएं तत्व को चुनता हूं 0और उसके 0समतुल्य हैं: 0और 2। चूंकि 0पहले ही दौरा किया जा चुका है, पर जाएं 2। 2 equivalences है: 0। तो सबसे बाएं तत्व को चुनने के लिए सबसे अच्छा परिणाम aहै 2। यहाँ पेड़ है:

   0   
 /   \
0     2
       \
        0

और आप वहां सबसे बड़ा मूल्य लेना चाहते हैं, इसलिए इसका परिणाम है 2


PPCG में आपका स्वागत है! में कोड गोल्फ , आप अपने कार्यक्रम में कम से कम bytecount संभव पाने के लिए करना है। इसका अर्थ है फ़ंक्शन और चर नामों को छोटा करना और अपने प्रोग्राम में अनावश्यक व्हाट्सएप को हटाना।
कृति लिथोस

आपको हार्ड-कोडिंग के बजाय दो सरणियों को उपयोगकर्ता इनपुट के रूप में लेना चाहिए।
जर्गब

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

जावा, 170 बाइट्स

golfed

(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;}

Ungolfed

(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[]

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.