Skip to content

Instantly share code, notes, and snippets.

@BonfaceKilz
Created March 16, 2021 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BonfaceKilz/d0af4fab8a70b080b0666e198e96a0fe to your computer and use it in GitHub Desktop.
Save BonfaceKilz/d0af4fab8a70b080b0666e198e96a0fe to your computer and use it in GitHub Desktop.
Prelude

Last week I wasn't able to post anything. I was down with flu(?); but now I'm doing way better. What I've been thinking about? How do I semi-automate creating this posts? Food for thought!

Previous Challenge

Issue 3: https://is.gd/EKppEK

Mailing List Discussion: https://is.gd/sgWOpT

This Week's Challenge

Source: E-mail from https://jnduli.co.ke/pages/about.html

[While checking ^^, see the comics at: https://comics.jnduli.co.ke/pub/contract-manipulation/]

Write an algorithm that converts integers to roman numerals. For ease of use, lets limit ourselves to the following roman characters:

    I - 1
    V - 5
    X - 10
    L - 50
    C - 100
    D - 500
    M - 1000

General rules: the standard form section from wikipedia: https://en.wikipedia.org/wiki/Roman_numerals

Post your answers here as comments.

@ianmuchina
Copy link

python solution

import math

def int_to_roman(num: int):
    if num > 3999:
        return

    #Declare variables
    index, ans = 0, ""

    characters = ['I', 'V', 'X', 'L', 'C', 'D', 'M', '', '']

    while num != 0:
        a = num % 10
        num = math.trunc(num / 10)

        # Characters to use
        i = characters[index]
        v = characters[index + 1]
        x = characters[index + 2]

        # Get numeral and append to answer
        ans = digit_to_roman(i, v, x, a) + ans
        # Move to next set of characters
        index += 2

    return ans

def digit_to_roman(i: str, v: str, x: str, num: int):
    if num <= 3:
        return num * i
    if num == 4:
        return i + v
    if num == 5:
        return v
    if num <= 8:
        return v + i * (num - 5)
    if num == 9:
        return i + x
    return

@kodesoul
Copy link

kodesoul commented Mar 17, 2021

Solution in Rust

// Object representing roman numeral.
struct Roman(u32);

impl Iterator for Roman {
    type Item = &'static str;
    fn next(&mut self) -> Option<Self::Item> {
        // check against possible values.
        let roman = match self.0 {
            x if x >= 1000 => (1000,"M"),
            x if x >= 900 => (900,"CM"),
            x if x >= 500 => (500,"D"),
            x if x >= 400 => (400,"CD"),
            x if x >= 100 => (100,"C"),
            x if x >= 90 => (90,"XC"),
            x if x >= 50 => (50,"L"),
            x if x >= 40 => (40,"XL"),
            x if x >= 10 => (10,"X"),
            x if x >= 9 => (9,"IX"),
            x if x >= 5 => (5,"V"),
            x if x >= 4 => (4,"IV"),
            x if x >= 1 => (1,"I"),
            _ => (0," "),
        };

        // return roman character
        match roman {
            (0," ") => None,
            (x,s) =>  {
                self.0 -= x;
                Some(s)
            }
        }
    }
}

fn get_roman(x: u32) -> String {
    let num = Roman(x);

    // Get string representation
    num.collect()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment