Lecture 3

Nov. 16th, 2009 05:44 pm
elz: (ada-tubes)
[personal profile] elz posting in [community profile] intro_to_cs
Link to video and handout + Problem set 2






Open to: Registered Users, detailed results viewable to: All, participants: 9


How's it going?

View Answers

Finished lecture 3
9 (100.0%)

Finished problem set 2
3 (33.3%)

Date: 2009-11-17 01:48 am (UTC)
yhlee: Alto clef and whole note (middle C). (Default)
From: [personal profile] yhlee
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*

Date: 2009-11-17 01:53 am (UTC)
zulu: Carson Shaw looking up at Greta Gill (abofal - not a freak you know)
From: [personal profile] zulu
Hee! I'm currently vexed by math.

Date: 2009-11-17 01:56 am (UTC)
yhlee: Alto clef and whole note (middle C). (Default)
From: [personal profile] yhlee
*rue* I majored in math. Just, sometimes coding? Does not come to me by first nature.

Date: 2009-11-17 01:59 am (UTC)
zulu: (sga - emboldened)
From: [personal profile] zulu
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!"

Date: 2009-11-17 02:05 am (UTC)
yhlee: Alto clef and whole note (middle C). (Default)
From: [personal profile] yhlee
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!

Date: 2009-11-17 02:08 am (UTC)
zulu: (house - house begs)
From: [personal profile] zulu
Hey, I've got a question that maybe you can help me with--I left it on the last entry and I don't know if it'll get seen there. I can't figure out how to sum logs; I get an error that says I need a float. I'd be really happy if I was able to put Problem Set 1, at least, behind me!

Date: 2009-11-17 02:15 am (UTC)
yhlee: Alto clef and whole note (middle C). (Default)
From: [personal profile] yhlee
Let's see--
Two things:

1. First thing to try is replace
log
with
math.log
(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.

Date: 2009-11-17 02:22 am (UTC)
zulu: (muppets - huh)
From: [personal profile] zulu
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.

Date: 2009-11-17 04:17 am (UTC)
yhlee: Alto clef and whole note (middle C). (Default)
From: [personal profile] yhlee
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?

zOhDswQJEmkaVNHem

Date: 2012-01-09 04:07 am (UTC)
From: (Anonymous)
An intelligent answer - no BS - which makes a palsenat change

Profile

Introduction to Computer Science

July 2010

S M T W T F S
    123
45678910
11121314151617
18192021222324
2526272829 3031

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 9th, 2025 01:12 pm
Powered by Dreamwidth Studios