Home > Software engineering >  Finding out if two Lists have some elements in common by only using map function
Finding out if two Lists have some elements in common by only using map function

Time:10-21

I am working on a Problem in Maple. But the question i have is general and could be answered based on ANY programming Language.

The Problem:

  • Use only map function (no loops). Assume there are two lists A and B with n and m elements. replace those elements in A with "true", which are also found in B and replace the other elements in A with "false".

The question: i did try to solve this problem like that:

funk := proc(i::integer,j::integer) # checks if two elements are identical
if (i = j) then
return true;
else
return false;
end if;
end proc:

funktion := proc(int::integer, L :: list) # calls funk on every element of L.
map(funk, L, int);
end proc:

SchnittTrueA := proc(M::list, L::list) # calls funktion on every Element of M and L.
map(funktion,M,L);
end proc:

And if i run SchnittTrueA(k,l); for k :=[1,3] and l := [2,3] i get this result:

[[false, false], [false, true]]

That is by no mean what i want the Program to do!

i need something like that [false, true]

I did try everything i could but i have no idea how to solve this problem.

P.S1: i am a newbie programmer so please do not be hard on me :D. P.S2: If you want to give me any Tip / Answer, it does not need to be in Maple Language as long as you can use map function.

CodePudding user response:

It's not clear what else you may use, besides map. Eg. your user-defined procedure uses if..then, etc.

But the following don't use loops.

The first two are reasonably efficient in the sense that for each element t in list A they generate the true as soon as any matching element is found in list B.

In contrast the last two approaches are inefficient because they test each element t in list A against every element in list B (even if a match has already been found).

restart;
A := [$1..10];

         [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

B := [seq(ithprime(i),i=1..4)];

                  [2, 3, 5, 7]

map(member,A,B);

  [false, true, true, false, true, false, true, false, false, false]

map(t->ormap(evalb@`=`,B,t),A);

  [false, true, true, false, true, false, true, false, false, false]

map(t->`or`(op(map(evalb@`=`,B,t))),A);

  [false, true, true, false, true, false, true, false, false, false]

map(t1->`if`(map(t2->`if`(t2=t1,true,NULL),B)=[true],
         true,false),A);

  [false, true, true, false, true, false, true, false, false, false]

Of the four apporaches shown above, the fourth is closest in spirit to your attempt.

I'll show some equivalents to the fourth above, each getting closer to your original attempt.

restart;
A := [$1..10]:

B := [seq(ithprime(i),i=1..4)]:

Each of these equivalents use those same A and B example list.

func := (t1,t2)->`if`(t2=t1,true,NULL):

funktion := (t1,B)->`if`(map(func,B,t1)=[true],
                     true,false):

map(funktion, A, B);

[false, true, true, false, true, false, true, false, false, false]

Now using proc instead of shorter operator syntax.

func := proc(t1,t2)
          `if`(t2=t1,true,NULL);
        end proc:

funktion := proc(t1,B)
              `if`(map(func,B,t1)=[true],
                          true,false);
            end proc:

map(funktion, A, B);

[false, true, true, false, true, false, true, false, false, false]

Now using long-form of if..then, instead of shorter if operator form.

func := proc(t1,t2)
          if t2=t1 then
            true;
          else
            NULL;
          end if;
        end proc:

funktion := proc(t1,B)
              if map(func,B,t1)=[true] then
                true;
              else
                false;
              end if;
            end proc:

map(funktion, A, B);

 [false, true, true, false, true, false, true, false, false, false]
  • Related