Help with python functions?

R

random832

I ended up with these. I know they're only like half right...
I was wondering if any of you had to do this, what would you end up with?

# Question 1.a
def temp(T, from_unit, to_unit):

Assuming this is temperature conversion. You should add a docstring.
if from_unit == 'C' or from_unit == 'c':

Consider normalizing the unit with .upper() at the top of the function
so you don't have to do this "or" case in every single section.
return 32 + (9/5)*T

You are ignoring the value of to_unit and returning the value in
fahrenheit.
elif from_unit == 'K' or from_unit == 'k':
return T + 273.15

This conversion is simply wrong - it's the conversion _from_ celsius
_to_ kelvin.
elif from_unit == 'F' or from_unit == 'f':
return (5/9)*(T - 32)

You are ignoring the value of to_unit and returning the value in celsius
else:
return to_unit

I don't know what this is. It's probably wrong.


To implement a temperature conversion function, I would convert from the
unit given to kelvin, then convert from kelvin to the desired unit -
that way you don't have to implement every combination separately.
# Question 1.b
def comp(T1, u1, T2, u2):
if u1 != u2:
T1 = temp(T1, u1)

You're not passing in u2 here.
elif T2 > T1:
return -1
elif T1 > T2:
return 1
else:
return 0
# Question 2
def P(p_0, t, i):
Amount = P(1 + (i/100))
return P(1 + (t * i/12))

I don't know what this is.

Is this for homework?
 
D

Denis McMahon

I ended up with these. I know they're only like half right...
I was wondering if any of you had to do this, what would you end up
with?
# Question 1.a
def temp(T, from_unit, to_unit):

I suspect that this doesn't work properly for all cases of from_unit,
to_unit.

As a general case:

def temp ( T, u1, u2 ):
# from and to units the same, return T unchanged
# else use a conversion table
ct = { (a, b):lambda x: formula, .... }
return ct[ (u1, u2 ) ]( T )

Note you may need to build in case independence!
# Question 1.b
def comp(T1, u1, T2, u2):

You completely missed the point of my earlier posts, and I suspect the
reason both these questions were included.

Firstly, consider that if temp (from q1a) works properly you can use temp
to convert the units of T2 to the units of T1, by calling:

temp( T2, u2, u1 )

q1b can be implemented in one line if temp from q1a works properly!
# Question 2
def P(p_0, t, i):
Amount = P(1 + (i/100))
return P(1 + (t * i/12))

First calculate the annual interest as 1 + fraction where fraction is
interest % / 100
The calculate the compounded interest as annual ^ years
Finally multiply the compounded interest by the principal

Mathematically:

principal * ( ( 1 + ( period_interest_% / 100 ) ) ^ periods )

Again, this should be possible as a single line function. All you have to
do is turn the math into python code.
 
D

Dennis Lee Bieber

I ended up with these. I know they're only like half right...
I was wondering if any of you had to do this, what would you end up with?

# Question 1.a

Well... Off-hand you aren't making use of "to_unit" -- For Celsius
input you are automatically returning Fahrenheit; for Kelvin input you are
erroneously adding the C->K conversion value, and for Fahrenheit you
automatically generating Celsius. If "from_unit" is not in the set "CcKkFf"
.... "f" : lambda x : (5.0 * (x - 32.0) / 9.0) + 273.15,
.... "k" : lambda x : x }.... "f" : lambda x : (x - 273.15) * 9.0 / 5.0 + 32.0,
.... "k" : lambda x : x }.... fu = from_unit.lower()[0]
.... tu = to_unit.lower()[0] # presumes both are string
.... return from_base[tu]( to_base[fu]( T ) )
....Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
 
T

tn156

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)



What happens if you run some tests? If you use unittest, you can use the

assertAlmostEqualMethod, or just write something similar yourself. Be

careful with values near 0..



At minimum, how many tests do you need, 6 or 9?

can I use elif instead of lambda?
 
T

tn156

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)



What happens if you run some tests? If you use unittest, you can use the

assertAlmostEqualMethod, or just write something similar yourself. Be

careful with values near 0..



At minimum, how many tests do you need, 6 or 9?

can elif be used instead of lambda
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,722
Latest member
NelsonHeil

Latest Threads

Top