Created
February 6, 2014 11:52
-
-
Save pirhoo/8842723 to your computer and use it in GitHub Desktop.
WIP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import( | |
| "fmt" | |
| "math" | |
| ) | |
| func Count(number int, count int) (int) { | |
| return int( math.Floor( float64(number/count) ) ) | |
| } | |
| func ToRomans(base int) string { | |
| var number int = base | |
| var output string = "" | |
| var units = map[string]int{ | |
| "M": 1000, | |
| "D": 500, | |
| "C": 100, | |
| "L": 50, | |
| "X": 10, | |
| "V": 5, | |
| "I": 1, | |
| } | |
| for char, n := range units { | |
| count := Count(number, n) | |
| if count > 0 && number >= n { | |
| for i:=0; i < count; i++ { | |
| output = output + char | |
| } | |
| number = number - n * Count(number, n) | |
| } | |
| } | |
| return output | |
| } | |
| func main() { | |
| fmt.Println( ToRomans(1000) == "M" , 1000 , ToRomans(1000) ) | |
| fmt.Println( ToRomans(9000) == "MMMMMMMMM" , 9000 , ToRomans(9000) ) | |
| fmt.Println( ToRomans(10) == "X" , 10 , ToRomans(10) ) | |
| fmt.Println( ToRomans(400) == "CD" , 400 , ToRomans(400) ) | |
| fmt.Println( ToRomans(800) == "DCCC" , 800 , ToRomans(800) ) | |
| fmt.Println( ToRomans(1999) == "MCMXCIX" , 1999 , ToRomans(1999) ) | |
| fmt.Println( ToRomans(448) == "CDXLVIII" , 448 , ToRomans(448) ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment