Question: Write a procedure whose input is a binary list and whose output is the corresponding set. E. g., list_to_set([1,0,1,1]) will return the set {1,3,4}

m:=proc(n::list)
local N, S, i:
N:=nops(n);
S:={};
for i from 1 to N do
if n[i]=1 then do
S:=S union {i}; break; od; fi; od;
for i from 1 to N do
if n[i]=0 then do
S:=S union {}; break; od; fi; od;
end proc;

 

the procedure works if the last member of a list is 0, for example 

m(1,0,1,0); 

returns {1,3}

 

but if it ends in 1, nothing gets returned, example: m(1,0,1);

I can also get it to work the other way around, where it'll return the set if the list ends in 1 but not zero. I need it to work for both.

Please Wait...