Decode and Encode Roman Numerals

139 views
Skip to first unread message

TorQ Guru

unread,
Feb 28, 2017, 5:25:43 AM2/28/17
to AquaQ kdb+/TorQ
Write two functions one, to encode, the other to decode all integers between 1 and 20000 from and to Roman Numerals. 

TM

ryan sparks

unread,
Feb 28, 2017, 7:37:35 PM2/28/17
to TorQ Guru, AquaQ kdb+/TorQ
Here's one way, needs a bit of setup with dictionaries. I have encode expecting a list as the vectorised version is a lot faster than the atomic. However for decode just left it as atomic because making it work for lists just involved putting in a bunch of each's in and didn't really speed things up at all. 

/ make a&b dicts of nums->romans
/ and romans->nums respectively
k:13#,/(10*)\1 4 5 9
s:$`I`IV`V`IX`X`XL`L`XC`C`CD`D`CM`M
a:k!s
b:@[s;&1=#:'s;*:]!k
d:{+/@[r;-1+1_&:(>)':r:b x;-:]}
e:{*{(x[0],'a n;i-n:k k bin i:x 1)}/[((#x)#,"";x)]}


Here's a website I was testing the results on if anyone else wants it (the website has a blank line after 1491 for some reason, hence extra ugliness below)

test:t@\:<*t:0^@[;1;.q.trim]("J*";"=")0:2001#14_"\n"\:.Q.hg`:http://oeis.org/A006968/a006968.txt
e[!2001]~test 1
d'[test 1]~test 0


Cheers

Ryan

--
www.aquaq.co.uk
www.aquaq.co.uk/blog
www.aquaq.co.uk/training
---
You received this message because you are subscribed to the Google Groups "AquaQ kdb+/TorQ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kdbtorq+unsubscribe@googlegroups.com.
To post to this group, send email to kdb...@googlegroups.com.
Visit this group at https://groups.google.com/group/kdbtorq.
To view this discussion on the web, visit https://groups.google.com/d/msgid/kdbtorq/20813cba-84b4-4839-a796-2cd0664ed177%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Muneish Adya

unread,
Mar 1, 2017, 3:47:32 AM3/1/17
to TorQ Guru, AquaQ kdb+/TorQ
Encoding is simple:
q)seed:([]I:1 4 5 9 10 40 50 90 100 400 500 900 1000;R:`I`IV`V`IX`X`XL`L`XC`X`CD`D`CM`M);
q)result:([I:`int$()]R:());
q)`result upsert {(x;1_{idx:seed[`I] bin x[0]; (x[0]-seed[`I]idx ; x[1],seed[`R]idx)}/[{x[0]};(x;`)][1])} each 1+til 20000
q)0N!result

or 

q)`result upsert {(x;`$raze string {idx:seed[`I] bin x[0]; (x[0]-seed[`I]idx ; x[1],seed[`R]idx)}/[{x[0]};(x;`)][1])} each 1+til 20000


--

Rahul Asati

unread,
Mar 1, 2017, 3:47:32 AM3/1/17
to AquaQ kdb+/TorQ
Encode:
v:1 4 5 9 10 40 50 90 100 400 500 900 1000; / value ranges
m:("I";"IV";"V";"IX";"X";"XL";"L";"XC";"C";"CD";"D";"CM";"M")   / corresponding roman number
encode:{first {(x[0],m i;x[1]-v i:v bin x 1);x) 1>x 1}/[(();x)]}

q) encode 15  / "XV"

Decode:

d:"IVXLCDM"!1 5 10 50 100 500 1000;
decode:{sum a*-1_1,?[a<=next a:reverse (),d x;1;-1]}

q) decode "XV"  / 15

Decode will convert all valid roman numbers to integers but side effect to that is it will also convert invalid roman numbers.
We can create a function to check validity of a roman number by applying all basic rules but I have used encode and decode functions to create one:

isValid:{x~encode decode x}
q) isValid "CCXIC"  / 0b


valid:{x~encode decode x}

Ryan McCarron

unread,
Mar 3, 2017, 2:14:18 PM3/3/17
to AquaQ kdb+/TorQ
Encoding with the values from 1-10 and the symbols used in each order of magnitude:
b:(1+til 9)!("i";"ii";"iii";"iv";"v";"vi";"vii";"viii";"ix")
o:("xvi";"clx";"mdc";"XVm";"CLX";"MDC")
dr:{(raze reverse ((count a)#o)@'"xvi"?/:b reverse a:10 vs x) except " "}

Decoding by comparing number pairs:
nl:"ivxlcdmIVXLCDM"
n:nl!1 5 10 50 100 500 1000 1000 5000 10000 50000 100000 500000 1000000
rd:{{x+$[0>(-) . nl?"i"^y;neg n first y;n first y]}/[0;x,'next x]}

Note I've used the lowercase for normal numerals, and uppercase for 1000x that numeral (instead of the vinculum system on the wiki)

Neill, Rolf

unread,
Mar 9, 2017, 11:06:55 AM3/9/17
to TorQ Guru, AquaQ kdb+/TorQ

(building on the work of others)

 

a:1 4 5 9 10 40 50 90 100 400 500 900 1000

b:("I";"IV";"V";"IX";"X";"XL";"L";"XC";"C";"CD";"D";"CM";"M")

e:{raze b a?-1*1_deltas -1_{x-a a bin x}\[x]}

d:{sum r*1+-2*next(>':)r:a b?x}

--

www.aquaq.co.uk
www.aquaq.co.uk/blog
www.aquaq.co.uk/training
---
You received this message because you are subscribed to the Google Groups "AquaQ kdb+/TorQ" group.

To unsubscribe from this group and stop receiving emails from it, send an email to kdbtorq+u...@googlegroups.com.


To post to this group, send email to kdb...@googlegroups.com.
Visit this group at https://groups.google.com/group/kdbtorq.
To view this discussion on the web, visit https://groups.google.com/d/msgid/kdbtorq/20813cba-84b4-4839-a796-2cd0664ed177%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


######################################################################

The information contained in this communication is confidential and

may contain information that is privileged or exempt from disclosure

under applicable law. If you are not a named addressee, please notify

the sender immediately and delete this email from your system.

If you have received this communication, and are not a named

recipient, you are hereby notified that any dissemination,

distribution or copying of this communication is strictly prohibited.

######################################################################

ryan sparks

unread,
Mar 10, 2017, 5:08:30 PM3/10/17
to Neill, Rolf, TorQ Guru, AquaQ kdb+/TorQ
Cool, doing r*1 -2+ on the decode is nice. Inspired by this, and also using ryan's idea for lower/upper casing for big numbers (never knew roman numerals went beyond about 3000), another similar way for the decode (vector input/output):

k)b:("I";"IV";"V";"IX";"X";"XL";"L";"XC";"C";"CD";"D";"CM";"M";"Mv";"v";"Mx";"x")!17#,/(10*)\1 4 5 9

// k/q basically the same here: sum each <-> +/']

d:{sum each r*1 -1 r<1_'(r:b x),'0}

encoded:e til 20000  // e in earlier email

d encoded


Cheers


Ryan


On Thu, Mar 9, 2017 at 11:06 AM, Neill, Rolf <Rolf....@mlp.com> wrote:

(building on the work of others)

 

a:1 4 5 9 10 40 50 90 100 400 500 900 1000

b:("I";"IV";"V";"IX";"X";"XL";"L";"XC";"C";"CD";"D";"CM";"M")

e:{raze b a?-1*1_deltas -1_{x-a a bin x}\[x]}

d:{sum r*1+-2*next(>':)r:a b?x}

 

 

From: kdb...@googlegroups.com [mailto:kdbtorq@googlegroups.com] On Behalf Of TorQ Guru
Sent: Tuesday, February 28, 2017 5:26 AM
To: AquaQ kdb+/TorQ
Subject: [Aquaq kdbtorq] Decode and Encode Roman Numerals

 

Write two functions one, to encode, the other to decode all integers between 1 and 20000 from and to Roman Numerals. 

 

TM

--
www.aquaq.co.uk
www.aquaq.co.uk/blog
www.aquaq.co.uk/training
---
You received this message because you are subscribed to the Google Groups "AquaQ kdb+/TorQ" group.

To unsubscribe from this group and stop receiving emails from it, send an email to kdbtorq+unsubscribe@googlegroups.com.


To post to this group, send email to kdb...@googlegroups.com.
Visit this group at https://groups.google.com/group/kdbtorq.
To view this discussion on the web, visit https://groups.google.com/d/msgid/kdbtorq/20813cba-84b4-4839-a796-2cd0664ed177%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


######################################################################

The information contained in this communication is confidential and

may contain information that is privileged or exempt from disclosure

under applicable law. If you are not a named addressee, please notify

the sender immediately and delete this email from your system.

If you have received this communication, and are not a named

recipient, you are hereby notified that any dissemination,

distribution or copying of this communication is strictly prohibited.

######################################################################

--
www.aquaq.co.uk
www.aquaq.co.uk/blog
www.aquaq.co.uk/training
---
You received this message because you are subscribed to the Google Groups "AquaQ kdb+/TorQ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kdbtorq+unsubscribe@googlegroups.com.

To post to this group, send email to kdb...@googlegroups.com.
Visit this group at https://groups.google.com/group/kdbtorq.
Reply all
Reply to author
Forward
0 new messages