HP Forums
(50G) Inverse Permutation - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (50G) Inverse Permutation (/thread-4007.html)



(50G) Inverse Permutation - Gerald H - 05-28-2015 09:15 AM

Given a list of positive integers 1 to g the programme returns the inverse permutation.

eg For input

{ 1 66 76 94 105 82 62 26 4 50 90 55 99 7 34 104 16 93 39 6 75 28 29 95 64 51 49 24 86 5 9 59 42 97 89 96 23 20 36 22 61 67 35 63 92 80 37 88 30 54 33 38 47 106 41 31 13 2 25 45 81 103 57 17 52 8 100 73 3 91 14 68 101 32 79 78 12 43 56 58 83 21 102 98 48 65 10 18 11 84 87 71 85 46 40 72 44 15 27 70 19 77 53 74 69 60 }

the programme returns

{ 1 58 69 9 30 20 14 66 31 87 89 77 57 71 98 17 64 88 101 38 82 40 37 28 59 8 99 22 23 49 56 74 51 15 43 39 47 52 19 95 55 33 78 97 60 94 53 85 27 10 26 65 103 50 12 79 63 80 32 106 41 7 44 25 86 2 42 72 105 100 92 96 68 104 21 3 102 76 75 46 61 6 81 90 93 29 91 48 35 11 70 45 18 4 24 36 34 84 13 67 73 83 62 16 5 54 }

Code:
::
  CK1
  FPTR 7 17B
  DUP
  toLEN_DO
  DUPINDEX@
  NTHCOMPDROP
  INDEX@
  FPTR2 ^#>Z
  SWAP
  FPTR2 ^Z2BIN
  4ROLL
  PUTLIST
  SWAPLOOP
  DROP
;



RE: HP 50G: Inverse Permutation - Didier Lachieze - 05-28-2015 11:17 AM

I'm not fluent in SysRPL so here is my version of Inverse Permutation for the HP Prime:

Code:
EXPORT InvPerm(list)
BEGIN
  MAKELIST(POS(list,X),X,1,SIZE(list));
END;



RE: HP 50G: Inverse Permutation - Gerald H - 05-28-2015 11:44 AM

Your prog, Didier, works nicely for well-formed lists but returns an erroneous inverse for eg {1,3,6,4,5}.

It would be better if the prog returned an error.


RE: HP 50G: Inverse Permutation - Didier Lachieze - 05-28-2015 01:00 PM

(05-28-2015 11:44 AM)Gerald H Wrote:  It would be better if the prog returned an error.
Sure, what kind of error does your program return on the 50G?


RE: HP 50G: Inverse Permutation - Gerald H - 05-28-2015 02:08 PM

For input {1 3 6 4 5} my prog returns "Bad Argument Type".


RE: HP 50G: Inverse Permutation - Didier Lachieze - 05-28-2015 03:12 PM

Does it return the same error message for {1 3 3 4 5}?

Here is the updated version with error handling:

Code:
EXPORT InvPerm(list)
BEGIN
  LOCAL p;
  MAKELIST(IFTE(p:=POS(list,X),p,ASC(0)),X,1,SIZE(list));
END;



RE: HP 50G: Inverse Permutation - Gerald H - 05-28-2015 06:17 PM

Yes, same error message for two 3s in list.


RE: HP 50G: Inverse Permutation - Gerald H - 05-29-2015 05:23 AM

My prog also returns the same error message for {3,4,5,3,2,1,6}.