From the Daily WTF:
“At my company, the powers-that-be determined that, because we rejected a lot of job candidates, my group was ineffective at hiring new employees,”Kendall writes, “thus, the responsibility of hiring new developers was shifted to a group much more proficient at hiring: human resources.”
“That has been going about as well as you might expect, and to make a long story short, we were told to handle any ‘knowledge gaps’ with training. And thus, one of the very first training jobs I give to new employees is to develop a method that translates Roman Numbers to Decimal Numbers. Most struggle with the challenge, but one new hire actually managed to solve the problem:
public string rom2num(string r) { if (r == "I") return "1"; if (r == "II") return "2"; if (r == "III") return "3"; if (r == "IV") return "4"; if (r == "V") return "5"; if (r == "VI") return "6"; if (r == "VII") return "7"; if (r == "VIII") return "8"; if (r == "IX") return "9"; // // Snipped LOTS of "code" here // if (r == "MMVIII") return "2008"; if (r == "MMIX") return "2009"; if (r == "MMX") return "2010"; if (r == "MMXI") return "2011"; return "E"; }
Kendall continues, “when I asked him why the method returns a decimal number as a String, he gave me a disbelieving look and said ‘For returning the error indicator, of course.'”
“I can only hope that the powers-that-be will determine that we are ineffective at training and shift the responsibility of teaching programmers to program to corporate training.”
via: [TheDailyWTF]
Got bored and this sounded like a fun challenge. Here is my python solution (whitespace might be off from the copy/paste):
def rom2num(roman):
numerals = {‘I’: 1,
‘V’: 5,
‘X’: 10,
‘L’: 50,
‘C’: 100,
‘D’: 500,
‘M’: 1000,
‘Q’: 500000}
values = []
roman = roman.upper()
i = 0
for l in roman:
try:
values.append(numerals[l])
except KeyError:
print l, “is not a valid numeral”
return 0
if i == 0:
output = values[i]
else:
output += values[i]
if values[i] > values[i – 1]:
output -= values[i – 1] * 2
i += 1
return output