001
This commit is contained in:
85
roman_numeral_encoder.v
Normal file
85
roman_numeral_encoder.v
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
import readline
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
mut r := readline.Readline{}
|
||||||
|
answer := r.read_line('What is your number: ')!
|
||||||
|
|
||||||
|
symbols := {
|
||||||
|
1: 'I'
|
||||||
|
5: 'V'
|
||||||
|
10: 'X'
|
||||||
|
50: 'L'
|
||||||
|
100: 'C'
|
||||||
|
500: 'D'
|
||||||
|
1000: 'M'
|
||||||
|
}
|
||||||
|
|
||||||
|
a := answer.int()
|
||||||
|
if a == 0 {
|
||||||
|
panic('The value cannot be 0 or a string')
|
||||||
|
}
|
||||||
|
|
||||||
|
mut num := a
|
||||||
|
mut result := ''
|
||||||
|
|
||||||
|
// Handle thousands
|
||||||
|
mut m := num / 1000
|
||||||
|
for i := 0; i < m; i++ {
|
||||||
|
result += symbols[1000]
|
||||||
|
}
|
||||||
|
num = num % 1000
|
||||||
|
|
||||||
|
// Handle hundreds
|
||||||
|
mut c := num / 100
|
||||||
|
if c == 9 {
|
||||||
|
result += symbols[100] + symbols[1000]
|
||||||
|
} else if c >= 5 {
|
||||||
|
result += symbols[500]
|
||||||
|
for i := 0; i < c - 5; i++ {
|
||||||
|
result += symbols[100]
|
||||||
|
}
|
||||||
|
} else if c == 4 {
|
||||||
|
result += symbols[100] + symbols[500]
|
||||||
|
} else {
|
||||||
|
for i := 0; i < c; i++ {
|
||||||
|
result += symbols[100]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
num = num % 100
|
||||||
|
|
||||||
|
// Handle tens
|
||||||
|
mut x := num / 10
|
||||||
|
if x == 9 {
|
||||||
|
result += symbols[10] + symbols[100]
|
||||||
|
} else if x >= 5 {
|
||||||
|
result += symbols[50]
|
||||||
|
for i := 0; i < x - 5; i++ {
|
||||||
|
result += symbols[10]
|
||||||
|
}
|
||||||
|
} else if x == 4 {
|
||||||
|
result += symbols[10] + symbols[50]
|
||||||
|
} else {
|
||||||
|
for i := 0; i < x; i++ {
|
||||||
|
result += symbols[10]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
num = num % 10
|
||||||
|
|
||||||
|
// Handle ones
|
||||||
|
if num == 9 {
|
||||||
|
result += symbols[1] + symbols[10]
|
||||||
|
} else if num >= 5 {
|
||||||
|
result += symbols[5]
|
||||||
|
for i := 0; i < num - 5; i++ {
|
||||||
|
result += symbols[1]
|
||||||
|
}
|
||||||
|
} else if num == 4 {
|
||||||
|
result += symbols[1] + symbols[5]
|
||||||
|
} else {
|
||||||
|
for i := 0; i < num; i++ {
|
||||||
|
result += symbols[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println('${a} --> ${result}')
|
||||||
|
}
|
Reference in New Issue
Block a user