Created
January 15, 2012 06:11
-
-
Save antsmartian/1614670 to your computer and use it in GitHub Desktop.
Problem solved on Top Coders.(14.2.500)
This file contains 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
//map for managing the conversion from roman to no's | |
def map =[I:1,II:2,III:3,IV:4,V:5,VI:6,VII:7,VIII:8,IX:9,X:10,XI:11] | |
//map for managing the conversion from no to romans | |
def map1 = ['1':"I",'2':"II",'3':"III",'4':"IV",'5':"V",'6':"VI",'7':"VII",'8':"VIII",'9':"IX",'10':"X",'11':"XI"] | |
//temp variables | |
ans = [] | |
res = [] | |
//final result | |
res1 = [] | |
//for managing the flow of algorithm | |
bool = true | |
//yes, its input | |
def input = ["La IV","L IV","La IV","La I","L I"] | |
//main method | |
//map paramter is used to select any one of map either map, or map1 | |
//see above | |
def method(def map,def input) { | |
def a = input | |
def temp = a.split(' ') | |
if(bool == true) | |
f= map.get(temp[1]) | |
else | |
f=map.get(temp[1]) | |
//temp having [asads,2], need to delete 2 and put II! | |
//temp is also used for converting [asdasd, II] to delete II and put 2! | |
//so basically temp[1] gets deleted that is, either no or romans | |
//and at last to the ans, the code adds the required thing | |
ans = temp - temp[1] as List | |
//adds either nos/romans based on vaule of f, which in turn based on | |
//bool value | |
ans << " $f" | |
//managing concurrent modification! so this if is needed here! :( | |
if(bool == true) | |
//for initial sort | |
res << ans.join() | |
else | |
//for final answer format | |
res1 << ans.join() | |
} | |
//do the sort normally | |
if(bool ==true) | |
input.each { method(map,it) } | |
//do the formatting work... | |
//change to bool and call the method again | |
bool =false | |
if(bool == false) | |
{ | |
//this is imp.. because making ans an empty map again as | |
// this list is used in my method. | |
//since ans has value from calling method on bool == true | |
// I need to delete it!, for tidy work.. | |
ans = [] | |
//since I'm formatting res I need to sort it before.. | |
//because thats where I have sorted in this format [a 1, a 2] | |
res.sort() | |
//calling the below method coverts that into [a I,a II] | |
res.each { method(map1,it) } | |
} | |
println res1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment