Lecture 2

Nov. 11th, 2009 05:21 pm
elz: (ada-tubes)
[personal profile] elz posting in [community profile] intro_to_cs






Problem set 1: Computing prime numbers, product of primes

This one's a little trickier, so feel free to comment if you run into any problems or want to brainstorm with other people!

(eta: having issues with poll, alas)

Date: 2009-11-11 10:42 pm (UTC)
phoenix: ink-and-watercolour drawing -- girl looking calmly over her shoulder (Default)
From: [personal profile] phoenix
Ooh, I'm proud of myself. I watched this lecture earlier in the week because I knew my time later in the week would be pretty limited and so I'd started the problem set too. I couldn't understand the second one, though. I was going to ask here and see if someone could help me out, and in phrasing the question, worked out what I'd misunderstood before. :)

[personal profile] elz, you may need to create the poll in another (public) entry. I think polls can only be created at entry posting time, not at edit time.

NASnMmOJgL

Date: 2012-01-06 10:20 pm (UTC)
From: (Anonymous)
Please teach the rest of these internet holoigans how to write and research!

Date: 2009-11-12 12:06 am (UTC)
gchick: Small furry animal wearing a tin-foil hat (Default)
From: [personal profile] gchick
while box == ticky:
    [x]

Date: 2009-11-12 04:30 am (UTC)
gchick: Small furry animal wearing a tin-foil hat (Default)
From: [personal profile] gchick
Of course, if you believe, as all right-thinking people must, that ticky box is ALWAYS ticky, it's an infinite loop. But who am I to resist?

Date: 2009-11-12 05:06 am (UTC)
From: [personal profile] ex_wicker969
Hey, I'm all for being a bit of a smartass. I realized I misread the question so I'm going back through all the Python Programming Wikibook examples, which led to this:

question = "Which prime do you wish to find?"
print (question)
n = raw_input()
print ("You wish to find the " + n + "th prime? Fat chance of THAT!"

aqgHeNVyWCUz

Date: 2012-01-07 05:23 am (UTC)
From: (Anonymous)
I rekcon you are quite dead on with that.

Date: 2009-11-12 12:13 am (UTC)
From: [personal profile] ex_wicker969
Man it is times like these when I know how non-mathy my brain is. I got all the primes between 1 and 1000 but my code is ungainly and ugly and I kind of just printed 2, 3, 5, and 7 before I got the ball rolling on the rest by nesting 'if' and n%2!=0, n%3!=0 ... etc.

Anybody else make a pretty ugly program? I'm gonna go try it again at some point tonight. How'd your approach differ from mine?

Date: 2009-11-12 01:30 am (UTC)
ungemmed: (Default)
From: [personal profile] ungemmed
hint: You may want to put the primes in a list as you find them.

Date: 2009-11-12 05:55 am (UTC)
From: [personal profile] ex_wicker969
Thank you! I'm giving it another shot and I think I'm starting to get the hang of it.

(I realized I totally got off-track and tried to get an answer the problem didn't ask for.)

pXWMiprUPTOVcxLS

Date: 2012-01-07 09:28 am (UTC)
From: (Anonymous)
Weeeee, what a quick and easy souliton.

MoDeXpCzNyFKFCSvSS

Date: 2011-08-14 05:14 am (UTC)
From: (Anonymous)
You're the gretaset! JMHO

vqiwThxRBr

Date: 2012-01-09 05:19 am (UTC)
From: (Anonymous)
If my problem was a Death Star, this atircle is a photon torpedo.

Date: 2009-11-12 12:16 am (UTC)
rodo: chuck on a roof in winter (Default)
From: [personal profile] rodo
I give up. I've been staring at the first problem for an hour and still have no clue where to start, while I don't even understand the second problem.

Date: 2009-11-12 01:49 am (UTC)
rodo: chuck on a roof in winter (Default)
From: [personal profile] rodo
I've come that far. In English, everything makes perfect sense. I just can't put it into code. At all.

Date: 2009-11-12 02:14 am (UTC)
rodo: chuck on a roof in winter (Default)
From: [personal profile] rodo
I think my problem is that I generally do get the problem itself (even though prime numbers were something I never really understood, for some reason), but I have absolutely no idea how to write code. I can understand it, the examples all make sense to me, but I can't really produce any myself. It's as if I had just learned to say "hello" and "how are you" in a foreign language, and then someone suddenly asks me for a detailed description of my day. I know what I want to say, obviously, but I have no idea how to say it.

I think this is a problem that I'll be having with this, if I choose to continue. The lecture and the reading material don't seem to be particularly helpful either.

Date: 2009-11-12 05:30 am (UTC)
ungemmed: (Default)
From: [personal profile] ungemmed
For what it's worth, when I was first learning to code, I Really Didn't Get It for a while, and I needed to have my friends sit by me and hold my hand and walk me through examples like I was the dumbest person to ever walk the earth.

After a while, though, I did hit critical mass, and the learning curve flattened out a *lot.* I know that "keep on trying, you'll get it" isn't always the most useful thing to say in situations like this, but....

It might be useful for you to, for a while, explicitly write out the pseudocode for anything you want to code. That way, you're forcing yourself to think algorithmically, but you're writing English steps instead of Python ones, so it's a little more familiar.

For example, if you're checking to see that a number is prime, then the pseudocode might look like:

n = the number we're checking
start with x = 2, 'cause it's the smallest prime
while x < n
if n is divisible by x
n is not prime, we can stop checking now
otherwise,
n might still be prime!
set x to the next value to check divisibility with, and run through the loop again

Date: 2009-11-12 05:51 am (UTC)
rodo: chuck on a roof in winter (Default)
From: [personal profile] rodo
My problem isn't that I can't think how the code is supposed to work. I can do that, so your example isn't really helping me any. My problem is that I have no clue how to put you example into actual working code. This might be the most obvious thing ever for you, but for me, it's the thing that's standing in my way. I've run into this problem before with HTML, so it isn't new. With HTML, I managed to work my way through it by tinkering with existing and working code to figure out how to adapt it to my purposes (I'm still starting with a collage of copypasta rather than actual writing when I'm not comfortable with an element). I haven't found a similar way to deal with PHP, and I'm not sure if there is such a thing for Python since it seems more specific to me.

Date: 2009-11-12 06:03 am (UTC)
From: [personal profile] ex_wicker969
I don't know if I'm helping or being annoying but I went back and worked through each example in the Python Programming Wikibook and tinkered with each one, seeing what variations did, so I still have no clue how to implement ungemmed's pseudocode yet (that's about as far as I got with my own) but at least I'm getting the idea of how to define variables and manipulate them.

I'm a lot like you, I think. This is my first non-HTML code because I self-taught in HTML by taking other people's source code and manipulating it to see how they'd done what they did. Hang in there a little longer and see if it clicks? If not, I don't mean to push you or frustrate you more.

Date: 2009-11-13 09:33 pm (UTC)
newrambler: sunrise outside my old trailer (Default)
From: [personal profile] newrambler
I'm glad to read these comments--I haven't had a chance to start working on this problem set for real yet, but I had to talk myself down from panicking. I'm going to try your suggestion and work through the rest of the examples, and perhaps some of it will start to sink in.

I'm grateful to have a community working on this stuff--thanks to all of you who have commented.

Date: 2009-11-14 02:40 am (UTC)
From: [personal profile] ex_wicker969
We can do this!

yvixSJkxBtcbHfBLqiT

Date: 2012-01-09 04:22 am (UTC)
From: (Anonymous)
This is way better than a brick & mortar etsablishemnt.

Date: 2009-11-12 06:21 am (UTC)
ungemmed: (Default)
From: [personal profile] ungemmed
Hmm, okay. I sometimes run into the same problem, I think every coder does--I mean, obviously you're running into it more than most, but I've definitely had that "argh, why do computers not speak English" feeling before.

If it helps, here's how I'd translate the pseudocode I just gave you into real python:

n = the number we're checking
n = int(raw_input('n? '))

(raw_input prompts the user and returns their answer; we need to typecast the input from a string to a number using int, or otherwise the computer will think it's text and whine at us)
start with x = 2, 'cause it's the smallest prime
x = 2

(nothing really fancy there)
while x < n
while x < n:

(another place where the python bears a striking resemblance to the pseudocode)
    
if n is divisible by x
    if x % n == 0:

(use the modulus function to see if x divides evenly into n)
        
n is not prime, we can stop checking now
        break

(breaks you out of the while loop, and sets your "instruction counter" at the instruction immediately after it)
    
otherwise,
    else
*
        
n might still be prime!
(since our program is assuming n is prime until told otherwise, already, there's no need to translate this into code)
        
set x to the next value to check divisibility with
        x += 1

(sets x to old-x + 1)**
        
and run through the loop again
(you don't actually need to put anything here for this, in python. When the interpreter reaches the end of a while loop, it will automatically go back to the top, assuming the loop condition (x < n) still holds true)

*You don't actually need to use an else case for this, because there's a break statement in the if case, but it's a more direct translation of the pseudocode.
**It would be faster to only check n's divisibility with just primes, rather than with all integers, but that is beyond the scope of this simple example. ^^

Date: 2009-11-16 05:15 am (UTC)
From: [personal profile] ex_wicker969
I need a lightbulb icon! I just tried to write a comment telling you how I got it but I rambled but I think I've at least got how I was thinking in the wrong direction, basically. I couldn't even start. Thank you!

Date: 2009-11-16 05:18 am (UTC)
ungemmed: (Default)
From: [personal profile] ungemmed
I'm really glad I could help!

LjdywZEBondMs

Date: 2011-08-14 05:08 am (UTC)
From: (Anonymous)
A good many vablulaes you've given me.

Date: 2009-11-12 09:29 am (UTC)
person:  Photo of 'Hello. My name is Person' nametag. (Default)
From: [personal profile] person
Though your pseudocode and translation aren't particularly helpful to me at the moment, reading about your past need for handholding does. I have a feeling I'm going to need to sit with a programming pal this evening and try to work it out, but it's comforting to know that that may not mean I'm doomed to never be capable on my own. So, thank you!

JQGKyQUiLhGaNrGPR

Date: 2011-08-14 06:59 am (UTC)
From: (Anonymous)
This piece was cogent, well-witretn, and pithy.

jBLQAbcTXgxeuAast

Date: 2012-01-07 08:24 am (UTC)
From: (Anonymous)
Superior thinking dmoenrstated above. Thanks!

Date: 2009-11-14 08:34 pm (UTC)
ephemera: (Cold Shoulder)
From: [personal profile] ephemera
It's as if I had just learned to say "hello" and "how are you" in a foreign language, and then someone suddenly asks me for a detailed description of my day. I know what I want to say, obviously, but I have no idea how to say it.

If it makes you feel any better, I've been drowning in that feeling while working my way through the *very first* exercise (I'm running late - lecture 2 is for tomorrow for me) and thank you for pinning it down so exactly!

I was feeling like most of the reading material was also written in that same foreign language, but I did get there in the end.

I'm hoping it will all get easier with practise!

BbJLPbCRReuw

Date: 2012-01-06 10:05 pm (UTC)
From: (Anonymous)
I never thought I would find such an everdayy topic so enthralling!

Date: 2009-11-12 12:35 am (UTC)
erda: (Default)
From: [personal profile] erda
Is anybody else trying to do just one lecture a week? I know I won't have time for two.

Date: 2009-11-12 01:06 am (UTC)
ghoti: fish jumping out of bowl (Default)
From: [personal profile] ghoti
I am! Still working on getting through the first lecture and problem set.

Date: 2009-11-12 02:00 am (UTC)
erda: (Default)
From: [personal profile] erda
Oh, good. Cause I feel a little like everyone is leaving me behind.

Date: 2009-11-12 02:11 am (UTC)
ghoti: fish jumping out of bowl (Default)
From: [personal profile] ghoti
We can be slow together :)

Examples used in Lecture

Date: 2009-11-14 12:06 pm (UTC)
bookchan: endless sky (Default)
From: [personal profile] bookchan
I typed up the examples he used while teaching since we don't have the handouts. This might help to get a better understanding of what he was doing. Everything is commented out also, so don't forget to remove the pound sign before playing. ^_^ Hopefully I typed it all correctly, I haven't played around with it yet.

ETA or you can just go to the lecture video at the MIT website and they have the handout right there.

Examples of overloading
3*4
3*'ab'
'a' + 'bcd'
3 + 'ab'
str(3)+'ab'
'a'<3
4<'3'
'4'<'3'
The code he used

##x=3 #create variable x and assign value 3 to it
##x=x*x # bind x to value 9
##print x
##n = raw_input('Enter a number; ')
##print n
##print n/n

##x=15
##if (x/2)*2 == x:
## print 'Even'
##else: print 'Odd'

##x=15
##if (x/2)*2 == x: print 'Even'
##else: print 'Odd'

##z='b'
##if 'x' < z :
## print 'Hello'
## print 'Mom'

##if 'x'< z :
## print 'Hello'
##print 'Mom'

##x=15
##y=5
##z=11
##print x,y,z
###Is this right?
##if x< y:
## if x< z : print 'x is least'
## else: print 'z is least'
##else: print 'y is least'
##
##if x < y and x < z : print 'x is least'
##elif y < z : print 'y is least'
##else: print 'z is least'


##y=0
##x=3
##itersLeft = x

##while (itersLeft>0):
## y= y+x
## itersLeft = itersLeft -1
###print 'y =',y,',intersLeft=',intersLeft
##print y

##x=10
##i=1
##while(i<x): ## if x%i == 0: ## print 'divisor ',i ## i = i+1 ##x = 10 ##for i in range(1,x): ## if x%i == 0: ## print 'divisor ',i
Edited Date: 2009-11-15 08:37 am (UTC)

hqFbGesiTVFUz

Date: 2012-01-09 03:48 am (UTC)
From: (Anonymous)
Keep these articles cminog as they've opened many new doors for me.

Profile

Introduction to Computer Science

July 2010

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

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 9th, 2025 08:11 am
Powered by Dreamwidth Studios