hi,

could someone please help me with the attached python (repetition:Iteration) questions....

any help will be greatly appreciated!!

*

Printable View

- Dec 7th 2007, 02:11 PM*skywalker*python questions...please help!!
hi,

could someone please help me with the attached python (repetition:Iteration) questions....

any help will be greatly appreciated!!

*

- Dec 8th 2007, 05:18 AMAradesh
**1.)**

consider the function

Code:`def infinite_recursion():`

return infinite_recursion()

Code:`Traceback (most recent call last):`

File "<stdin>", line 1, in <module>

File "<stdin>", line 2, in infinite_recursion

...

if we just have an infinitely repeating loop. ie:

Code:`i=0`

while 1: i+=1

**2.)**

recursive function to generate

Code:`def n_sum(n):`

if n <= 1: return 1 #base case

return n+n_sum(n-1) #inductive step

__for fibonacci sequence:__

Code:`def nth_fib(n):`

if n <= 1: return 1 #covers our base cases

return nth_fib(n-1) + nth_fib(n-2) #inductive step

for fib(n) we call our function how many times it calls for both and

so

this is the*fibonacci relation*, and we have the same initial cases:

so

ie the function is called 8 times with fib(5).

**3.)**

Code:`i = 0`

while i < 100:

i+=1

if i%3 == 2:

print i, "mod", 3, "= 2"

Code:`for i in range(0,100):`

if i%2 == 0:

print i, "is even"

else:

print i, "is odd"

**4.**

it will execute zero times.

this is because it executes once for each value i takes in the list []. this is an empty list so i takes no values. - Dec 8th 2007, 01:02 PM*skywalker*
- Dec 9th 2007, 08:06 AMAradesh
really? the fibonacci one should work fine (it does for me)... just paste it in, and to call it put , for example, it should return 8 in that case.

the infinite recursion one isn't really meant to work... its just a function that should infinitely call itself, and this seems to cause errors (and the question asks what error do you get, so i'm guessing it should.)