You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: 100+ Python challenging programming exercises.txt
+269
Original file line number
Diff line number
Diff line change
@@ -1291,6 +1291,9 @@ print aCircle.area()
1291
1291
1292
1292
1293
1293
#----------------------------------------#
1294
+
1295
+
7.2
1296
+
1294
1297
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
1298
1296
1299
Hints:
@@ -1314,6 +1317,9 @@ print aRectangle.area()
1314
1317
1315
1318
1316
1319
#----------------------------------------#
1320
+
1321
+
7.2
1322
+
1317
1323
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
1324
1319
1325
Hints:
@@ -1350,10 +1356,273 @@ print aSquare.area()
1350
1356
#----------------------------------------#
1351
1357
1352
1358
1359
+
Please raise a RuntimeError exception.
1353
1360
1361
+
Hints:
1354
1362
1363
+
Use raise() to raise an exception.
1364
+
1365
+
Solution:
1366
+
1367
+
raise RuntimeError('something wrong')
1355
1368
1356
1369
1357
1370
1358
1371
#----------------------------------------#
1372
+
Write a function to compute 5/0 and use try/except to catch the exceptions.
1373
+
1374
+
Hints:
1375
+
1376
+
Use try/except to catch exceptions.
1377
+
1378
+
Solution:
1379
+
1380
+
def throws():
1381
+
return 5/0
1382
+
1383
+
try:
1384
+
throws()
1385
+
except ZeroDivisionError:
1386
+
print "division by zero!"
1387
+
except Exception, err:
1388
+
print 'Caught an exception'
1389
+
finally:
1390
+
print 'In finally block for cleanup'
1391
+
1392
+
1393
+
#----------------------------------------#
1394
+
Define a custom exception class which takes a string message as attribute.
1395
+
1396
+
Hints:
1397
+
1398
+
To define a custom exception, we need to define a class inherited from Exception.
1399
+
1400
+
Solution:
1401
+
1402
+
class MyError(Exception):
1403
+
"""My own exception class
1404
+
1405
+
Attributes:
1406
+
msg -- explanation of the error
1407
+
"""
1408
+
1409
+
def __init__(self, msg):
1410
+
self.msg = msg
1411
+
1412
+
error = MyError("something wrong")
1413
+
1414
+
#----------------------------------------#
1415
+
Question:
1416
+
1417
+
Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only.
1418
+
1419
+
Example:
1420
+
If the following email address is given as input to the program:
1421
+
1422
+
john@google.com
1423
+
1424
+
Then, the output of the program should be:
1425
+
1426
+
john
1427
+
1428
+
In case of input data being supplied to the question, it should be assumed to be a console input.
1429
+
1430
+
Hints:
1431
+
1432
+
Use \w to match letters.
1433
+
1434
+
Solution:
1435
+
import re
1436
+
emailAddress = raw_input()
1437
+
pat2 = "(\w+)@((\w+\.)+(com))"
1438
+
r2 = re.match(pat2,emailAddress)
1439
+
print r2.group(1)
1440
+
1441
+
1442
+
#----------------------------------------#
1443
+
Question:
1444
+
1445
+
Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the company name of a given email address. Both user names and company names are composed of letters only.
1446
+
1447
+
Example:
1448
+
If the following email address is given as input to the program:
1449
+
1450
+
john@google.com
1451
+
1452
+
Then, the output of the program should be:
1453
+
1454
+
google
1455
+
1456
+
In case of input data being supplied to the question, it should be assumed to be a console input.
1457
+
1458
+
Hints:
1459
+
1460
+
Use \w to match letters.
1461
+
1462
+
Solution:
1463
+
import re
1464
+
emailAddress = raw_input()
1465
+
pat2 = "(\w+)@(\w+)\.(com)"
1466
+
r2 = re.match(pat2,emailAddress)
1467
+
print r2.group(2)
1468
+
1469
+
1470
+
1471
+
1472
+
#----------------------------------------#
1473
+
Question:
1474
+
1475
+
Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only.
1476
+
1477
+
Example:
1478
+
If the following words is given as input to the program:
1479
+
1480
+
2 cats and 3 dogs.
1481
+
1482
+
Then, the output of the program should be:
1483
+
1484
+
['2', '3']
1485
+
1486
+
In case of input data being supplied to the question, it should be assumed to be a console input.
1487
+
1488
+
Hints:
1489
+
1490
+
Use re.findall() to find all substring using regex.
1491
+
1492
+
Solution:
1493
+
import re
1494
+
s = raw_input()
1495
+
print re.findall("\d+",s)
1496
+
1497
+
1498
+
#----------------------------------------#
1499
+
Print a unicode string "hello world".
1500
+
1501
+
Hints:
1502
+
1503
+
Use u'strings' format to define unicode string.
1504
+
1505
+
Solution:
1506
+
1507
+
unicodeString = u"hello world!"
1508
+
print unicodeString
1509
+
1510
+
#----------------------------------------#
1511
+
Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8.
1512
+
1513
+
Hints:
1514
+
1515
+
Use unicode() function to convert.
1516
+
1517
+
Solution:
1518
+
1519
+
s = raw_input()
1520
+
u = unicode( s ,"utf-8")
1521
+
print u
1522
+
1523
+
#----------------------------------------#
1524
+
Question:
1525
+
1526
+
Write a special comment to indicate a Python source code file is in unicode.
1527
+
1528
+
Hints:
1529
+
1530
+
Solution:
1531
+
1532
+
# -*- coding: utf-8 -*-
1533
+
1534
+
#----------------------------------------#
1535
+
1536
+
8
1537
+
1538
+
Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).
1539
+
1540
+
Example:
1541
+
If the following n is given as input to the program:
1542
+
1543
+
5
1544
+
1545
+
Then, the output of the program should be:
1546
+
1547
+
3.55
1548
+
1549
+
In case of input data being supplied to the question, it should be assumed to be a console input.
1550
+
1551
+
Solution:
1552
+
1553
+
n=int(raw_input())
1554
+
sum=0.0
1555
+
for i in range(1,n+1):
1556
+
sum += float(float(i)/(i+1))
1557
+
print sum
1558
+
1559
+
1560
+
#----------------------------------------#
1561
+
1562
+
8
1563
+
1564
+
Write a program to compute:
1565
+
1566
+
f(n)=f(n-1)+100 when n>0
1567
+
and f(0)=1
1568
+
1569
+
with a given n input by console (n>0).
1570
+
1571
+
Example:
1572
+
If the following n is given as input to the program:
1573
+
1574
+
5
1575
+
1576
+
Then, the output of the program should be:
1577
+
1578
+
500
1579
+
1580
+
In case of input data being supplied to the question, it should be assumed to be a console input.
0 commit comments