Problem in code

yes that looks better

Thanks

Might have missed some others, XC for 90, and who knows what else. When we get into big numbers all h e l l breaks loose. It’s not like they were translating to Arabic numerals or had a handle on the decimal system. Roman Numeral math is outright hard. It takes a lot of thought.

MCMXLIV

Yeah, I’m hoping you’ll compute this. The optimal word there is ‘compute’ since that is how roman numerals work, they are sums. Compute the character, and if the character is preceded by as lesser degree character, subtract the lesser from the greater then compute forward the result.

And blah, blah, blah. Proselytizing this system today would be insanity.


###  converting an integer number to a roman number

class py_solution:

    def int_roman(self,num):

# count iterations

        count = 0

# create a string to store the result

        answer = ""

# create a dictionary to store the roman numbers

        roman_num = {"M":1000,"D":500,"C":100,"L":50,"XX":20,"X":10,"V":5,"IV":4,"I":1}

        for key, value in roman_num.items():

                if value - num == 100:

                        answer = "C" + key

                        num = num - num

                if num > value:

                        answer += key

# get the remainder

                        num = num - value

# Ensure that remaninder divided by value == 0

                if num % value == 0:

# find the roman numbers needed

                        rom_needed = num // value

# add that to the answer

                        answer += rom_needed * key

                        num = num - (rom_needed * value)

                if num == 0:

                        return answer

print(py_solution().int_roman(1100))

print(py_solution().int_roman(900))

I would just add values to the dictionary. This is the version I envisioned.

###  converting an integer number to a roman number

class py_solution:

    def int_roman(self,num):

# create a string to store the result

        answer = ""

# create a dictionary to store the roman numbers

        roman_num = {"M":1000, "CM":900, "D":500, "CD":400, "C":100,"XC":90, "L":50, "XL":40, "X":10,"XI":9, "V":5,"IV":4,"I":1}

# for each value in dictionary roman_num, add keys to answer while num is greater than value

        for key,value in roman_num.items():
            
            while num >= value:

                answer += key

                num -= value

                print(f'Conversion status: {num} + {answer}')

        return answer

print(py_solution().int_roman(1944))
1 Like

Isn’t that eleven? Just ribbing you.

1 Like

Slightly off topic (in another language)

This version deconstructs the decimal number and builds up the Roman, making slight corrections along the way.

function convertToRoman(num) {
  if (num > 4999) {
    throw Error('Number exceeds common range')
  }
  let n = num
  const m = Math.floor(n / 1000)
  n %= 1000
  const d = Math.floor(n / 500)
  n %= 500
  const c = Math.floor(n / 100)
  n %= 100
  const l = Math.floor(n / 50)
  n %= 50
  const x = Math.floor(n / 10)
  n %= 10
  const v = Math.floor(n / 5)
  n %= 5
  const i = n
  //console.log(m, d, c, l, x, v, i)
  const M = "M".repeat(m)
  const D = "D".repeat(d)
  let C = "C".repeat(c)
  if (c === 4) {
      C = "CD"
  }
  const L = "L".repeat(l)
  let X = "X".repeat(x)
  if (x === 4) {
      X = "XL"
  }
  const V = "V".repeat(v)
  let I = "I".repeat(i)
  if (i === 4) {
      I = "IV"
  }
  //console.log(M, D, C, L, X, V, I)
  let roman = `${M}${D}${C}${L}${X}${V}${I}`
  roman = roman.replace("DCD", "CM")
  roman = roman.replace("LXL", "XC")
  roman = roman.replace("VIV", "IX")
  //console.log(roman)
  return roman
}

convertToRoman(9);

It shouldn’t be too hard to port over to Python.

1 Like

Oops, having scanned over the wiki page on roman numerals I’m very happy we went with Arabic. Slightly daunted by binary too, simple and yet so foreign.

That’s JavaScript? It’s good to see that learning one language makes the next ones easier to pick up.

2 Likes

Correct. Yes, once we learn one language, others are easier to pick up.

1 Like

Ported code

def convertToRoman(num):
  if num > 4999:
    return f"Number ({num}) exceeds common range"
  
  n = num
  m = n // 1000
  n %= 1000
  d = n // 500
  n %= 500
  c = n // 100
  n %= 100
  l = n // 50
  n %= 50
  x = n // 10
  n %= 10
  v = n // 5
  n %= 5
  i = n
  # print(m, d, c, l, x, v, i)
  M = "M" * m
  D = "D" * d
  C = "C" * c
  if c == 4:
      C = "CD"

  L = "L" * l
  X = "X" * x
  if x == 4:
      X = "XL"
  
  V = "V" * v
  I = "I" * i
  if i == 4:
      I = "IV"

  # print (M, D, C, L, X, V, I)
  roman = f"{M}{D}{C}{L}{X}{V}{I}"
  roman = roman.replace("DCD", "CM")
  roman = roman.replace("LXL", "XC")
  roman = roman.replace("VIV", "IX")
  # print (roman)
  return roman

print (convertToRoman(1944))    # MCMXLIV
2 Likes

Revised the normalization process and simplified the assignments with conditional expressions::

def convertToRoman(n): if n > 4999: return f"Number ({num}) exceeds common range" y = [] for x in [1000, 500, 100, 50, 10, 5, 1]: y += [n // x] n %= x m, d, c, l, x, v, i = y C = "CD" if c == 4 else "C" * c X = "XL" if x == 4 else "X" * x I = "IV" if i == 4 else "I" * i R = f"{'M' * m}{'D' * d}{C}{'L' * l}{X}{'V' * v}{I}" return R.replace("DCD", "CM").replace("LXL", "XC").replace("VIV", "IX") print (convertToRoman(1944))

Can’t help it. I’m watching the Olympics and just want to do better. Sub-15 isn’t bad.


Whoo-hoo! Got it into the Codebyte window with no scroll.


Method chaining on line 12 is also supported in JS. Now to port this revision back to JS.

1 Like
function convertToRoman(n) { if (n > 4999) return `Number (${num}) exceeds common range` const y = [] for (let x of [1000, 500, 100, 50, 10, 5, 1]) { y.push(Math.floor(n / x)); n %= x } [m, d, c, l, x, v, i] = y const C = c == 4 ? "CD" : "C".repeat(c) const X = x == 4 ? "XL" : "X".repeat(x) const I = i == 4 ? "IV" : "I".repeat(i) const R = `${'M'.repeat(m)}${'D'.repeat(d)}${C}${'L'.repeat(l)}${X}${'V'.repeat(v)}${I}` return R.replace("DCD", "CM").replace("LXL", "XC").replace("VIV", "IX") } console.log(convertToRoman(1944))

Sub-15.

This teaches us that porting to another language can open inroads to optimization.

One rather doubts that staring at JS this solution would come about as easily as it did.


Edited to correct the omission bug in the three conditional expressions (the ternaries). Left off changing * back to .repeat.

1 Like

what does this do? is it python, l have tried searching

Thanks

It is common to all languages that support augmented assignment. As we can see, it is supported in both Python and JavaScript.

+=
*=
-=
/=
//=
**=
%=    =>  modulo assignment

Search for ‘assignment operators’.

1 Like