Help with python functions?

K

kjakupak

1.a. Write a function temp(T, from_unit, to_unit) where from_unit and to_unit are temperature units, either 'F' (or 'f') for fahrenheit, or 'C' (or 'c') for celsius, or 'K' (or 'k') for kelvin; and T is a temperature number for the unit from_unit. The function should return the temperature number inthe unit to to_unit; no unit is returned. [input(...) and print(...) are not allowed]

1.b. Write a function comp(T1, u1, T2, u2) where u1, u2 are temperature units as in 1.a., and T1, T2 are temperature numbers for these units. The function should return -1 if T1 in u1 is a lower temperature than T2 in u2; it should return 0 is the two express equal temperatures; and it should return1 otherwise.

2. Write a function P(p_0, t, i) where t is a time in years *integer), is is a yearly interest (as a traditional percentage), and p_0 is the initial principal (at time 0). The function should return the principal at time t. Principals and interests are float numbers.

Can anyone help me with any of these please? Much appreciated. I honestly don't even know how to start them
 
R

Roy Smith

1.a. Write a function temp(T, from_unit, to_unit) where from_unit and to_unit
are temperature units, either 'F' (or 'f') for fahrenheit, or 'C' (or 'c')
for celsius, or 'K' (or 'k') for kelvin; and T is a temperature number for
the unit from_unit. The function should return the temperature number in the
unit to to_unit; no unit is returned. [input(...) and print(...) are not
allowed]

We don't do people's homework for them.

But, I'll give you a hint. Forget about doing this in python. Write
down the steps you would take to do this problem with pencil and paper.
That really is how you begin any programming problem; understanding the
algorithm you need to execute. The rest is, as Mozart said, just
scribbling.
 
S

Steven D'Aprano

Can anyone help me with any of these please? Much appreciated. I
honestly don't even know how to start them

Start by writing a function that does nothing:

def nothing():
pass


Now change it so that it takes three arguments:

T, a temperature
from_unit, the temperature scale to convert from
to_unit, the temperature scale to convert to


def nothing(T, from_unit, to_unit):
pass


Now change the name of the function to match the name you were told to
use:


def temp(T, from_unit, to_unit):
pass


Now you're a third of the way done! All you need do now is write the
logic of the function, and make sure it works.

What's the logic of the function? There are four cases:

(1) If from_unit is "C", and to_unit is "F", you need to convert T from
Celsius to Fahrenheit. I'm sure you can find the formula for that if you
google it.

(2) otherwise, if from_unit is "F", and to_unit is "C", you need to
convert T from Fahrenheit to Celsius. Again, the formula to use is
readily available in about a million reference books and web sites.

(3) otherwise, if from_unit and to_unit are the same (both "C", or both
"F"). In this case, there is no conversion needed and you can just return
T unchanged.

(4) otherwise, one or both of from_unit or to_unit must be something
other than "C" or "F", e.g. "Z" or "Hello". In that case, you have to
deal with the error. Have you learned about exceptions yet?

It may be acceptable to ignore case #4 -- you'll have to ask your teacher.

Then try it out and make sure it works! For example:

107.6°F == 42°C
-15°C = 5°F


Now you're done! On to the next function...
 
K

kjakupak

On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote:

Now you're done! On to the next function...

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)

Would this be correct?
 
T

Terry Reedy

On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote:

Now you're done! On to the next function...

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?
 
K

kjakupak

On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote:

Now you're done! On to the next function...

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)

Would this be correct?
Also, the temperature number had to be of type float so I feel like I did this wrong...

As for the next one, so far I've gotten:
def comp(T1, u1, T2, u2):
if u1 > u2:
return -1
elif u2 > u1:
return 1
else:
return 0
 
D

Dave Angel

On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote:

Now you're done! On to the next function...

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)

Would this be correct?
Also, the temperature number had to be of type float so I feel like I did this wrong...

As for the next one, so far I've gotten:
def comp(T1, u1, T2, u2):
if u1 > u2:
return -1
elif u2 > u1:
return 1
else:
return 0


I didn't see any spec that said Python 3.x. in version 2.x, this would
be incorrect.
 
D

Denis McMahon

As for the next one, so far I've gotten:
def comp(T1, u1, T2, u2):
if u1 > u2:
return -1
elif u2 > u1:
return 1
else:
return 0

If the first function you wrote allows you to convert temps in different
scales to a common scale, then in the second function, you can call the
first function to convert both temps to a common scale, and compare them.

Adding "same scale" conversions in the first function might help. In a
same scale conversion, the input and output units are the same, and the
output value is the input value.

Then to compare T1 in u1 and T2 in u2, convert them both to a common
scale (which might be u1 or u2 or some other scale) using your temp
function, and then compare the resulting values.
 
K

kjakupak

If the first function you wrote allows you to convert temps in different

scales to a common scale, then in the second function, you can call the

first function to convert both temps to a common scale, and compare them.



Adding "same scale" conversions in the first function might help. In a

same scale conversion, the input and output units are the same, and the

output value is the input value.



Then to compare T1 in u1 and T2 in u2, convert them both to a common

scale (which might be u1 or u2 or some other scale) using your temp

function, and then compare the resulting values.

Not sure if we've gotten that far in class, considering I don't know how to go about doing that.

For the third function, I'm actually kind of stumped:
def P(p_0, t, i):
Amount = P(1 + (i/100))
return P(1 + (t * i/12))
 
S

Steven D'Aprano

On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote:

Now you're done! On to the next function...

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?
 
S

Steven D'Aprano

As for the next one, so far I've gotten: def comp(T1, u1, T2, u2):
if u1 > u2:
return -1
elif u2 > u1:
return 1
else:
return 0


That is only comparing the units, not the temperatures. Since the units
are characters, you're essentially saying that any temperature in Kelvin
is always greater than any temperature in Celsius, since "K" > "C".
Worse, you're saying that any two temperatures with the same unit are
automatically equal, so that -50°F == 50000°F.

Instead, you need to convert both temperatures into a common unit, say,
Kelvin, then compare the two temperatures:

* convert T1 from u1 to Kelvin;
* convert T2 from u2 to Kelvin;
* compare T1 and T2.

You don't have to use Kelvin. You could use any temperature scale, so
long as it is the same for both temperatures.
 
S

Steven D'Aprano

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)

Would this be correct?

Oh, I forgot... what happens if both units are the same?

Hint: if both units are the same, no conversion is necessary.
 
D

Denis McMahon

Not sure if we've gotten that far in class, considering I don't know how
to go about doing that.

Which bit aren't you sure about?

(a) adding a "same unit" conversion to the units conversion program?
(Actually, this bit isn't needed after all, you can avoid it with a test
in comp.)

(b) calling temp from comp to establish a common unit?

(c) comparing the returned value of the call to temp with the other temp
in comp

Question, given the original "temp" function as previously described by
yourself, what does the following function "f" which takes the same params
as "comp" do:

def f( t1, u1, t2, u2 ):
if u1 == u2:
return t2
else:
return temp( t2, u2, u1 )
 
D

Denis McMahon

You don't have to use Kelvin. You could use any temperature scale, so
long as it is the same for both temperatures.

Given that he already has a handy conversion function to call, he should
be able to convert t2 into the units of t1 if they're in different units
(2 lines), and then do his comparison (5 lines).
 
G

giacomo boffi

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)

Would this be correct?

not always:
 
M

MRAB

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)

Would this be correct?

not always:
In other words, it depends what you mean by 'correct'.

Zero Kelvin ("Absolute Zero") is the lowest possible temperature; in
reality there's no such temperature as -300°C.
 
D

Denis McMahon

Question, given the original "temp" function as previously described by
yourself, what does the following function "f" which takes the same
params as "comp" do:

def f( t1, u1, t2, u2 ):
if u1 == u2:
return t2
else:
return temp( t2, u2, u1 )

Hmm, maybe:

if u1 == u2:

should have been:

if u1.lower() == u2.lower():
 
K

kjakupak

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):
if from_unit == 'C' or from_unit == 'c':
return 32 + (9/5)*T
elif from_unit == 'K' or from_unit == 'k':
return T + 273.15
elif from_unit == 'F' or from_unit == 'f':
return (5/9)*(T - 32)
else:
return to_unit

# Question 1.b
def comp(T1, u1, T2, u2):
if u1 != u2:
T1 = temp(T1, u1)
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))
 

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,184
Members
46,721
Latest member
DollieSawt

Latest Threads

Top