czardas

Roman (Again)

18 posts in this topic




#2 ·  Posted (edited)

I was always too lazy to write this conversion. Thanks for sharing.

:thumbsup:

Edited by UEZ
1 person likes this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

nice one

Share this post


Link to post
Share on other sites
6 hours ago, UEZ said:

I was always too lazy to write this conversion.

I know the feeling. I'm waiting for someone to come up with a Natural Roman Sort algorithm (King Henry I, II, III, IV etc...). :D

1 person likes this

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks for sharing.

I well remember adding a Roman Numeral element to my old Titlecase Function ... was it turned (or a variation of) into a UDF ... not sure? There is a couple of discussions about the place somewhere ... involving @guinness, @tcurran, you and others ... if I remember rightly.

If and when I ever get around to updating that function, then hopefully I will remember to incorporate your excellent UDF.

Edited by TheSaint
1 person likes this

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

If someone has some time for a roman challenge, here's one:

A new hotel in Vegas has 2016 rooms and the tenant wants to number every room in roman numerals (in natural sequence).

How many letters of each in ( I V X L ...) will that need? Algorithmic answer only, please.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
3 hours ago, jchd said:

If someone has some time for a roman challenge....

a first attempt:

#include <Array.au3>
#include 'Roman.au3'

Local $aRomanDigit = [['I', 0],['V', 0],['X', 0],['L', 0],['C', 0],['D', 0],['M', 0]]

For $iRoom = 1 To 2016
    _SumRomanDigit($iRoom, $aRomanDigit)
Next

_ArrayDisplay($aRomanDigit, 'Roman digits to buy')

Func _SumRomanDigit($iNum, ByRef $aStack)
    Local $sRoman = _Roman($iNum)
    For $i = 0 To 6 ; check all possible roman digit IVXLCDM
        StringReplace($sRoman, $aStack[$i][0], '') ; count digit
        $aStack[$i][1] += @extended ; sum digit
        If $sRoman = "" Then ExitLoop ; speedup a bit
    Next
EndFunc   ;==>_SumRomanDigi

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I'm surprised at the level of interest shown here. Roman numerals are still a popular alternative numbering system though. There are a few other threads about this subject around here somewhere. I'm happy if someone finds it useful. :)

Edited by czardas

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I recently wrote some quick and dirty decimal-to-Roman, Roman-to-decimal functions, then decided to check this forum and found this thread. For what it's worth, here's my code. It's compact, with minimal checking for bad input. Enjoy.

; ROMANUM.AU3
; Convert Arabic to Roman, Roman to Arabic numerals
; CLD -- XXVI IULIUS MMXVII

If $CmdLine[0] Then
    $vArg = $CmdLine[1]
    $sFunc = "Roman"
    If StringinStr("MDCLXVI", StringLeft($vArg, 1)) Then $sFunc = "Arabic"
    $vOut = Call($sFunc, $vArg)
    If Not $vOut Then $vOut = "No output"
    Exit MsgBox(0, @ScriptName, $vOut)
Else
    Exit MsgBox(64, @ScriptName, "Usage:" & @CRLF & @ScriptName & " decimal|roman_in")
EndIf

Func Arabic($sRom) ; Roman numerals to Arabic
    $aNum = StringSplit("CM,CD,XC,XL,IX,IV,M,D,C,L,X,V,I", ",")
    $aVal = StringSplit("900,400,90,40,9,4,1000,500,100,50,10,5,1", ",")
    $vOut = 0   
    For $i = 1 To $aVal[0]
        If StringInStr($sRom, $aNum[$i]) Then
            $sRom = StringReplace($sRom, $aNum[$i], "")
            $vOut += @extended * $aVal[$i]
        EndIf
    Next
    If $sRom Then
        Return SetError(1, 0, "Bad input")
    Else
        Return $vOut
    EndIf
EndFunc  ;==>Arabic

;----------
Func Roman($iIn) ; Arabic to Roman numerals
    $sOut = ""
    $aNum = StringSplit("M,CM,D,CD,C,XC,L,XL,X,IX,V,IV,I", ",")
    $aVal = StringSplit("1000,900,500,400,100,90,50,40,10,9,5,4,1", ",")
    For $i = 1 To $aVal[0]
        While $iIn >= Int($aVal[$i])
            $sOut &= $aNum[$i]
            $iIn -= $aVal[$i]
        WEnd
    Next
    Return $sOut
EndFunc  ;==>Roman

 

Edited by CarlD
1 person likes this

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Thanks for sharing! :)

Edit - You ought to include some form of validity check because it erroneously returns values for invalid roman numeral sequences and out of range integers - it's something to think about.

Edited by czardas
1 person likes this

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

On 7/27/2017 at 3:53 AM, czardas said:

Thanks for sharing! :)

Edit - You ought to include some form of validity check because it erroneously returns values for invalid roman numeral sequences and out of range integers - it's something to think about.

Thanks, czardas. For my specific purpose I didn't need error checking, because I could rely on input being valid. For a general purpose utility, your UDF covers the territory nicely. :)

I'm sure you know that you can expand the valid range by adding overlines to M, D, C, X, and V, which multiplies each value by 1,000. I've also read that double overlines can be used to multiply the values by 100,000 1,000,000. I haven't checked whether these overline characters are covered by Unicode. With double overlines, the valid range goes up to 399,999,999 3,999,999,999. Though I think even higher numbers would be very decipherable, e.g., {MMMM} for 400,000,000 4,000,000,000 (where the curly braces mean double overline), and on up.

Correction. I was referring to this page: http://sorenwinslow.com/RomanNumerals.asp. The "100,000" in the text appears to be an error; the examples show that the double overline multiplier is 1,000,000,

Edited by CarlD

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

On 28/07/2017 at 9:46 PM, CarlD said:

I'm sure you know that you can expand the valid range by adding overlines to M, D, C, X, and V, which multiplies each value by 1,000.

Hehe! No I wasn't aware of this convention, but it's good to know about. I'm okay with the limited range of 1 to 3999: I won't be doing any calculations with them. Having said that, perhaps it would be a fun challenge for someone to design a Roman calculator. Zero would throw an error (makes me giggle).

Edited by czardas

Share this post


Link to post
Share on other sites

#13 ·  Posted

Some historical chatter...

Do you know why letters I, V and X are used for the lower numbers?  It works like this:

Start with your left hand, palm out. Raise your little finger (outside finger), this means "1"

Add the ring finger, two. Add the middle finger, three.

Now, drop little and ring fingers, raise middle finger, index and thumb, four.

Drop middle finger, what remains is five.

Keep five on the left hand, and add the right thumb, six.  Etc

Two thumbs crossed means ten.

:)

 

1 person likes this

Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Sounds like a good daily excersice for ukulele. :D

Edited by czardas

Share this post


Link to post
Share on other sites

#15 ·  Posted

Yes, overline characters are covered by Unicode. Here is a little test.

global const $dbar = ChrW(0x33F)  ;DOUBLE OVERLINE
global const $bar  = ChrW(0x305)  ;OVERLINE

Func I_Quit()
    Exit
EndFunc

Func To_Roman($iDec)
    local $roman = ""

    local $io[25][2] = [[1000000000,"M" & $dbar], _
                        [500000000,"D" & $dbar], _
                        [100000000,"C" & $dbar], _
                        [50000000,"L" & $dbar], _
                        [10000000,"X" & $dbar], _
                        [5000000,"V" & $dbar], _
                        [1000000,"M" & $bar], _
                        [500000,"D" & $bar], _
                        [100000,"C" & $bar], _
                        [50000,"L" & $bar], _
                        [10000,"X" & $bar], _
                        [5000,"V" & $bar], _
                        [1000,"M"], _
                        [900,"CM"], _
                        [500,"D"], _
                        [400,"CD"], _
                        [100,"C"], _
                        [90,"XC"], _
                        [50,"L"], _
                        [40,"XL"], _
                        [10,"X"], _
                        [9,"IX"], _
                        [5,"V"], _
                        [4,"IV"], _
                        [1,"I"]]

    For $i = 0 To Ubound($io) - 1
        While $iDec >= $io[$i][0]
            $iDec -= $io[$i][0]
            $roman &= $io[$i][1]
        WEnd
    Next
    Return $roman
EndFunc

HotKeySet("{ESC}","I_Quit")

local $iNumber = 3457898377

SplashTextOn("Roman Numbers For:" & $iNumber,To_Roman($iNumber),@DesktopWidth,60,-1,-1,-1,default,24)

While True
wEnd

 

2 people like this

Share this post


Link to post
Share on other sites

#16 ·  Posted

On 7/31/2017 at 4:23 AM, Skysnake said:

Some historical chatter...

Do you know why letters I, V and X are used for the lower numbers?  It works like this:

Start with your left hand, palm out. Raise your little finger (outside finger), this means "1"

Add the ring finger, two. Add the middle finger, three.

Now, drop little and ring fingers, raise middle finger, index and thumb, four.

Drop middle finger, what remains is five.

Keep five on the left hand, and add the right thumb, six.  Etc

Two thumbs crossed means ten.

:)

 

Fascinating! And makes sense. But the lawyer in me asks: What is your authority for this?

Share this post


Link to post
Share on other sites

#17 ·  Posted

On 7/31/2017 at 10:24 AM, xroot said:

Yes, overline characters are covered by Unicode. Here is a little test.

global const $dbar = ChrW(0x33F)  ;DOUBLE OVERLINE
global const $bar  = ChrW(0x305)  ;OVERLINE

Func I_Quit()
    Exit
EndFunc

Func To_Roman($iDec)
    local $roman = ""

    local $io[25][2] = [[1000000000,"M" & $dbar], _
                        [500000000,"D" & $dbar], _
                        [100000000,"C" & $dbar], _
                        [50000000,"L" & $dbar], _
                        [10000000,"X" & $dbar], _
                        [5000000,"V" & $dbar], _
                        [1000000,"M" & $bar], _
                        [500000,"D" & $bar], _
                        [100000,"C" & $bar], _
                        [50000,"L" & $bar], _
                        [10000,"X" & $bar], _
                        [5000,"V" & $bar], _
                        [1000,"M"], _
                        [900,"CM"], _
                        [500,"D"], _
                        [400,"CD"], _
                        [100,"C"], _
                        [90,"XC"], _
                        [50,"L"], _
                        [40,"XL"], _
                        [10,"X"], _
                        [9,"IX"], _
                        [5,"V"], _
                        [4,"IV"], _
                        [1,"I"]]

    For $i = 0 To Ubound($io) - 1
        While $iDec >= $io[$i][0]
            $iDec -= $io[$i][0]
            $roman &= $io[$i][1]
        WEnd
    Next
    Return $roman
EndFunc

HotKeySet("{ESC}","I_Quit")

local $iNumber = 3457898377

SplashTextOn("Roman Numbers For:" & $iNumber,To_Roman($iNumber),@DesktopWidth,60,-1,-1,-1,default,24)

While True
wEnd

 

Lovely! Very nice. Now....

Share this post


Link to post
Share on other sites

#18 ·  Posted

res ipsa loquitur

:)

 


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now