Author Topic: -={ Challenge }=- Reverse Items  (Read 8556 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Reverse Items
« Reply #15 on: December 12, 2010, 12:32:59 PM »
Code: [Select]
def reverseNths (nths, lst):
    result=list(lst[::]) ; nths=list(nths) ; nths.sort()
    lst2=list(lst[i] for i in nths) ; lst2.reverse()
    for i,j in enumerate(nths): result[j]=lst2[i]
    return result

Code: [Select]
reverseNths((1,4,5), (0,1,2,3,4,5,6,7))

>>> [0, 5, 2, 3, 4, 1, 6, 7]

Code: [Select]
reverseNths((5,1,4), (0,1,2,3,4,5,6,7))

>>> [0, 5, 2, 3, 4, 1, 6, 7]

Added bonus:

Code: [Select]
reverseNths((5,1,4),'abcdefg')

>>> ['a', 'f', 'c', 'd', 'e', 'b', 'g']
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Reverse Items
« Reply #16 on: December 12, 2010, 12:56:52 PM »
Nice Python(?) solution Michael!

Your approach sparked an idea:

Code: [Select]
(defun ReverseItems ( idx lst / arr )
  (setq arr
    (vlax-safearray-fill
      (vlax-make-safearray vlax-vbVariant (cons 0 (1- (length lst)))) lst
    )
  )
  (mapcar
    (function
      (lambda ( a b )
        (vlax-safearray-put-element arr a b)
      )
    )
    (setq idx (vl-sort idx '<)) (mapcar '(lambda ( x ) (nth x lst)) (reverse idx))
  )
  (mapcar 'vlax-variant-value (vlax-safearray->list arr))
)

[ Edited to sort index list ]
« Last Edit: December 12, 2010, 01:21:16 PM by Lee Mac »

LE3

  • Guest
Re: -={ Challenge }=- Reverse Items
« Reply #17 on: December 12, 2010, 12:58:13 PM »
geez... can't compete with those very compacted linear coding stylish's - unless i make a class with a set of functions, and exposed them in that similiar style... but dang' to much work for a simple challenge.

need to find time to learn another new language...  :wink:

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Reverse Items
« Reply #18 on: December 12, 2010, 01:00:46 PM »
Nice Python(?) solution Michael!

ummm, yeah, guess I should have spec'd what language it was :lol:

Your approach sparked an idea:

Code: [Select]
(defun ReverseItems ( idx lst / arr )
  (setq arr
    (vlax-safearray-fill
      (vlax-make-safearray vlax-vbVariant (cons 0 (1- (length lst)))) lst
    )
  )
  (mapcar
    (function
      (lambda ( a b )
        (vlax-safearray-put-element arr a b)
      )
    )
    idx (mapcar '(lambda ( x ) (nth x lst)) (reverse idx))
  )
  (mapcar 'vlax-variant-value (vlax-safearray->list arr))
)

cool, ain't programming fun? :D
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Reverse Items
« Reply #19 on: December 12, 2010, 01:07:46 PM »
Nice Python(?) solution Michael!
ummm, yeah, guess I should have spec'd what language it was :lol:

Well, I was pretty sure it was Python, but not 100%  :oops:

ain't programming fun? :D

I'm pretty much addicted, yeah

LE3

  • Guest
Re: -={ Challenge }=- Reverse Items
« Reply #20 on: December 12, 2010, 04:23:55 PM »
Was thinking something like this to test them, but not sure how to test the non-LISP solutions  :|
Here it is a release version of my arx function (for acad 2010-2011)

Command:
***Usage: ReverseByIndices(<list of strings> <list of integers - indices>) ***

Some testings:
Quote
(setq l (reversebyindices (list "A" "B" "C" "D" "E" "F" "G" "H") (list 1 3 4)))
;;("A" "E" "C" "D" "B" "F" "G" "H")

;;(A B C D E F G H)  -->  (A E C D B F G H)

;;0 2 5 6
(setq l (reversebyindices (list "A" "B" "C" "D" "E" "F" "G" "H") (list 0 2 5 6)))
;;("G" "B" "F" "D" "E" "C" "A" "H")

;;(A B C D E F G H)  -->  (G B F D E C A H)

(setq l (reversebyindices (list "A" "B" "C" "D" "E" "F" "G" "H") (list 1 3)))
 0 1 2 3
(A B C D E F G H)
("A" "D" "C" "B" "E" "F" "G" "H")

(setq l (reversebyindices (list "A" "B" "C" "D" "E" "F" "G" "H") (list 1)))
("A" "B" "C" "D" "E" "F" "G" "H")

(setq l (reversebyindices (list "A" "B" "C" "D" "E" "F" "G" "H") (list 1 1 1 3 3 3 4 4 4)))

(setq l (reversebyindices (list "A" "B" "C" "D" "E" "F" "G" "H") (list 1 2 3 4 5 6 7 8 9 10 11 12 13)))

(setq l (reversebyindices (list "A" "B" "C" "D" "E" "F" "G" "H") (list -1 -2)))

pkohut

  • Bull Frog
  • Posts: 483
Re: -={ Challenge }=- Reverse Items
« Reply #21 on: December 12, 2010, 05:26:05 PM »
If I understand correctly what the outputs should be...C++ version
Code: [Select]
#include <iostream>
#include <vector>
using namespace std;

void ReverseIndice(vector<int> indices, vector<char> & lst)
{
    sort(indices.begin(), indices.end()); // edit: added this to get same results as Lee's func
    vector<int>::const_iterator it = indices.begin();
    vector<int>::const_iterator rit = indices.end() - 1;
    
    for(; it != indices.begin() + indices.size() / 2; ++it, --rit)
        swap(lst.at(*it), lst.at(*rit));
}

#define ELEMENTS(x) sizeof(x) / sizeof(x[0])

int main(int argc, char *argv[])
{
    char items_array[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
//  int indices_array[] = {1, 3, 4};
    int indices_array[] = {0, 2, 5, 6};
//  int indices_array[] = {0, 5, 1 ,2};
    
    // make vectors from arrays
    vector<char> lst(items_array, &items_array[ELEMENTS(items_array)]);
    vector<int> indices(indices_array, &indices_array[ELEMENTS(indices_array)]);

    ReverseIndice(indices, lst);

    // output list
    for(int i = 0; i < lst.size(); i++)
        cout << lst.at(i) << " ";

    return 0;
}
Quote
edit: with the sort(indices.begin(), indices.end()); (corrected output)
{1, 2, 3}     {A, B, C, D, E, F, G, H} => A E C D B F G H
{0, 2, 5, 6} {A, B, C, D, E, F, G, H} => G B F D E C A H
{0, 5, 1, 2} {A, B, C, D, E, F, G, H} => F C B D E A G H
witout sort function:
{0, 5, 1, 2} {A, B, C, D, E, F, G, H} => C F A D E B G H
« Last Edit: December 12, 2010, 08:08:53 PM by pkohut »
New tread (not retired) - public repo at https://github.com/pkohut

pkohut

  • Bull Frog
  • Posts: 483
Re: -={ Challenge }=- Reverse Items
« Reply #22 on: December 12, 2010, 11:25:44 PM »
C++ version converted to Python (note pythonic swap (no temp var needed))

Code: [Select]
def revNths(nths, lst):
result = list(lst); nths = list(nths); nths.sort()
i = 0; j = len(nths) - 1
while(i < len(nths) / 2):
result[nths[i]], result[nths[j]] = result[nths[j]], result[nths[i]]
i += 1; j -= 1;
return result

Quote
>>> revNths((1, 3, 4), "abcdefgh")
['a', 'e', 'c', 'd', 'b', 'f', 'g', 'h']
>>> revNths((0, 2, 5, 6), "abcdefgh")
['g', 'b', 'f', 'd', 'e', 'c', 'a', 'h']
>>> revNths((0, 5, 1, 2), "abcdefgh")
['f', 'c', 'b', 'd', 'e', 'a', 'g', 'h']
>>> revNths((0, 5, 1, 2, 3), "abcdefgh")
['f', 'd', 'c', 'b', 'e', 'a', 'g', 'h']
New tread (not retired) - public repo at https://github.com/pkohut

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Reverse Items
« Reply #23 on: December 13, 2010, 07:45:50 AM »
Nice idea Paul to only perform half the iterations!

LE3

  • Guest
Re: -={ Challenge }=- Reverse Items
« Reply #24 on: December 13, 2010, 07:59:56 PM »
Code: [Select]
char items[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
const int indices[] = { 1, 3, 4, };
std::vector<char, std::allocator<char>> v (items, items + sizeof items / sizeof *items);
std::vector<char, std::allocator<char>> v2;
std::vector<int, std::allocator<int>> idxs (indices, indices + sizeof indices / sizeof *indices);
std::reverse(idxs.begin(), idxs.end());
for (int i = 0; i < idxs.size(); i++)
v2.push_back(v.at(idxs.at(i)));
std::reverse(idxs.begin(), idxs.end());
for (int i = 0; i < idxs.size(); i++)
v.at(idxs.at(i)) = v2.at(i);
for (int i = 0; i < v.size(); i++)
acutPrintf(_T("%s "), AcString(v.at(i)).kACharPtr());

pkohut

  • Bull Frog
  • Posts: 483
Re: -={ Challenge }=- Reverse Items
« Reply #25 on: December 13, 2010, 11:59:27 PM »
Code: [Select]
<<snip>>
Not bad, need to sort indices though. This is based off yours, removes the reverse calls and clean up the code a bit and add some white space.

Code: [Select]
int main(int argc, char *argv[])
{
    char _items[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
    int _indices[] = { 0, 5, 1, 2, };
    vector<char> items(_items, &_items[ELEMENTS(_items)]);
    vector<int> indices(_indices, &_indices[ELEMENTS(_indices)]);
    vector<char> pickedItems;

    sort(indices.begin(), indices.end());
    for(vector<int>::reverse_iterator rit = indices.rbegin(); rit != indices.rend(); ++rit)
        pickedItems.push_back(items.at(*rit));

    for(int i = 0; i < pickedItems.size(); ++i)
        items.at(indices.at(i)) = pickedItems.at(i);

    for (int i = 0; i < items.size(); ++i)
        cout << items[i] << " ";
    cout << endl;

    system("pause");
    return 0;
}
New tread (not retired) - public repo at https://github.com/pkohut

pkohut

  • Bull Frog
  • Posts: 483
Re: -={ Challenge }=- Reverse Items
« Reply #26 on: December 14, 2010, 12:00:01 AM »

Here's a a final more "C" like version, much cleaner and easier to read.
Code: [Select]
int main(int argc, char *argv[])
{
    char items[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
    int indices[] = { 0, 2, 5, 6, };
    size_t nIndices = ELEMENTS(indices);
    char * pPickedItems = (char *) alloca(sizeof(char) * nIndices);

    sort(indices, &indices[nIndices]);
    for(int i = nIndices - 1, j = 0; i >= 0; --i, ++j)
        pPickedItems[j] = items[indices[i]];

    for(int i = 0; i < nIndices; ++i)
        items[indices[i]] = pPickedItems[i];

    for (int i = 0; i < ELEMENTS(items); ++i)
            cout << items[i] << " ";
    cout << endl;

    system("pause");
    return 0;
}
New tread (not retired) - public repo at https://github.com/pkohut

LE3

  • Guest
Re: -={ Challenge }=- Reverse Items
« Reply #27 on: December 14, 2010, 12:31:40 AM »
Not bad, need to sort indices though. This is based off yours, removes the reverse calls and clean up the code a bit and add some white space.

Oops, forgot that part...



Now it looks good :)

LE3

  • Guest
Re: -={ Challenge }=- Reverse Items
« Reply #28 on: December 14, 2010, 12:35:24 AM »
This looks good, Paul


Here's a a final more "C" like version, much cleaner and easier to read.
Code: [Select]
int main(int argc, char *argv[])
{
    char items[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
    int indices[] = { 0, 2, 5, 6, };
    size_t nIndices = ELEMENTS(indices);
    char * pPickedItems = (char *) alloca(sizeof(char) * nIndices);

    sort(indices, &indices[nIndices]);
    for(int i = nIndices - 1, j = 0; i >= 0; --i, ++j)
        pPickedItems[j] = items[indices[i]];

    for(int i = 0; i < nIndices; ++i)
        items[indices[i]] = pPickedItems[i];

    for (int i = 0; i < ELEMENTS(items); ++i)
            cout << items[i] << " ";
    cout << endl;

    system("pause");
    return 0;
}