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 7280506

Browse files
committed
added more questions
1 parent febcb48 commit 7280506

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

‎100+ Python challenging programming exercises.txt

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,9 @@ Then, the output of the program should be:
15481548

15491549
In case of input data being supplied to the question, it should be assumed to be a console input.
15501550

1551+
Hints:
1552+
Use float() to convert an integer to a float
1553+
15511554
Solution:
15521555

15531556
n=int(raw_input())
@@ -1579,6 +1582,9 @@ Then, the output of the program should be:
15791582

15801583
In case of input data being supplied to the question, it should be assumed to be a console input.
15811584

1585+
Hints:
1586+
We can define recursive function in Python.
1587+
15821588
Solution:
15831589

15841590
def f(n):
@@ -1592,26 +1598,159 @@ print f(n)
15921598

15931599
#----------------------------------------#
15941600

1601+
8
1602+
1603+
The Fibonacci Sequence is computed based on the following formula:
15951604

15961605

1606+
f(n)=0 if n=0
1607+
f(n)=1 if n=1
1608+
f(n)=f(n-1)+f(n-2) if n>1
1609+
1610+
Please write a program to compute the value of f(n) with a given n input by console.
1611+
1612+
Example:
1613+
If the following n is given as input to the program:
1614+
1615+
7
1616+
1617+
Then, the output of the program should be:
1618+
1619+
13
1620+
1621+
In case of input data being supplied to the question, it should be assumed to be a console input.
1622+
1623+
Hints:
1624+
We can define recursive function in Python.
1625+
1626+
1627+
Solution:
1628+
1629+
def f(n):
1630+
if n == 0: return 0
1631+
elif n == 1: return 1
1632+
else: return f(n-1)+f(n-2)
1633+
1634+
n=int(raw_input())
1635+
print f(n)
15971636

15981637

15991638
#----------------------------------------#
16001639

1640+
#----------------------------------------#
1641+
1642+
8
1643+
1644+
The Fibonacci Sequence is computed based on the following formula:
1645+
1646+
1647+
f(n)=0 if n=0
1648+
f(n)=1 if n=1
1649+
f(n)=f(n-1)+f(n-2) if n>1
1650+
1651+
Please write a program using list comprehension to print the Fibonacci Sequence in comma separated form with a given n input by console.
1652+
1653+
Example:
1654+
If the following n is given as input to the program:
1655+
1656+
7
1657+
1658+
Then, the output of the program should be:
1659+
1660+
0,1,1,2,3,5,8,13
1661+
1662+
1663+
Hints:
1664+
We can define recursive function in Python.
1665+
Use list comprehension to generate a list from an existing list.
1666+
Use string.join() to join a list of strings.
1667+
1668+
In case of input data being supplied to the question, it should be assumed to be a console input.
1669+
1670+
Solution:
16011671

1672+
def f(n):
1673+
if n == 0: return 0
1674+
elif n == 1: return 1
1675+
else: return f(n-1)+f(n-2)
16021676

1677+
n=int(raw_input())
1678+
values = [str(f(x)) for x in range(0, n+1)]
1679+
print ",".join(values)
16031680

16041681

16051682
#----------------------------------------#
16061683

1684+
8
1685+
1686+
Please write a program using generator to print the even numbers between 0 and n in comma separated form while n is input by console.
1687+
1688+
Example:
1689+
If the following n is given as input to the program:
16071690

1691+
10
16081692

1693+
Then, the output of the program should be:
1694+
1695+
0,2,4,6,8,10
1696+
1697+
Hints:
1698+
Use yield to produce the next value in generator.
1699+
1700+
In case of input data being supplied to the question, it should be assumed to be a console input.
1701+
1702+
Solution:
1703+
1704+
def EvenGenerator(n):
1705+
i=0
1706+
while i<=n:
1707+
if i%2==0:
1708+
yield i
1709+
i+=1
1710+
1711+
1712+
n=int(raw_input())
1713+
values = []
1714+
for i in EvenGenerator(n):
1715+
values.append(str(i))
1716+
1717+
print ",".join(values)
16091718

16101719

16111720
#----------------------------------------#
16121721

16131722

1723+
8
1724+
1725+
Please write a program using generator to print the numbers which can be divisible by 5 and 7 between 0 and n in comma separated form while n is input by console.
1726+
1727+
Example:
1728+
If the following n is given as input to the program:
1729+
1730+
100
1731+
1732+
Then, the output of the program should be:
1733+
1734+
0,35,70
1735+
1736+
Hints:
1737+
Use yield to produce the next value in generator.
1738+
1739+
In case of input data being supplied to the question, it should be assumed to be a console input.
1740+
1741+
Solution:
16141742

1743+
def NumGenerator(n):
1744+
for i in range(n+1):
1745+
if i%5==0 and i%7==0:
1746+
yield i
1747+
1748+
n=int(raw_input())
1749+
values = []
1750+
for i in NumGenerator(n):
1751+
values.append(str(i))
1752+
1753+
print ",".join(values)
16151754

16161755

16171756
#----------------------------------------#

0 commit comments

Comments
(0)

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