Groovy web console

subscribe to the feed Subscribe
to this
site

RNConversion

Published 1 month ago by Tyler Zimmerman
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
def emitRNValue(int quotient, int divisor) {     
    // Declare strokes based on thousand/hundred/ten/one
    Map strokes = [1000:['M','?','?'],                
                    100:['C','D','M'],                      
                     10:['X','L','C'],                       
                      1:['I','V','X']] 
/* Breakpoints: lower/mid/high
 / 1000 = RN (1000)
 / 100 = RN (100)
 / 10 = RN (10)
 / 1 = RN (1)
 / Quotient (0-9) | Divisor (1000, 100, 10, 1) | Contained in the map (ln3-6)
 / Divisor = midpoint
 / Higher = next highest divisor
 */

// begin switchcase
// iterate through 9 cases (1-9), ignoring 0
    switch (quotient) {         
        case 1:      

        case 2: 

        case 3: print strokes[divisor][0] * quotient  
        break

        case 4: print "${strokes[divisor][0]}${strokes[divisor][1]}"             
        break

        case 5: print strokes[divisor][1]             
        break 

        case 6:

        case 7:

        case 8: print "${strokes[divisor][1]}" + strokes[divisor][0] * (quotient - 5)             
        break

        case 9: print "${strokes[divisor][0]}${strokes[divisor][2]}"             
        break

        default:             
        break      
    } 
}

// Main process to run the conversion 
def processNumber(int n) {     
    print "Number: ${n}\n"     
    int divisor = 1000     
    while (n > 0) {         
        int quotient = n.intdiv(divisor);
        emitRNValue(quotient, divisor)
        n = n - quotient * divisor
        divisor = divisor.intdiv(10)
    }
println()
}
static void main(String[] args) {
    processNumber(3569)
    processNumber(1234)
}