(50G) Inverse Permutation
05-28-2015, 09:15 AM (This post was last modified: 06-15-2017 01:40 PM by Gene.)
Post: #1
 Gerald H Senior Member Posts: 1,452 Joined: May 2014
(50G) Inverse Permutation
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 ;
05-28-2015, 11:17 AM
Post: #2
 Didier Lachieze Senior Member Posts: 1,236 Joined: Dec 2013
RE: HP 50G: Inverse Permutation
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;
05-28-2015, 11:44 AM
Post: #3
 Gerald H Senior Member Posts: 1,452 Joined: May 2014
RE: HP 50G: Inverse Permutation
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.
05-28-2015, 01:00 PM
Post: #4
 Didier Lachieze Senior Member Posts: 1,236 Joined: Dec 2013
RE: HP 50G: Inverse Permutation
(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?
05-28-2015, 02:08 PM
Post: #5
 Gerald H Senior Member Posts: 1,452 Joined: May 2014
RE: HP 50G: Inverse Permutation
For input {1 3 6 4 5} my prog returns "Bad Argument Type".
05-28-2015, 03:12 PM (This post was last modified: 05-28-2015 03:52 PM by Didier Lachieze.)
Post: #6
 Didier Lachieze Senior Member Posts: 1,236 Joined: Dec 2013
RE: HP 50G: Inverse Permutation
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;
05-28-2015, 06:17 PM
Post: #7
 Gerald H Senior Member Posts: 1,452 Joined: May 2014
RE: HP 50G: Inverse Permutation
Yes, same error message for two 3s in list.
05-29-2015, 05:23 AM
Post: #8
 Gerald H Senior Member Posts: 1,452 Joined: May 2014
RE: HP 50G: Inverse Permutation
My prog also returns the same error message for {3,4,5,3,2,1,6}.
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)