Winston_Churchill
Bazinga!
+521|6984|Toronto | Canada

we have an assignment to write 10 or so python programs and i've gotten all but 2 so far.  the ones im stumped on are:

For this part of the assignment, you will write a function to evaluate the mathematical exponential function, ex. Your Python function will be called badexp(x), because it really won't work very well, at least for some values of x.

Using the badexp formula:
term[i+1] = term[i] * x / (i + 1)

For your badexp function, you will certainly need to write a loop, but because of equation (7), you will not need to write a loop inside a loop (a "doubly-nested loop").

To write badexp, make a sum of the terms in the mathematical formula, starting with i = 0. You can't keep going forever, so stop as soon as adding a new term to the sum doesn't change the sum. That will certainly happen eventually, because for large i the terms become very small.
I could figure out how to write a better exp function, but this one is ridiculous and I cant figure out the loop.  What I have so far:

Code:

def badexp(x):
    i = 0
    term1 = 0
    term2 = x / (i + 1)    
    while term1 != term2:
        term2 = term1 * x / (i + 1)
    print term2
But that obviously doesnt work

The other one is more complex
This function takes two parameters, url and line_number. It returns the first word in the line_numberth line of url. Line numbers are to be counted from 1, not from 0.

A "word" is defined as a sequence of alphabetic characters — letters — and you must find the first word by looking at the characters in the line one by one. Python strings have a function isalpha that will help you here. Your function must return None if line_number is not positive or is greater than the number of lines in url, or if there are no words in the line.

This function is harder to write. You will need more than one loop, at the very least.
My attempt so far:

Code:

def first_word(url, line_number):
    i = 1
    if line_number > lines(url):
        return None
    if line_number <= 0:
        return None
    while i <= line_number:
        stream = urlopen(url)
        line = stream.readline()
        #line = url
        if line[0] == ' ':
            return None
        word1 = ''
        x = 0
        while line[x] != ' ':
            word1 += line[x]
            x = x + 1
        print word1
        i = i + 1
Any help on either would be amazing <3
bf2gammer
Member
+14|6466

Code:

def badexp(x):
    i = 0
    term1 = 0
    term2 = x / (i + 1)    
    while term1 != term2:
        term2 = term1 * x / (i + 1)
    print term2
I think it is saying you need to loop the variable i and print the output until the output value no longer changes. I dont know python but you will need to increase the value of i for each loop, after googling i found that "i += 1" is the way python does this.

Here is a start using your function as an outline "note i dont know python"

Code:

def badexp(x):
    i = 0
    term1 = 0
    term2 = x / (i + 1)   
    prev = 0
    while term1 != term2:
        term2 = term1 * x / (i + 1)
        if prev == term2:
            break
        else:
            prev = term2
        i += 1
    print term2
It says to stop the loop if adding a new term to the sum doesnt change the value so I put an if statement that check to see if the variable prev equals term2, if not it will set the variable prev to the value of term2 in order for the loop to check to see if the next term equals the previous term.


I hope this works, if not I hope I helped a little bit,
bf2gammer
Member
+14|6466
after looking at it again i noticed that term1 != term2 was redundent so...

Code:

def badexp(x):
    i = 0
    term1 = 0
    term2 = x / (i + 1)   
    prev = 0
    while prev != term2:
        term2 = term1 * x / (i + 1)
        prev = term2
        i += 1
    print term2
I also noticed term1 was always 0 so..

Code:

def badexp(x):
    i = 0
    term1 = 0
    term2 = x / (i + 1)   
    prev = 0
    while prev != term2:
        term2 = i * x / (i + 1)
        prev = term2
        i += 1
    print term2
Winston_Churchill
Bazinga!
+521|6984|Toronto | Canada

Yeah, I kinda put a half-finished part in my post and kept working after.  The first one won't work for what I need since we're only allowed 1 loop, and the second two both give zero.  I've played with it since and got it to give me numbers, just the wrong ones

Code:

def badexp(x):
    i = 0
    term1 = 1.
    term2 = x
    temp = term1
    while term1 != term2:
        term1 = temp
        term2 = 1 + ((term1 * x) / (i + 1))
        temp = term2
        i = i + 1
    print term2
and this one are my ongoing attempts

Code:

def badexp(x):
    i = 0
    term1 = 1
    term2 = x / (i + 1)   
    prev = 1
    while prev != term2:
        term2 = prev * x / (i + 1)
        prev = term2
        i = i + 1
    print term2
HaiBai
Your thoughts, insights, and musings on this matter intrigue me
+304|5730|Bolingbrook, Illinois
alright, can you please explain that badexp formula to me?  the way it is written here:

term[i+1] = term[i] * x / (i + 1)

makes no sense to me at all

here's the second one

Code:

from urllib import urlopen

def first_word(url, line_number):
    if line_number <= 0:
        return None

    stream = urlopen(url)
    lines = stream.readlines()

    if line_number > len(lines):
        return None

    line = lines[line_number - 1]

    firstWord = ""

    for character in line:
        if not character.isalpha():
            if firstWord == "":
                continue
            break
        firstWord += character

    if firstWord == "":
        return None
    else:
        return firstWord

lol = first_word("http://www.cdf.toronto.edu/~csc120h/winter/posted/assignments/a1/data", 1)
not sure if it's EXACTLY what you're looking for.  but the following will be returned from the following strings:

12341234HaiBai1234512345 returns HaiBai
123451234512345 returns None
HaiBai12341234 returns HaiBai
HaiBai is cool returns HaiBai

it doesn't return the first word by the general definition, but it returns the first word by the definition that the prompt gave us.  a "sequence of alphabetic characters"

oh, and so much for needing "more than one loop, at the very least."

Last edited by HaiBai (2012-02-08 16:25:34)

HaiBai
Your thoughts, insights, and musings on this matter intrigue me
+304|5730|Bolingbrook, Illinois
oh, and if you want help with the first one, can you show me how the calculation would proceed in a non-programming way?

for example, let's say i pass in the value of 1.

term[i+1] = term[i] * x / (i + 1)

show me how that value changes as the program evaluates it.

or just give me a wiki link that describes the formula in better detail or something
Winston_Churchill
Bazinga!
+521|6984|Toronto | Canada

thanks for the 2nd one, ill take a look at it in a second - in the class right now haha

the first one is estimating an e^x function, but using a simplified and shitty way.  my current attempt (i think im close-ish) is:

Code:

def badexp(x):
    i = 0
    term2 = x
    sum = 1.
    while sum != term2:
        term2 = (term2 * x) / (i + 1)
        sum = sum + term2
        i = i + 1
    print sum
Since you already found the link, its equation 7 here http://www.cdf.toronto.edu/~csc120h/win … ations.pdf
Winston_Churchill
Bazinga!
+521|6984|Toronto | Canada

fuckyeah i think i got it

Code:

def badexp(x):
    i = 0
    term2 = 1
    sum = 1
    while (sum + term2) != sum:
        term2 = (term2 * float(x)) / (i + 1)
        sum = sum + term2
        i = i + 1
    print sum
HaiBai
Your thoughts, insights, and musings on this matter intrigue me
+304|5730|Bolingbrook, Illinois
we pretty much got the same thing

Code:

def badexp(x):
    i = 0
    term = 1
    result = 1

    x = float(x)
    
    while term != 0:
        term *= x / (i + 1)
        result += term
        i += 1

    return result

print badexp(1)
Winston_Churchill
Bazinga!
+521|6984|Toronto | Canada

small programming lab question:

Given that t refers to a tuple, write a statement that assigns the value of its first element to k.
I've tried this about a thousand different ways, cant figure it out though

Most go like this but dont work. Any ideas?

Code:

t = k, t[1:]
Winston_Churchill
Bazinga!
+521|6984|Toronto | Canada

i feel dumb, just figured it out. read the question wrong, carry on
Winston_Churchill
Bazinga!
+521|6984|Toronto | Canada

okay im having a big problem with getting info from columns from a data file. this is the data (mine is just in a webarchive text format - http://www.canadarunningseries.com/resu … /sl10k.htm

im trying to get the names from it, but i cant use split() since there are 2 or 3 word names so it wont know what to do, i cant use a constant spacing thing like line[35:] since thats not constant, i cant use split by ('\t\) either since theyre not spaced by tabs. im really confused on what to do here and i feel its a simple fix. help!?

Board footer

Privacy Policy - © 2024 Jeff Minard