Python Forum
Help with homework problem - iterating a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with homework problem - iterating a function
#1
def iterate(iterationnumber):
indented - f(x) = 2*x
indented - count = srange(iterationnumber)
indented - vals = [3]
indented - for i in count:
second indented - a = vals[i]
second indented - b = f(a)
vals.append(b)
return vals

So the problem is asking me to modify my code above to make the initial value in the iteration to be an input. Here it is 3 as seen above - could someone point me in the right direction?
Reply
#2
You're going to need to post your code correctly in order for people to help you.
https://python-forum.io/misc.php?action=help&hid=25
Reply
#3
Hi - I'm being asked to iterate a function, though a modification to this code needed is one to allow the function to be an input, which can take any value. Here the function is set to be 2*x. Could someone point me in the right direction?

def iterate(numberofiterations,initial):
    f(x) = 2*x
    count = srange(numberofiterations)
    vals = [initial]
    for i in count:
        a = vals[i]
        b = f(a)
        vals.append(b)
    return vals
iterate(3,2); iterate (3,3); iterate(3,4)
Reply
#4
could you post the exact assignment? The code in your post is yours and not from the assignment, right?
Reply
#5
It's been a while, so just answering this is probably fine.

First, let's rewrite your example, so it's actually real code:
def iterate(numberofiterations,initial):
    f = lambda x: 2*x
    count = srange(numberofiterations)
    vals = [initial]
    for i in count:
        a = vals[i]
        b = f(a)
        vals.append(b)
    return vals
Ok, now let's rewrite it again, so the function can be passed in:
def iterate(mutator, numberofiterations, initial):
    count = srange(numberofiterations)
    vals = [initial]
    for i in count:
        a = vals[i]
        b = mutator(a)
        vals.append(b)
    return vals
Also, for the love of Guido, please use variable names with more than one character. a and b is incredibly ridiculous, and you should feel bad for using those.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with a formula 1 kind of homework claroque 2 1,346 Jun-20-2023, 09:58 PM
Last Post: claroque
  Bakery Problem.. Can you Help My Homework Cemvrs 1 2,318 Jan-14-2022, 05:47 PM
Last Post: Yoriz
  Iterating over dictionaries with the namedtuple function (PyBite #108) Drone4four 7 4,639 Jul-15-2020, 04:23 AM
Last Post: ndc85430
  Homework advice - Boolean function, whole-word values musicjoeyoung 4 3,187 May-07-2020, 06:10 PM
Last Post: musicjoeyoung
  Need help with a homework problem on logical operators voodoo 3 4,537 Jun-28-2019, 03:45 PM
Last Post: jefsummers
  Homework Problem csascott 1 2,231 Oct-27-2018, 04:44 AM
Last Post: scidam
  Homework Problem Travisbulls34 1 2,910 Sep-11-2018, 04:04 PM
Last Post: ichabod801
  Problem with a homework Samuelcu 2 2,794 Feb-03-2018, 01:39 PM
Last Post: Samuelcu
  Homework problem Dem_Prorammer 1 3,760 May-20-2017, 07:49 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020