Skip to content

Commit febcb48

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

File tree

1 file changed

+269
-0
lines changed

1 file changed

+269
-0
lines changed

100+ Python challenging programming exercises.txt

+269
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,9 @@ print aCircle.area()
12911291

12921292

12931293
#----------------------------------------#
1294+
1295+
7.2
1296+
12941297
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.
12951298

12961299
Hints:
@@ -1314,6 +1317,9 @@ print aRectangle.area()
13141317

13151318

13161319
#----------------------------------------#
1320+
1321+
7.2
1322+
13171323
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.
13181324

13191325
Hints:
@@ -1350,10 +1356,273 @@ print aSquare.area()
13501356
#----------------------------------------#
13511357

13521358

1359+
Please raise a RuntimeError exception.
13531360

1361+
Hints:
13541362

1363+
Use raise() to raise an exception.
1364+
1365+
Solution:
1366+
1367+
raise RuntimeError('something wrong')
13551368

13561369

13571370

13581371
#----------------------------------------#
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.
1581+
1582+
Solution:
1583+
1584+
def f(n):
1585+
if n==0:
1586+
return 0
1587+
else:
1588+
return f(n-1)+100
1589+
1590+
n=int(raw_input())
1591+
print f(n)
1592+
1593+
#----------------------------------------#
1594+
1595+
1596+
1597+
1598+
1599+
#----------------------------------------#
1600+
1601+
1602+
1603+
1604+
1605+
#----------------------------------------#
1606+
1607+
1608+
1609+
1610+
1611+
#----------------------------------------#
1612+
1613+
1614+
1615+
1616+
1617+
#----------------------------------------#
1618+
1619+
1620+
1621+
1622+
1623+
#----------------------------------------#
1624+
1625+
1626+
1627+
13591628

0 commit comments

Comments
 (0)