On Mon, 23 Sep 2013 15:32:37 -0700, kjakupak wrote:
> On Monday, September 23, 2013 9:56:45 AM UTC-4, Steven D'Aprano wrote:
>> On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote:
>>
>> Now you're done! On to the next function...
>>
>>
>>
>> --
>>
>> Steven
>
> def temp(T, from_unit, to_unit):
> conversion_table = {('c', 'k'):lambda x: x + 273.15,
> ('c', 'f'):lambda x: (x * (9.0/5)) + 32,
> ('k', 'c'):lambda x: x - 273.15,
> ('k', 'f'):lambda x: (x * (9.0/5)) - 459.67,
> ('f', 'c'):lambda x: (x - 32) * (5.0/9),
> ('f', 'k'):lambda x: (x + 459.67) * (5.0/9)}
> f = conversion_table[(from_unit.lower(), to_unit.lower())]
> return f(T)
Well, I'm impressed! From "I honestly don't even know how to start them"
to a dispatch table containing first class functions made with lambda in
under 9 hours. Well done!
I expected you to start with a big block of if...elif statements, but a
dispatch table is a much nicer solution.
> Would this be correct?
You tell us :-) Does it work? Are you confident that the conversion
equations are correct? If you try converting various temperatures, do you
get the right results?
--
Steven
--
https://mail.python.org/mailman/listinfo/python-list