Cody

# Problem 793. Combinations without using nchoosek

Solution 122577

Submitted on 2 Aug 2012 by Jean-Marie Sainthillier
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
%% n = 3; y_correct = nchoosek(1:n,2); assert(isequal(your_fcn_name(n),y_correct)) % Prevents cheating filetext = fileread('your_fcn_name.m') assert(isempty(strfind(filetext, 'nchoosek')))

filetext = function c= your_fcn_name(n) v=1:n; if n == 2 c = v(:).'; elseif n == 2 + 1 tmp = v(:).'; c = tmp(ones(n,1),:); c(1:n+1:n*n) = []; c = reshape(c,n,n-1); else c = []; for idx = 1:n-2+1 Q = combs(v(idx+1:n),1); c = [c; [v(ones(size(Q,1),1),idx) Q]]; end end end function P = combs(v,m) %COMBS All possible combinations. % COMBS(1:N,M) or COMBS(V,M) where V is a row vector of length N, % creates a matrix with N!/((N-M)! M!) rows and M columns containing % all possible combinations of N elements taken M at a time. % % This function is only practical for situations where M is less % than about 15. v = v(:).'; n = length(v); if n == m P = v; elseif m == 1 P = v.'; else P = []; if m < n && m > 1 for k = 1:n-m+1 Q = combs(v(k+1:n),m-1); P = [P; [v(ones(size(Q,1),1),k) Q]]; end end end end %This code written by profile_id 343082

2   Pass
%% n = 25; y_correct = nchoosek(1:n,2); assert(isequal(your_fcn_name(n),y_correct)) filetext = fileread('your_fcn_name.m') assert(isempty(strfind(filetext, 'nchoosek')))

filetext = function c= your_fcn_name(n) v=1:n; if n == 2 c = v(:).'; elseif n == 2 + 1 tmp = v(:).'; c = tmp(ones(n,1),:); c(1:n+1:n*n) = []; c = reshape(c,n,n-1); else c = []; for idx = 1:n-2+1 Q = combs(v(idx+1:n),1); c = [c; [v(ones(size(Q,1),1),idx) Q]]; end end end function P = combs(v,m) %COMBS All possible combinations. % COMBS(1:N,M) or COMBS(V,M) where V is a row vector of length N, % creates a matrix with N!/((N-M)! M!) rows and M columns containing % all possible combinations of N elements taken M at a time. % % This function is only practical for situations where M is less % than about 15. v = v(:).'; n = length(v); if n == m P = v; elseif m == 1 P = v.'; else P = []; if m < n && m > 1 for k = 1:n-m+1 Q = combs(v(k+1:n),m-1); P = [P; [v(ones(size(Q,1),1),k) Q]]; end end end end %This code written by profile_id 343082

3   Pass
%% n = 112; y_correct = nchoosek(1:n,2); assert(isequal(your_fcn_name(n),y_correct)) filetext = fileread('your_fcn_name.m') assert(isempty(strfind(filetext, 'nchoosek')))

filetext = function c= your_fcn_name(n) v=1:n; if n == 2 c = v(:).'; elseif n == 2 + 1 tmp = v(:).'; c = tmp(ones(n,1),:); c(1:n+1:n*n) = []; c = reshape(c,n,n-1); else c = []; for idx = 1:n-2+1 Q = combs(v(idx+1:n),1); c = [c; [v(ones(size(Q,1),1),idx) Q]]; end end end function P = combs(v,m) %COMBS All possible combinations. % COMBS(1:N,M) or COMBS(V,M) where V is a row vector of length N, % creates a matrix with N!/((N-M)! M!) rows and M columns containing % all possible combinations of N elements taken M at a time. % % This function is only practical for situations where M is less % than about 15. v = v(:).'; n = length(v); if n == m P = v; elseif m == 1 P = v.'; else P = []; if m < n && m > 1 for k = 1:n-m+1 Q = combs(v(k+1:n),m-1); P = [P; [v(ones(size(Q,1),1),k) Q]]; end end end end %This code written by profile_id 343082

4   Pass
%% n = 2 y_correct = nchoosek(1:n,2); assert(isequal(your_fcn_name(n),y_correct)) filetext = fileread('your_fcn_name.m') assert(isempty(strfind(filetext, 'nchoosek')))

n = 2 filetext = function c= your_fcn_name(n) v=1:n; if n == 2 c = v(:).'; elseif n == 2 + 1 tmp = v(:).'; c = tmp(ones(n,1),:); c(1:n+1:n*n) = []; c = reshape(c,n,n-1); else c = []; for idx = 1:n-2+1 Q = combs(v(idx+1:n),1); c = [c; [v(ones(size(Q,1),1),idx) Q]]; end end end function P = combs(v,m) %COMBS All possible combinations. % COMBS(1:N,M) or COMBS(V,M) where V is a row vector of length N, % creates a matrix with N!/((N-M)! M!) rows and M columns containing % all possible combinations of N elements taken M at a time. % % This function is only practical for situations where M is less % than about 15. v = v(:).'; n = length(v); if n == m P = v; elseif m == 1 P = v.'; else P = []; if m < n && m > 1 for k = 1:n-m+1 Q = combs(v(k+1:n),m-1); P = [P; [v(ones(size(Q,1),1),k) Q]]; end end end end %This code written by profile_id 343082