This is now my current code after what user2486 said.
def romanMap():
map=((\"M\", 1000),(\"CM\", 900),(\"D\", 500),(\"CD\", 400),(\"C\", 100),(\"XC\"
try this:
def translate(string):
values = {"i":1, "v":5, "x":10, "l":50, "c":100, "m":1000}
return sum(map(lambda x: values[x], string))
The lambda stands for a one-line function. That's why they are called anonymous functions. You don't have to define them outide using def and all that formality.
You can type something like this on the shell:
f = lambda x: x + 3 f(3) 6 or f = lambda x,y: x + y f("foo", "bar") 'foobar'
Im using map to apply my new-born function into every element of a iterable. In this case, the iterable is one string like "mclvii". Doing it i generated a list where each value its his rersepective value. See a lambda example to calculate squares:
>>> a = [1,2,3,4]
>>> square = lambda x: x**2
>>> l = map(square, a)
>>> l = [1,4,9,16]
So, it's lambda when you need a function on the fly, and map when you want to apply a function to all elements in a list.
Now a example using recursion:
def translate2(string):
values = {"i":1, "v":5, "x":10, "l":50, "c":100, "m":1000}
if not string:
return 0
return values[string[0]] + translate2(string[1:])