Precious Metals Forum

Go Back   Precious Metals Forum > Bunker Talk > STS

Reply
 
LinkBack Thread Tools
Old 10-28-2012, 07:43 PM   #1
Predaceous stink bug
 
Join Date: Nov 2011
Posts: 129
Liked: 40 times
is anyone a programmer (python)?

im trying to learn programming, and the tutorial that i am using has been pretty good, but has exercises at the end of each lesson. For the most part, im doing pretty decent with the exercises, but could use a little help on some of them.....if anyone is fluent....
oppie2005 is offline   Reply With Quote
Old 10-29-2012, 06:16 AM   #2
Golden Cockroach
 
PMBug's Avatar
 
Join Date: Oct 2011
Location: In Scrooge McDuck's vault
Posts: 7,029
Liked: 2449 times
I'm a programmer, but I have no experience with Python. If you have questions of a general nature, I can probably help.
__________________
The journey of a thousand miles begins with a single step. - Lao Tzu

Important stuff: PMBug 101 * Forum Guidelines * Support PMBug
PMBug is offline   Reply With Quote
Old 10-29-2012, 07:24 AM   #3
Predaceous stink bug
 
Join Date: Nov 2011
Posts: 129
Liked: 40 times
ok. i dont know if you can help or not, cause im inexperienced, and dont know how similar the "problem" is with other languages.

the exercise says to define a function to give the sum of of all integer numbers up to and including n using the accumulator pattern. the example earlier in the tutorial for the accumulator pattern was for calculating the square root of n.

an earlier exercise was to define the same "sumTo" function, using a different way to calculate it

def sumTo(n):
y = (n * (n+1))/2
return y

Im understanding the accumulator pattern to mean updating a variable for each cycle through a for loop that would add up each number in the range of n, instead of in the example above where multiplication and division is used.

does my ramblings make any sense? i think i have confused myself even further in this process lol.


[edit:]the tutorial that i am using is found here: http://interactivepython.org/coursel...functions.html

where it talks about the accumulator pattern, is about halfway down the page, and the exercise that im working on is near the very bottom of the page.

Last edited by oppie2005; 10-29-2012 at 07:35 AM. Reason: add in reference material
oppie2005 is offline   Reply With Quote
Old 10-29-2012, 07:32 AM   #4
Super Moderator
 
benjamen's Avatar
 
Join Date: Mar 2012
Location: Migratory
Posts: 1,620
Liked: 685 times
In SAS, this would look something like this:

%let maxnum=13;
%macro data_mining;
%do i=1 %to &maxnum;
def sumTo(i):
y = (i * (i+1))/2
%end;
%mend;
%data_mining;


Sorry, I have used a lot of languages, but not Python.
__________________
I drive men mad
For love of me,
Easily beaten,
Never free.

PMBug 101 *** Forum Rules
benjamen is offline   Reply With Quote
Old 10-29-2012, 07:33 AM   #5
Predaceous stink bug
 
Join Date: Nov 2011
Posts: 129
Liked: 40 times
what is SAS?
oppie2005 is offline   Reply With Quote
Old 10-29-2012, 07:34 AM   #6
Golden Cockroach
 
PMBug's Avatar
 
Join Date: Oct 2011
Location: In Scrooge McDuck's vault
Posts: 7,029
Liked: 2449 times
Not sure I fully understood what is meant be "accumulator pattern", but you have two basic choices for calculating the sum of integers in a range from 0..n - an iterative loop (for/next, while/wend, etc.) or a recursive function (likely not the approach they are looking for in an introductory programming course). It would look something like this:
Quote :
def AddOneToN(n)

lSum = 0
FOR lLoopCounter = 1 TO n
lSum = lSum + lLoopCounter
NEXT

return lSum
__________________
The journey of a thousand miles begins with a single step. - Lao Tzu

Important stuff: PMBug 101 * Forum Guidelines * Support PMBug
PMBug is offline   Reply With Quote
Old 10-29-2012, 07:39 AM   #7
Super Moderator
 
benjamen's Avatar
 
Join Date: Mar 2012
Location: Migratory
Posts: 1,620
Liked: 685 times
Originally Posted by oppie2005 View Post:
what is SAS?
It is a language that is good at using large amounts of data and statistical analysis.
__________________
I drive men mad
For love of me,
Easily beaten,
Never free.

PMBug 101 *** Forum Rules
benjamen is offline   Reply With Quote
Old 10-29-2012, 07:45 AM   #8
Predaceous stink bug
 
Join Date: Nov 2011
Posts: 129
Liked: 40 times
pmbug, you, sir, are a genius.

from your example i typed

def sumTo(n):
----runningtotal = 0
----for counter in range(n +1):
--------runningtotal = runningtotal + counter

----return runningtotal

and that was exactly what i was trying to do. i spent an hour or two trying to figure it out last night.


btw, PMBug, what language are you using for that example?

Last edited by oppie2005; 10-29-2012 at 07:51 AM.
oppie2005 is offline   Reply With Quote
Old 10-29-2012, 08:04 AM   #9
Golden Cockroach
 
PMBug's Avatar
 
Join Date: Oct 2011
Location: In Scrooge McDuck's vault
Posts: 7,029
Liked: 2449 times
It's "psuedo code". I tried to mimic your code sample. The "for ... next" loop is BASIC style.
__________________
The journey of a thousand miles begins with a single step. - Lao Tzu

Important stuff: PMBug 101 * Forum Guidelines * Support PMBug
PMBug is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Monty Python 's merchant banker :) bushi STS 0 04-06-2012 11:26 AM


All times are GMT -5. The time now is 01:46 PM.


Powered by vBulletin® from Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.6.0 PL2 ©2011, Crawlability, Inc.
Content of PMBug.com copyright © 2011 - 2019 Measuring Up. All Rights Reserved.