Post Reply 
Roman Numeral Function
01-20-2016, 01:08 PM
Post: #5
RE: Roman Numeral Function
(01-20-2016 05:56 AM)Gerald H Wrote:  Here is an economical Roman to Arabic converter:

http://www.hpmuseum.org/forum/thread-164...ight=roman

Perhaps someone could translate the programme into Prime?

With some checks on the input:
Code:
EXPORT R→A(r)
BEGIN
  LOCAL l1:=ASC("IVXLCDM");
  LOCAL l2:={1,5,10,50,100,500,1000};
  LOCAL l3, ix, rn, dn, cur, prev, sz;
  
  //if not a string, stop
  IF TYPE(r)<>2 THEN RETURN r; END; 

  rn:=UPPER(r); sz:=SIZE(rn);
  l3:= MAKELIST(POS(l1,rn(I)),I,1,sz);

  //if string includes non-roman characters, stop
  IF NOT ΠLIST(l3) THEN RETURN r; END; 
  
  //else returns decimal number
  FOR ix FROM 1 to sz DO
    cur:= l2(l3(ix));
    dn:=dn + IFTE(prev<cur, -prev, prev);
    prev:=cur;
  END;
  RETURN dn+cur;
END;
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Roman Numeral Function - smp - 01-17-2016, 09:29 PM
RE: Roman Numeral Function - John Colvin - 01-19-2016, 09:46 PM
RE: Roman Numeral Function - smp - 01-19-2016, 10:12 PM
RE: Roman Numeral Function - Gerald H - 01-20-2016, 05:56 AM
RE: Roman Numeral Function - Didier Lachieze - 01-20-2016 01:08 PM
RE: Roman Numeral Function - Gerald H - 01-20-2016, 04:09 PM
RE: Roman Numeral Function - Gerald H - 01-21-2016, 10:45 AM
RE: Roman Numeral Function - DrD - 01-21-2016, 11:49 AM
RE: Roman Numeral Function - JMB - 01-21-2016, 01:42 PM



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