Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Wednesday, February 9, 2011

# Find the first m in Array, return reversed sub array before it.

Q: Given a sample array of integers containing a zero at random location, return reversed sub array before the first 0.

==> In C/C++::
We need to start from the start of the Array to check the location of the "m/ zero"

# include <iostream>
# include <vector>
using namespace std;

int main()
{
   int myintsA[] = {16, 2, 88, 0, 82, 10. 0. 87};
   m= 0;
   vector<int> myints (myintsA, myintsA + sizeof(myintsA)/sizeof(int));
   vector<int> myvector;
   vector<int>::iterator it;

   for (i = 0; i < myints.size(); i++)
{
    if (myints.at(i) == m)
         {
              break;
         }
    it = myvector.begin();
    myvector.insert(it, myints.at(i));
}

for(i =0, i<myvector.size(), i++)
   {
     printf("%d", myvector.at(i));
   }

}


==>In Python::


def reverSubArray(Array, m):                   # Array is a list
""" this can find the first apprearacn of m, and return the reverse list before m"""
    subA = Array[:Array.index(m)]
    return subA.reverse()



♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥
http://www.cplusplus.com/reference/stl/vector/insert/
http://www.cplusplus.com/reference/stl/vector/pop_back/

No comments:

Post a Comment