Currently vexed by my inability to handle some state variables for Problem 3 in PS2. Either that or I'm doing something wrong with my nested loops. *sigh* I miss doing this stuff for math class when I didn't have to write code for it. *wry g*
Alas, I hear you. I took Math 31 (high school calculus) oh, about...thirteen years ago? And if I wanted to get really ambitious in my recollections, I'll say that scraped through Math 251 and 253 (more calculus, for Bio majors) with a C- about twelve years ago? Right now I'm blinking at all these chicken mcnuggets and thinking "I thought they said if I learned math I'd never have to work at McDonald's!"
Hee! Yeah, seriously. The versions of this class of problems I encountered before was either weights, postage stamps, or coin denominations. I guess they decided to jazz up the real-world context!
(I couldn't get the former to work for me and don't know whether it's a version-based syntax change or an error or what).
2. Also, from your code:
sum = sum(log(primes)) # this is where I get an error message
First, you've defined
primes
earlier thus:
primes = (2, 3, 5, 7, 11, 13) # this is just as a test case
I'd have to look up the syntax, but that means
primes
is a tuple. I don't believe that
log(primes)
will work because
log
(or
math.log
) needs to operate on a float, it can't operate on the whole tuple--it sees this tuple of 2 3 5 7 11 13 and doesn't know which one to operate on. (It would be fairly easy to define a function to take a tuple of numbers > 0 and return another tuple containing the logs of those numbers, though.)
Also, I'm a little confused by your use of
sum
--did you previously initialize it to something not excerpted in your example code? Because
sum = sum(log(primes)) # this is where I get an error message
makes it look like
sum
is a function acting on something, and then the function is being assigned to itself. Which I guess you could do, but I don't think we've hit recursion yet (and that might not be the syntax to do it in Python anyway).
Ping me if you have any other questions (assuming someone more knowledgeable doesn't get to it first, of course), but hopefully that'll get you started.
Ah, cool, well at least I understand what the error message is telling me now, thanks.
What I was trying to do was to sum up the logs of each item in my tuple (which I also tried with a list and that didn't work either). I thought the point of tuples was that I could do my function on each item and then move on, rather than doing each one individually, like:
log(2) + log(3) + log(5)
et cetera.
I do see the problem now with assigning the name of "sum"--I shouldn't do that because it's one of the restricted words. *nods*
Well, I still don't know how to do this, but now I know one way of not doing it, which is a pretty good start. Shall try again tomorrow when I have teh brainz.
For summing up the logs you'd need a new function that takes in a tuple. Basically:
def logSum(tuple):
and make sure there's a tab indent for the next line, where you define the block of code. What you'll probably want to do is get the length of the tuple, then use that to define a while loop (or you could do this with a for loop but I'd have to look up the syntax), looping over each item in the tuple: take the log, then add it to a running sum variable. At the end you'll want to return the running sum. Does that help a little?
no subject
Date: 2009-11-17 01:48 am (UTC)no subject
Date: 2009-11-17 01:53 am (UTC)no subject
Date: 2009-11-17 01:56 am (UTC)no subject
Date: 2009-11-17 01:59 am (UTC)no subject
Date: 2009-11-17 02:05 am (UTC)no subject
Date: 2009-11-17 02:08 am (UTC)no subject
Date: 2009-11-17 02:15 am (UTC)Two things:
1. First thing to try is replace with (I couldn't get the former to work for me and don't know whether it's a version-based syntax change or an error or what).
2. Also, from your code:
First, you've defined earlier thus:
I'd have to look up the syntax, but that means is a tuple. I don't believe that will work because (or ) needs to operate on a float, it can't operate on the whole tuple--it sees this tuple of 2 3 5 7 11 13 and doesn't know which one to operate on. (It would be fairly easy to define a function to take a tuple of numbers > 0 and return another tuple containing the logs of those numbers, though.)
Also, I'm a little confused by your use of --did you previously initialize it to something not excerpted in your example code? Because
makes it look like is a function acting on something, and then the function is being assigned to itself. Which I guess you could do, but I don't think we've hit recursion yet (and that might not be the syntax to do it in Python anyway).
Ping me if you have any other questions (assuming someone more knowledgeable doesn't get to it first, of course), but hopefully that'll get you started.
no subject
Date: 2009-11-17 02:22 am (UTC)What I was trying to do was to sum up the logs of each item in my tuple (which I also tried with a list and that didn't work either). I thought the point of tuples was that I could do my function on each item and then move on, rather than doing each one individually, like:
log(2) + log(3) + log(5)
et cetera.
I do see the problem now with assigning the name of "sum"--I shouldn't do that because it's one of the restricted words. *nods*
Well, I still don't know how to do this, but now I know one way of not doing it, which is a pretty good start. Shall try again tomorrow when I have teh brainz.
no subject
Date: 2009-11-17 04:17 am (UTC)and make sure there's a tab indent for the next line, where you define the block of code. What you'll probably want to do is get the length of the tuple, then use that to define a while loop (or you could do this with a for loop but I'd have to look up the syntax), looping over each item in the tuple: take the log, then add it to a running sum variable. At the end you'll want to return the running sum. Does that help a little?
zOhDswQJEmkaVNHem
Date: 2012-01-09 04:07 am (UTC)