Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 229c08b

Browse files
committed
added more questions
1 parent e379474 commit 229c08b

File tree

1 file changed

+250
-0
lines changed

1 file changed

+250
-0
lines changed

‎100+ Python challenging programming exercises.txt‎

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,5 +1105,255 @@ print tp2
11051105

11061106

11071107

1108+
#----------------------------------------#
1109+
2.14
1110+
1111+
Question:
1112+
Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No".
1113+
1114+
Hints:
1115+
1116+
Use if statement to judge condition.
1117+
1118+
Solution
1119+
s= raw_input()
1120+
if s=="yes" or s=="YES" or s=="Yes":
1121+
print "Yes"
1122+
else:
1123+
print "No"
1124+
1125+
1126+
1127+
#----------------------------------------#
1128+
3.4
1129+
1130+
Question:
1131+
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].
1132+
1133+
Hints:
1134+
1135+
Use filter() to filter some elements in a list.
1136+
Use lambda to define anonymous functions.
1137+
1138+
Solution
1139+
li = [1,2,3,4,5,6,7,8,9,10]
1140+
evenNumbers = filter(lambda x: x%2==0, li)
1141+
print evenNumbers
1142+
1143+
1144+
#----------------------------------------#
1145+
3.4
1146+
1147+
Question:
1148+
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].
1149+
1150+
Hints:
1151+
1152+
Use map() to generate a list.
1153+
Use lambda to define anonymous functions.
1154+
1155+
Solution
1156+
li = [1,2,3,4,5,6,7,8,9,10]
1157+
squaredNumbers = map(lambda x: x**2, li)
1158+
print squaredNumbers
1159+
1160+
#----------------------------------------#
1161+
3.5
1162+
1163+
Question:
1164+
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].
1165+
1166+
Hints:
1167+
1168+
Use map() to generate a list.
1169+
Use filter() to filter elements of a list.
1170+
Use lambda to define anonymous functions.
1171+
1172+
Solution
1173+
li = [1,2,3,4,5,6,7,8,9,10]
1174+
evenNumbers = map(lambda x: x**2, filter(lambda x: x%2==0, li))
1175+
print evenNumbers
1176+
1177+
1178+
1179+
1180+
#----------------------------------------#
1181+
3.5
1182+
1183+
Question:
1184+
Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included).
1185+
1186+
Hints:
1187+
1188+
Use filter() to filter elements of a list.
1189+
Use lambda to define anonymous functions.
1190+
1191+
Solution
1192+
evenNumbers = filter(lambda x: x%2==0, range(1,21))
1193+
print evenNumbers
1194+
1195+
1196+
#----------------------------------------#
1197+
3.5
1198+
1199+
Question:
1200+
Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included).
1201+
1202+
Hints:
1203+
1204+
Use map() to generate a list.
1205+
Use lambda to define anonymous functions.
1206+
1207+
Solution
1208+
squaredNumbers = map(lambda x: x**2, range(1,21))
1209+
print squaredNumbers
1210+
1211+
1212+
1213+
1214+
#----------------------------------------#
1215+
7.2
1216+
1217+
Question:
1218+
Define a class named American which has a static method called printNationality.
1219+
1220+
Hints:
1221+
1222+
Use @staticmethod decorator to define class static method.
1223+
1224+
Solution
1225+
class American(object):
1226+
@staticmethod
1227+
def printNationality():
1228+
print "America"
1229+
1230+
anAmerican = American()
1231+
anAmerican.printNationality()
1232+
American.printNationality()
1233+
1234+
1235+
1236+
1237+
#----------------------------------------#
1238+
1239+
7.2
1240+
1241+
Question:
1242+
Define a class named American and its subclass NewYorker.
1243+
1244+
Hints:
1245+
1246+
Use class Subclass(ParentClass) to define a subclass.
1247+
1248+
Solution:
1249+
1250+
class American(object):
1251+
pass
1252+
1253+
class NewYorker(American):
1254+
pass
1255+
1256+
anAmerican = American()
1257+
aNewYorker = NewYorker()
1258+
print anAmerican
1259+
print aNewYorker
1260+
1261+
1262+
1263+
1264+
#----------------------------------------#
1265+
1266+
1267+
7.2
1268+
1269+
Question:
1270+
Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area.
1271+
1272+
Hints:
1273+
1274+
Use def methodName(self) to define a method.
1275+
1276+
Solution:
1277+
1278+
class Circle(object):
1279+
def __init__(self, r):
1280+
self.radius = r
1281+
1282+
def area(self):
1283+
return self.radius**2*3.14
1284+
1285+
aCircle = Circle(2)
1286+
print aCircle.area()
1287+
1288+
1289+
1290+
1291+
1292+
1293+
#----------------------------------------#
1294+
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.
1295+
1296+
Hints:
1297+
1298+
Use def methodName(self) to define a method.
11081299

1300+
Solution:
1301+
1302+
class Rectangle(object):
1303+
def __init__(self, l, w):
1304+
self.length = l
1305+
self.width = w
1306+
1307+
def area(self):
1308+
return self.length*self.width
1309+
1310+
aRectangle = Rectangle(2,10)
1311+
print aRectangle.area()
1312+
1313+
1314+
1315+
1316+
#----------------------------------------#
1317+
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.
1318+
1319+
Hints:
1320+
1321+
To override a method in super class, we can define a method with the same name in the super class.
1322+
1323+
Solution:
1324+
1325+
class Shape(object):
1326+
def __init__(self):
1327+
pass
1328+
1329+
def area(self):
1330+
return 0
1331+
1332+
class Square(Shape):
1333+
def __init__(self, l):
1334+
Shape.__init__(self)
1335+
self.length = l
1336+
1337+
def area(self):
1338+
return self.length*self.length
1339+
1340+
aSquare= Square(3)
1341+
print aSquare.area()
1342+
1343+
1344+
1345+
1346+
1347+
1348+
1349+
1350+
#----------------------------------------#
1351+
1352+
1353+
1354+
1355+
1356+
1357+
1358+
#----------------------------------------#
11091359

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /