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

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. 10th, 2025 09:55 pm
Powered by Dreamwidth Studios