Answered # iterative python

adam1122 7 shadwickman 159 Discussion Starter srk619 Discussion Starter srk619 adam1122 7 Gribouillis 1,313 Discussion Starter srk619 adam1122 7 Discussion Starter srk619 Discussion Starter srk619 woooee 813 vidaj 35 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

0

By "iterative" they are asking for a looping mechanism as opposed to the usual recursion that is used for this problem.

Start with a loop.

0

Everyone else is right. Please show some effort of your own; attempt the problem and then post the code you have so far. We can try to point out hints from there.

P.S. Remember to use CODE tags. We need to see your indentation.

0

thanks for your comment but you know what i did know what to do and thanks to people like adam 1122 who can start me off

0

so i have come up with some coding is this right to get the fibonacci numbers:

def fib(n):

if n == 0:

return 0

elif n == 1:

return 1

else:

return fib(n-1) + fib(n-2)

0

You are using recursion, but your problem asks for an "iterative Python function" which is probably asking for a loop. So you would need a `for`

or `while`

statement.

0

Your function will compute the fibonacci sequence but it's not iterative, it's recursive (the function fib calls itself). If you want to write an iterative function, you must work with a pair of numbers x(n), y(n) = fib(n), fib(n-1). Now you should be able to write a formula which gives the pair x(n), y(n) if you know x(n-1), y(n-1). Also note that you know x(1), y(1).

0

u mean like this:

def fib(n):

a, b = 0, 1

for i in range(n):

a, b = b, a + b

return a

0

Just a note that I edited my post above. Your code is good (as a recursive function). I was relying on your initial definition of Fibonacci sequence, which is incorrect, instead of thinking for myself. I removed part of my comment so I don't confuse someone else looking at this.

0

ok so is this code a lexer:

def fib():

'''

a generator that produces the elements of the fibonacci series

'''

a = 1

b = 1 while True:

a, b = a + b, a

yield a

def nth(series, n):

'''

returns the nth element of a series, consuming the earlier elements of the series

'''

for x in series:

n = n - 1

if n <= 0: return x

print nth(fib(), 10)

0

does anyone know how to do the question:

Implement an iterative Python function to generate numbers in the Fibonacci sequence:

fib(0) = 1

fib(1) = 1

fib(n) = fib(n − 1) + fib(n − 2)

i know how to do recursive as you can see i the other posts but i dont know how to do iterative

0

It seems that all you have done is to copy code you found somewhere else. For example, what does this line that you posted mean and why would it be used in a fib sequence?

a, b = b, a + b

Start with a routine to get the number(s) to begin with and the number of fibs that you want, or the maximum value you don't want to exceed before stopping. Post the code here. Then write some code to calculate one fib number in a sequence, given the previous sequence. Because a fib can start with any number(s), you first have to set the parameters and then calculate the fibs.

0

As you probably already know, and have been told earlier in this thread, iterative means you have to solve the problem in one function without calling the same function from within the same function (recursion)

What I would use for this is lists.

Define a list with the initial solutions, i.e for n = 0 and n = 1. Then do a loop and calculate every solution from 2 to and including n.

To start you off:

```
def fib(n):
solutions = [1, 1] # Create the list containing the initial solutions
# Test if you already know the solution for n, if so - return the solution
for x in range(2, n + 1): # iterate over every value from fib(2) to fib(n) and generate the solutions
# here you calculate the solution for fib(x) by appending (x - 1) + (x - 2) from solutions to the list
```

Post your function back and let's see what you got :)

-Vidaj-

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```