Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54631 > unrolled thread

Help with python functions?

Started bykjakupak@gmail.com
First post2013-09-23 05:57 -0700
Last post2013-09-24 15:02 +0000
Articles 20 on this page of 23 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  Help with python functions? kjakupak@gmail.com - 2013-09-23 05:57 -0700
    Re: Help with python functions? Roy Smith <roy@panix.com> - 2013-09-23 09:11 -0400
    Re: Help with python functions? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-23 13:56 +0000
      Re: Help with python functions? kjakupak@gmail.com - 2013-09-23 15:32 -0700
        Re: Help with python functions? Terry Reedy <tjreedy@udel.edu> - 2013-09-23 18:48 -0400
        Re: Help with python functions? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-24 03:08 +0000
        Re: Help with python functions? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-24 03:17 +0000
        Re: Help with python functions? giacomo boffi <pecore@pascolo.net> - 2013-09-24 18:53 +0200
          Re: Help with python functions? MRAB <python@mrabarnett.plus.com> - 2013-09-24 18:18 +0100
      Re: Help with python functions? kjakupak@gmail.com - 2013-09-23 15:55 -0700
        Re: Help with python functions? Dave Angel <davea@davea.name> - 2013-09-24 00:07 +0000
          Re: Help with python functions? kjakupak@gmail.com - 2013-09-23 18:23 -0700
            Re: Help with python functions? Dave Angel <davea@davea.name> - 2013-09-24 03:52 +0000
        Re: Help with python functions? Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-24 02:12 +0000
          Re: Help with python functions? kjakupak@gmail.com - 2013-09-23 19:40 -0700
            Re: Help with python functions? Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-24 14:51 +0000
              Re: Help with python functions? Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-24 19:42 +0000
                Re: Help with python functions? kjakupak@gmail.com - 2013-10-01 10:53 -0700
                  Re: Help with python functions? random832@fastmail.us - 2013-10-01 15:02 -0400
                  Re: Help with python functions? Denis McMahon <denismfmcmahon@gmail.com> - 2013-10-01 21:45 +0000
                  Re: Help with python functions? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-10-01 19:19 -0400
        Re: Help with python functions? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-24 03:15 +0000
          Re: Help with python functions? Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-24 15:02 +0000

Page 1 of 2  [1] 2  Next page →


#54631 — Help with python functions?

Fromkjakupak@gmail.com
Date2013-09-23 05:57 -0700
SubjectHelp with python functions?
Message-ID<e484b709-1287-4e6a-bc43-05f02a608579@googlegroups.com>
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]

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 return 1 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

[toc] | [next] | [standalone]


#54634

FromRoy Smith <roy@panix.com>
Date2013-09-23 09:11 -0400
Message-ID<roy-51492A.09112523092013@news.panix.com>
In reply to#54631
In article <e484b709-1287-4e6a-bc43-05f02a608579@googlegroups.com>,
 kjakupak@gmail.com wrote:

> 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.

[toc] | [prev] | [next] | [standalone]


#54637

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-09-23 13:56 +0000
Message-ID<5240489d$0$29992$c3e8da3$5496439d@news.astraweb.com>
In reply to#54631
On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote:

> 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... 




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#54663

Fromkjakupak@gmail.com
Date2013-09-23 15:32 -0700
Message-ID<66aa7d75-a819-4b21-9f1e-7ad265996150@googlegroups.com>
In reply to#54637
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)

Would this be correct?

[toc] | [prev] | [next] | [standalone]


#54664

FromTerry Reedy <tjreedy@udel.edu>
Date2013-09-23 18:48 -0400
Message-ID<mailman.281.1379976512.18130.python-list@python.org>
In reply to#54663
On 9/23/2013 6:32 PM, kjakupak@gmail.com 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)

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?


-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#54673

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-09-24 03:08 +0000
Message-ID<52410225$0$29992$c3e8da3$5496439d@news.astraweb.com>
In reply to#54663
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

[toc] | [prev] | [next] | [standalone]


#54675

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-09-24 03:17 +0000
Message-ID<5241045a$0$29992$c3e8da3$5496439d@news.astraweb.com>
In reply to#54663
On Mon, 23 Sep 2013 15:32:37 -0700, kjakupak wrote:

> 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.




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#54709

Fromgiacomo boffi <pecore@pascolo.net>
Date2013-09-24 18:53 +0200
Message-ID<87txhap3e7.fsf@pascolo.net>
In reply to#54663
kjakupak@gmail.com writes:

> 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:

>>> temp(-300.0, 'c', 'k')
-26.850000000000023
>>> 


-- 
le mie sacrosante questioni di principio
          VS     gli sciocchi puntigli di quel cretino del mio vicino

[toc] | [prev] | [next] | [standalone]


#54711

FromMRAB <python@mrabarnett.plus.com>
Date2013-09-24 18:18 +0100
Message-ID<mailman.301.1380043085.18130.python-list@python.org>
In reply to#54709
On 24/09/2013 17:53, giacomo boffi wrote:
> kjakupak@gmail.com writes:
>
>> 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:
>
>>>> temp(-300.0, 'c', 'k')
> -26.850000000000023
>>>>
>
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.

[toc] | [prev] | [next] | [standalone]


#54665

Fromkjakupak@gmail.com
Date2013-09-23 15:55 -0700
Message-ID<3af43a58-fc56-46d1-9e1e-cb69f8aa520b@googlegroups.com>
In reply to#54637
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) 

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

[toc] | [prev] | [next] | [standalone]


#54667

FromDave Angel <davea@davea.name>
Date2013-09-24 00:07 +0000
Message-ID<mailman.282.1379981286.18130.python-list@python.org>
In reply to#54665
On 23/9/2013 18:55, kjakupak@gmail.com 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) 
>
> 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.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#54668

Fromkjakupak@gmail.com
Date2013-09-23 18:23 -0700
Message-ID<2c20e376-8803-40e1-acae-e1b8a2b08ebf@googlegroups.com>
In reply to#54667
On Monday, September 23, 2013 8:07:44 PM UTC-4, Dave Angel wrote:
> 
> I didn't see any spec that said Python 3.x.  in version 2.x, this would
> 
> be incorrect.
> 
> 
> 
> -- 
> 
> DaveA

It's for Python 3.2

[toc] | [prev] | [next] | [standalone]


#54676

FromDave Angel <davea@davea.name>
Date2013-09-24 03:52 +0000
Message-ID<mailman.283.1379994743.18130.python-list@python.org>
In reply to#54668
On 23/9/2013 21:23, kjakupak@gmail.com wrote:

> On Monday, September 23, 2013 8:07:44 PM UTC-4, Dave Angel wrote:
>> 
>> I didn't see any spec that said Python 3.x.  in version 2.x, this would
>> 
>> be incorrect.
>> 
>> 
>> 
>> -- 
>> 
>> DaveA
>
> It's for Python 3.2

Then I'd have a comment saying so, right at the top.  Or use that for
the shebang line.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#54669

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-09-24 02:12 +0000
Message-ID<l1qsdl$f3k$1@dont-email.me>
In reply to#54665
On Mon, 23 Sep 2013 15:55:53 -0700, kjakupak wrote:

> 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.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#54671

Fromkjakupak@gmail.com
Date2013-09-23 19:40 -0700
Message-ID<5d5e5c4d-a79c-4b1a-b9da-ee2ad766ded8@googlegroups.com>
In reply to#54669
On Monday, September 23, 2013 10:12:05 PM UTC-4, Denis McMahon wrote:
> 
> 
> 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.
> 
> 
> 
> -- 
> 
> Denis McMahon, denismfmcmahon@gmail.com

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))

[toc] | [prev] | [next] | [standalone]


#54702

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-09-24 14:51 +0000
Message-ID<l1s8tj$4ob$1@dont-email.me>
In reply to#54671
On Mon, 23 Sep 2013 19:40:47 -0700, kjakupak wrote:

> 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 )

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#54716

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-09-24 19:42 +0000
Message-ID<l1spuh$4ob$4@dont-email.me>
In reply to#54702
On Tue, 24 Sep 2013 14:51:31 +0000, Denis McMahon wrote:

> 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():

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#55231

Fromkjakupak@gmail.com
Date2013-10-01 10:53 -0700
Message-ID<1b025b07-3c9c-4390-80e5-8b5661f00d96@googlegroups.com>
In reply to#54716
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))

[toc] | [prev] | [next] | [standalone]


#55235

Fromrandom832@fastmail.us
Date2013-10-01 15:02 -0400
Message-ID<mailman.568.1380654155.18130.python-list@python.org>
In reply to#55231
On Tue, Oct 1, 2013, at 13:53, kjakupak@gmail.com wrote:
> 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?

[toc] | [prev] | [next] | [standalone]


#55240

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-10-01 21:45 +0000
Message-ID<l2ffpk$618$2@dont-email.me>
In reply to#55231
On Tue, 01 Oct 2013 10:53:26 -0700, kjakupak wrote:

> 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.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web