Algorithm for matching all elements with another element in the same list, where some have limitations

This array [a, b, c, d, e, f]

I want to match each letter with every other letter except myself, resulting in something like:

  • a - c
  • b - f
  • d - e

The trick is that each letter can be limited by the coincidence of one or more other letters.

So let's say for example

  • a cannot be matched with c, d
  • c cannot be matched with e, f
  • e cannot be matched with

Any directions on how to do this? I use Ruby, but any pseudo-code will be useful.

Thank!

+3
source share
4 answers

, , - , (, , ). , .

.

+5

, . .

  • .
  • , , .
  • . , , .
  • , .

, , , . , , , - .

+1

I'm not sure I understand the problem, but this seems like a question:

%w[a b c d e f].combination(2).to_a - [%w[a c],%w[a d],%w[c e],%w[c f],%w[e a]]
# =>  [["a", "b"], ["a", "e"], ["a", "f"], ["b", "c"], ["b", "d"], ["b", "e"], ["b", "f"], ["c", "d"], ["d", "e"], ["d", "f"], ["e", "f"]]
0
source

$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
$exclusions = array('a' => array('e', 'd', 'c'), 'b' => array('a','b', 'c','d')); foreach ($letters as $matching) {
foreach ($letters as $search) {
if(!in_array($search,$exclusions[$matching])){
if($search!=$matching){
$match[$matching][] = $search;
}
}
}
}
print_r ($ match);

The innermost EVAL can be added to the next outer ...

you can see it in action at http://craigslist.fatherstorm.com/stackoverflow2.php

-1
source

Source: https://habr.com/ru/post/1771571/


All Articles