Example #1
0
 vector<vector<int> > permute(vector<int> &num) {
     // Note: The Solution object is instantiated only once and is reused by each test case.
     vector<int> used(num.size(),0);
     vector<bool> visited(num.size(),false);
     vector<vector<int> > results;
     doPermute(num,used, visited, results,0);
     return results;
 }
Example #2
0
 void doPermute(vector<int> &num, vector<int> &used, vector<bool> &visit, vector<vector<int> >&results, int level)
 {
     // base 
     if(level == num.size())
     {   
         results.push_back(used);
         return;
     }
     
     
     for(int i = 0; i < num.size(); i++)
     {
         if(!visit[i])
         {
             visit[i] = true;
             used[level] = num[i];
             doPermute(num, used, visit, results, level+1);
             visit[i] = false;
         }
         
     }
     return;
 }
Example #3
0
types::Function::ReturnValue sci_permute(types::typed_list& in, int _iRetCount, types::typed_list& out)
{
    if (in.size() != 2)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "permute", 2);
        return types::Function::Error;
    }

    if (_iRetCount != 1)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected."), "permute", 1);
        return types::Function::Error;
    }

    if (in[0]->isArrayOf() == false)
    {
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_permute";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    types::GenericType* pIn = in[0]->getAs<types::GenericType>();
    types::GenericType* pDims = in[1]->getAs<types::GenericType>();

    int iDims = pIn->getDims();
    int* piDimsArray = pIn->getDimsArray();
    int iNewDims = pDims->getSize();
    int* piNewDimsArray = NULL;
    std::vector<int> dimsVect;

    if ((iNewDims >= iDims) & pDims->isDouble() & !pDims->getAs<types::Double>()->isComplex())
    {
        // Check if 2nd argument is a permutation of [1..iNewDims]
        types::Double* pDbl = pDims->getAs<types::Double>();
        std::vector<double> sortedNewDimsVect(pDbl->get(), pDbl->get() + iNewDims);
        std::sort(sortedNewDimsVect.begin(), sortedNewDimsVect.end());
        std::vector<double> rangeVect(iNewDims);
        std::iota(rangeVect.begin(), rangeVect.end(), 1.0);

        if (sortedNewDimsVect == rangeVect)
        {
            piNewDimsArray = new int[iNewDims];
            for (int i = 0; i < iNewDims; i++)
            {
                int j = (int)pDbl->get(i);
                piNewDimsArray[i] = 1;
                if (j <= iDims)
                {
                    piNewDimsArray[i] = piDimsArray[j - 1];
                    dimsVect.push_back(j);
                }
            }
        }
    }

    if (dimsVect.empty())
    {
        delete[] piNewDimsArray;
        Scierror(78, _("%s: Wrong value for input argument #%d: Must be a valid permutation of [1..n>%d] integers.\n"), "permute", 2, iDims - 1);
        return types::Function::Error;
    }

    types::GenericType *pOut;

    switch (in[0]->getType())
    {
        case types::InternalType::ScilabDouble:
        {
            pOut = doNativePermute(in[0]->getAs<types::Double>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabUInt64:
        {
            pOut = doNativePermute(in[0]->getAs<types::UInt64>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabInt64:
        {
            pOut = doNativePermute(in[0]->getAs<types::Int64>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabUInt32:
        {
            pOut = doNativePermute(in[0]->getAs<types::UInt32>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabInt32:
        {
            pOut = doNativePermute(in[0]->getAs<types::Int32>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabUInt16:
        {
            pOut = doNativePermute(in[0]->getAs<types::UInt16>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabInt16:
        {
            pOut = doNativePermute(in[0]->getAs<types::Int16>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabUInt8:
        {
            pOut = doNativePermute(in[0]->getAs<types::UInt8>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabInt8:
        {
            pOut = doNativePermute(in[0]->getAs<types::Int8>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabBool:
        {
            pOut = doNativePermute(in[0]->getAs<types::Bool>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabString:
        {
            pOut = doPermute(in[0]->getAs<types::String>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabPolynom:
        {
            pOut = doPermute(in[0]->getAs<types::Polynom>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabStruct:
        {
            pOut = doPermute(in[0]->getAs<types::Struct>(), dimsVect);
            break;
        }
        case types::InternalType::ScilabCell:
        {
            pOut = doPermute(in[0]->getAs<types::Cell>(), dimsVect);
            break;
        }
        default:
        {
            delete[] piNewDimsArray;
            std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_permute";
            return Overload::call(wstFuncName, in, _iRetCount, out);
        }
    }

    pOut->reshape(piNewDimsArray, iNewDims);

    delete[] piNewDimsArray;

    out.push_back(pOut);

    return types::Function::OK;
}