From 1cb17733c6435e2bf773f244dbe7bf433c4120df Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 5 Jul 2020 16:55:31 +0200 Subject: [PATCH] Adding a numberToRoman function for converting integers to Roman number strings --- nw/core/__init__.py | 8 +++----- nw/core/tools.py | 33 +++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/nw/core/__init__.py b/nw/core/__init__.py index 0107dac2..91616302 100644 --- a/nw/core/__init__.py +++ b/nw/core/__init__.py @@ -3,13 +3,10 @@ from nw.core.document import NWDoc from nw.core.index import NWIndex from nw.core.project import NWProject -from nw.core.spellcheck import NWSpellCheck -from nw.core.spellcheck import NWSpellEnchant -from nw.core.spellcheck import NWSpellSimple +from nw.core.spellcheck import NWSpellCheck, NWSpellEnchant, NWSpellSimple from nw.core.tokenizer import Tokenizer from nw.core.tohtml import ToHtml -from nw.core.tools import countWords -from nw.core.tools import numberToWord +from nw.core.tools import countWords, numberToRoman, numberToWord __all__ = [ "NWDoc", @@ -21,5 +18,6 @@ __all__ = [ "Tokenizer", "ToHtml", "countWords", + "numberToRoman", "numberToWord", ] diff --git a/nw/core/tools.py b/nw/core/tools.py index 5c352fe7..c4455ff1 100644 --- a/nw/core/tools.py +++ b/nw/core/tools.py @@ -34,9 +34,9 @@ from os import path, unlink, rmdir logger = logging.getLogger(__name__) -# =========================================================================== # +# =============================================================================================== # # Simple Word Counter -# =========================================================================== # +# =============================================================================================== # def countWords(theText): """Count words in a piece of text, skipping special syntax and @@ -84,9 +84,34 @@ def countWords(theText): return charCount, wordCount, paraCount -# =========================================================================== # +# =============================================================================================== # +# Convert an Integer to a Roman Number +# =============================================================================================== # + +def numberToRoman(numVal, isLower=False): + """Convert an integer to a roman number. + """ + if numVal < 1 or numVal > 4999: + return "NAN" + + theValues = [ + (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"), + ] + + romNum = "" + for theDiv, theSym in theValues: + for _ in range(numVal//theDiv): + romNum += theSym + numVal -= theDiv + if numVal <= 0: + break + + return romNum.lower if isLower else romNum + +# =============================================================================================== # # Convert an Integer to a Word Number -# =========================================================================== # +# =============================================================================================== # def numberToWord(numVal, theLanguage): """Wrapper for converting numbers to words for chapter headings.