Skip to content

Commit

Permalink
added more questions
Browse files Browse the repository at this point in the history
  • Loading branch information
xcao21 committed Jun 16, 2012
1 parent e379474 commit 229c08b
Showing 1 changed file with 250 additions and 0 deletions.
250 changes: 250 additions & 0 deletions 100+ Python challenging programming exercises.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1105,5 +1105,255 @@ print tp2



#----------------------------------------#
2.14

Question:
Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No".

Hints:

Use if statement to judge condition.

Solution
s= raw_input()
if s=="yes" or s=="YES" or s=="Yes":
print "Yes"
else:
print "No"



#----------------------------------------#
3.4

Question:
Write a program which can filter even numbers in a list by using filter function. The list is: [1,2,3,4,5,6,7,8,9,10].

Hints:

Use filter() to filter some elements in a list.
Use lambda to define anonymous functions.

Solution
li = [1,2,3,4,5,6,7,8,9,10]
evenNumbers = filter(lambda x: x%2==0, li)
print evenNumbers


#----------------------------------------#
3.4

Question:
Write a program which can map() to make a list whose elements are square of elements in [1,2,3,4,5,6,7,8,9,10].

Hints:

Use map() to generate a list.
Use lambda to define anonymous functions.

Solution
li = [1,2,3,4,5,6,7,8,9,10]
squaredNumbers = map(lambda x: x**2, li)
print squaredNumbers

#----------------------------------------#
3.5

Question:
Write a program which can map() and filter() to make a list whose elements are square of even number in [1,2,3,4,5,6,7,8,9,10].

Hints:

Use map() to generate a list.
Use filter() to filter elements of a list.
Use lambda to define anonymous functions.

Solution
li = [1,2,3,4,5,6,7,8,9,10]
evenNumbers = map(lambda x: x**2, filter(lambda x: x%2==0, li))
print evenNumbers




#----------------------------------------#
3.5

Question:
Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included).

Hints:

Use filter() to filter elements of a list.
Use lambda to define anonymous functions.

Solution
evenNumbers = filter(lambda x: x%2==0, range(1,21))
print evenNumbers


#----------------------------------------#
3.5

Question:
Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included).

Hints:

Use map() to generate a list.
Use lambda to define anonymous functions.

Solution
squaredNumbers = map(lambda x: x**2, range(1,21))
print squaredNumbers




#----------------------------------------#
7.2

Question:
Define a class named American which has a static method called printNationality.

Hints:

Use @staticmethod decorator to define class static method.

Solution
class American(object):
@staticmethod
def printNationality():
print "America"

anAmerican = American()
anAmerican.printNationality()
American.printNationality()




#----------------------------------------#

7.2

Question:
Define a class named American and its subclass NewYorker.

Hints:

Use class Subclass(ParentClass) to define a subclass.

Solution:

class American(object):
pass

class NewYorker(American):
pass

anAmerican = American()
aNewYorker = NewYorker()
print anAmerican
print aNewYorker




#----------------------------------------#


7.2

Question:
Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area.

Hints:

Use def methodName(self) to define a method.

Solution:

class Circle(object):
def __init__(self, r):
self.radius = r

def area(self):
return self.radius**2*3.14

aCircle = Circle(2)
print aCircle.area()






#----------------------------------------#
Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area.

Hints:

Use def methodName(self) to define a method.

Solution:

class Rectangle(object):
def __init__(self, l, w):
self.length = l
self.width = w

def area(self):
return self.length*self.width

aRectangle = Rectangle(2,10)
print aRectangle.area()




#----------------------------------------#
Define a class named Shape and its subclass Square. The Square class has an init function which takes a length as argument. Both classes have a area function which can print the area of the shape where Shape's area is 0 by default.

Hints:

To override a method in super class, we can define a method with the same name in the super class.

Solution:

class Shape(object):
def __init__(self):
pass

def area(self):
return 0

class Square(Shape):
def __init__(self, l):
Shape.__init__(self)
self.length = l

def area(self):
return self.length*self.length

aSquare= Square(3)
print aSquare.area()








#----------------------------------------#







#----------------------------------------#

0 comments on commit 229c08b

Please sign in to comment.