谁可以缩短/修改/简化 Scilab 代码(和 JavaScript 代码)?搜索多余的代码

Who can shorten / modify / simplify Scilab code ( and JavaScript code)? Search for excess in code

Scilab 6.0.2

    function [a, ka] = srty(a, b, orien)
    // returns a values which are not in b


    // orien="r"|"c" added, including the hypermat case

    rhs = argn(2);

    // CHECKING INPUT ARGUMENTS
    // ========================
    if rhs < 2 | rhs > 3 then
        msg = gettext("%s: Wrong number of input argument(s): %d or %d expected.\n");
        error(msprintf(msg, "setdiff", 2, 3));
    end
    // Trivial case _whatever is b_
    if a==[]
        ka = []
        return
    end
    // orien
    if ~isdef("orien","l") then
        orien = 0
    elseif orien~="r" & orien~="c" & orien~=1 & orien~=2
        msg = gettext("%s: Argument #%d: Must be in the set {%s}.\n");
        error(msprintf(msg, "setdiff", 3, "''r'',''c'',1,2"));
    elseif orien=="c"
        orien = 2
    elseif orien=="r"
        orien = 1
    end
    if orien==1 & size(a,2)~=size(b,2) then
        msg = _("%s: Arguments #%d and #%d: Same numbers of columns expected.\n")
        error(msprintf(msg, "setdiff", 1, 2))
    end
    if orien==2 & size(a,1)~=size(b,1) then
        msg = _("%s: Arguments #%d and #%d: Same numbers of rows expected.\n")
        error(msprintf(msg, "setdiff", 1, 2))
    end

    // PROCESSING
    // ==========
    // "r" or "c"
    // ----------
    if orien then
        it = inttype(a)
        if ndims(a)>2 then
            a = serialize_hypermat(a, orien)
        elseif orien==2
            a = a.'
        end
        // Trivial case
        if b == [] then
            ka = 1:size(a,orien);
            if orien==1
                ka = ka'
            end
            return
        end
        if ndims(b)>2 then
            b = serialize_hypermat(b, orien)
        elseif orien==2
            b = b.'
        end
        [a, ka] = unique(a, "r")
        b = unique(b, "r")
        [c, kc] = gsort([[a iconvert(ones(a(:,1)),it)] ;
                         [b iconvert(ones(b(:,1))*2,it)]], "lr","i")
        k = find(or(c(1:$-1,1:$-1)~=c(2:$,1:$-1),"c") & c(1:$-1,$)==1)
        if c($,$)==1
            k = [k size(c,1)]
        end
        ka = ka(kc(k))
        // a = a(ka,:) // in initial order
        a = c(k,1:$-1)
        if orien==2
            ka = ka'
            a = a.'
        end
    else
        // by element
        // ----------
        [a,ka] = unique(a);
        na = size(a,"*");

        b = unique(b(:));

        [x,k] = gsort([a(:); b], "g", "i");
        d = find(x(2:$)==x(1:$-1));  //index of common entries in sorted table
        if d <> [] then
            k([d;d+1]) = [];
        end

        keep = find(k <= na);
        a = a(k(keep));
        ka = ka(k(keep));
    end
   endfunction

 function h = serialize_hypermat(h, orien)
    if orien==1 then
        dims = 1:ndims(h)
        dims([1 2]) = [2 1]
        h = permute(h, dims)
    end
    h = matrix(h, size(h,1), -1).
endfunction
a = [1,2,3,5,8];
b = [0,1,3,4,8,9,10];
k = 3; //indicated number
G = srty(a,b)
disp(G(G>k)) //Shows a number greater than the given

我正在尝试构建一种算法,用于确定属于集合 A\B(差值 A-B)且超过设定值的元素数量。

例如: A = {1,2,3,5,8}, B = {0,1,3,4,8,9,10} 设值为3。A \ B = {2,5},但有是一个元素 大于3。这个值为5。

如果有人知道这个作业的另一种解决方案,我不会拒绝帮助。


我也有 JavaScript 代码,没有搜索大于给定的数字。

JavaScript

function diff_sort_arr(array_1,array_2)
{
    var n = array_1.length, m = array_2.length, i = 0, k = 0, j = 0, array_3 = [];
    while ((i < n) && (j < m)) // until we reached the end of the array 
    {
        if (array_1[i] == array_2[j])
        { 
            i++,j++;
        } else {
            if (array_1[i] < array_2[j]) {
                array_3[k] = array_1[i];
                k++;
                i++; // shift position in the first array
            } else {
                j++; // move the position in the second array
            }
        }
    }
    while (i < n) {
        array_3[k] = array_1[i];
        k++, i++;
    }
    return array_3;
} 


diff_sort_arr([1,2,3,5,8], [0,1,3,4,8,9,10]); // at the exit [2, 5]

如果 setdiff 代码(您粘贴在上面)没有帮助,请查看以下讨论:An algorithm to find the difference of two set A and B with size n

对于Javascript

if array_1[i] < set_value

skip it

function diff_sort_arr(array_1,array_2, set_value)
 {
     var n = array_1.length, m = array_2.length, i = 0, k = 0, j = 0, array_3 = [];
     while ((i < n) && (j < m)) // until we reached the end of the array 
     {
         if (array_1[i] == array_2[j] || array_1[i]<set_value)
         { 
             i++,j++;
         } else {
             if (array_1[i] < array_2[j]) {
                 array_3[k] = array_1[i];
                 k++;
                 i++; // shift position in the first array
             } else {
                 j++; // move the position in the second array
             }
         }
     }
      let ret_arr=[];
     while (i < n) {
        array_3[k] = array_1[i];
        k++, i++;
     }
     return array_3;
 } 


 console.log(diff_sort_arr([1,2,3,5,8,10,12], [0,1,3,4,8,9,10], 3)); // at the exit [2, 5]

已测试工作 SciLab 代码

函数[a, ka] = srty(a, b, orien) // returns a 值不在 b

    // orien="r"|"c" added, including the hypermat case

    rhs = argn(2);

    // CHECKING INPUT ARGUMENTS
    // ========================
    if rhs < 2 | rhs > 3 then
        msg = gettext("%s: Wrong number of input argument(s): %d or %d expected.\n");
        error(msprintf(msg, "setdiff", 2, 3));
    end
    // Trivial case _whatever is b_
    if a==[]
        ka = []
        return
    end
    // orien
    if ~isdef("orien","l") then
        orien = 0
   end

    // PROCESSING
    // ==========
    // "r" or "c"
    // ----------

        // by element
        // ----------
    [a,ka] = unique(a);
    na = size(a,"*");

    b = unique(b(:));

    [x,k] = gsort([a(:); b], "g", "i");
    d = find(x(2:$)==x(1:$-1));  //index of common entries in sorted table
    if d <> [] then
        k([d;d+1]) = [];
    end

    keep = find(k <= na);
    a = a(k(keep));
    ka = ka(k(keep));

   endfunction

 function h = serialize_hypermat(h, orien)
    if orien==1 then
        dims = 1:ndims(h)
        dims([1 2]) = [2 1]
        h = permute(h, dims)
    end
    h = matrix(h, size(h,1), -1)
endfunction
a = [1,2,3,5,8,10,12];
b = [0,1,3,4,8,9,10];
k = 3; //indicated number
G = srty(a,b)
H = G(G>k)
disp(H) //Shows a number greater than the given

SciLab 输出

--> G = srty(a,b) G =

    1. 12.

--> H = G(G>k) H =

  1. 12.

--> disp(H) //Shows a number greater than the given

  1. 12.