dantevergil

15 Reputation

One Badge

8 years, 178 days

MaplePrimes Activity


These are questions asked by dantevergil

**I am trying write a procedure (bit(n,k)) that print all the binary sequences with 2 following conditions:
  - Each of the sequences has the length of 'n'. (n elements)
 - k bits of n elements have the value 1. (all the remains have the value 0)
   Example: bit(3,2) and the result is ((101),(110),(011)).
**My code:
bit := proc (n, k)
     local one, index, i, d;
     global A;
     one := 0;
     index := 1;
     A := [seq(0, i = 1 .. n)];
     d := proc (index)
        if one = k or index = n+1 then return NULL; end if;
        for i from 0 to 1 do
              A[index] := i;
              if i = 1 then one := one+1; end if;
              if one = k then print(op(A)); end if;
             d(index+1);
             if i = 1 then one := one-1; A[index] := 0; end if;
        end do;
     end proc;
     d(1);
end proc;
>bit(5,1);
result:
0, 0, 0, 0, 1
That 's not the answer for the excercise. What is the problem?

 

 Hi, I got an error (the code below) when using subsop in procedure. But when I replace "NULL" with an integer, it would work.                             

> phephoi := proc (A, B)
local check, i, j, C, E;
C := A;
E := B;
for i from 1 to nops(C) do
   check := 0; 
   for j from 1 to nops(E) do
      if E[j] = C[i] then check := 1; break; end if;
   end do; 
   if check = 0 then C := subsop(i = NULL, eval(C)); end if;
end do;
return C;
end proc;


> A := [1, 2, 3, 5, 6, 7];
B := [2, 4, 7, 11, 8];
phephoi(A, B);

                             [1, 2, 3, 5, 6, 7]
                              [2, 4, 7, 11, 8]
Error, (in phephoi) invalid subscript selector
 

Page 1 of 1