Python Recursion Function: Squares and Class Example, Quizzes of Electrical and Electronics Engineering

Two python examples: the first one is a recursive function named squares that takes a list of integers and returns a new list with the same integers squared. The second example is a class named foo that initializes an object with an argument and prints the result of the argument squared.

Typology: Quizzes

Pre 2010

Uploaded on 09/17/2009

koofers-user-zbe-1
koofers-user-zbe-1 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Write a Python function called Squares that takes a list of integers and returns a list of the
same integers, but squared. Use recu rs io n, not a for loop or a while loop, to accomplish this.
(Note: This is actually an example of tail recursion.) Thus, Squares( [8,2,3] ) should
return the list [64,4,9].
2. What will the following print statment print?
class foo():
def init ( self, arg ):
self.x = ( arg, arg * arg )
z = foo( 5 )
print z.x
1

Partial preview of the text

Download Python Recursion Function: Squares and Class Example and more Quizzes Electrical and Electronics Engineering in PDF only on Docsity!

  1. Write a Python function called Squares that takes a list of integers and returns a list of the same integers, but squared. Use recursion, not a for loop or a while loop, to accomplish this. (Note: This is actually an example of tail recursion.) Thus, Squares( [8,2,3] ) should return the list [64,4,9].
  2. What will the following print statment print?

class foo(): def init ( self, arg ): self.x = ( arg, arg * arg )

z = foo( 5 ) print z.x