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
+294-9
Original file line number
Diff line number
Diff line change
@@ -1496,6 +1496,9 @@ print re.findall("\d+",s)
1496
1496
1497
1497
1498
1498
#----------------------------------------#
1499
+
Question:
1500
+
1501
+
1499
1502
Print a unicode string "hello world".
1500
1503
1501
1504
Hints:
@@ -1532,8 +1535,7 @@ Solution:
1532
1535
# -*- coding: utf-8 -*-
1533
1536
1534
1537
#----------------------------------------#
1535
-
1536
-
8
1538
+
Question:
1537
1539
1538
1540
Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).
1539
1541
@@ -1561,8 +1563,7 @@ print sum
1561
1563
1562
1564
1563
1565
#----------------------------------------#
1564
-
1565
-
8
1566
+
Question:
1566
1567
1567
1568
Write a program to compute:
1568
1569
@@ -1598,7 +1599,8 @@ print f(n)
1598
1599
1599
1600
#----------------------------------------#
1600
1601
1601
-
8
1602
+
Question:
1603
+
1602
1604
1603
1605
The Fibonacci Sequence is computed based on the following formula:
1604
1606
@@ -1639,7 +1641,7 @@ print f(n)
1639
1641
1640
1642
#----------------------------------------#
1641
1643
1642
-
8
1644
+
Question:
1643
1645
1644
1646
The Fibonacci Sequence is computed based on the following formula:
1645
1647
@@ -1681,7 +1683,7 @@ print ",".join(values)
1681
1683
1682
1684
#----------------------------------------#
1683
1685
1684
-
8
1686
+
Question:
1685
1687
1686
1688
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
1689
@@ -1719,8 +1721,7 @@ print ",".join(values)
1719
1721
1720
1722
#----------------------------------------#
1721
1723
1722
-
1723
-
8
1724
+
Question:
1724
1725
1725
1726
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
@@ -1755,13 +1756,297 @@ print ",".join(values)
1755
1756
1756
1757
#----------------------------------------#
1757
1758
1759
+
Question:
1760
+
1761
+
1762
+
Please write assert statements to verify that every number in the list [2,4,6,8] is even.
1763
+
1764
+
1765
+
1766
+
Hints:
1767
+
Use "assert expression" to make assertion.
1768
+
1769
+
1770
+
Solution:
1771
+
1772
+
li = [2,4,6,8]
1773
+
for i in li:
1774
+
assert i%2==0
1775
+
1776
+
1777
+
#----------------------------------------#
1778
+
Question:
1779
+
1780
+
Please write a program which accepts basic mathematic expression from console and print the evaluation result.
1781
+
1782
+
Example:
1783
+
If the following string is given as input to the program:
1784
+
1785
+
35+3
1786
+
1787
+
Then, the output of the program should be:
1788
+
1789
+
38
1790
+
1791
+
Hints:
1792
+
Use eval() to evaluate an expression.
1793
+
1794
+
1795
+
Solution:
1796
+
1797
+
expression = raw_input()
1798
+
print eval(expression)
1799
+
1800
+
1801
+
#----------------------------------------#
1802
+
Question:
1803
+
1804
+
Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list.
1805
+
1806
+
1807
+
Hints:
1808
+
Use if/elif to deal with conditions.
1809
+
1810
+
1811
+
Solution:
1812
+
1813
+
import math
1814
+
def bin_search(li, element):
1815
+
bottom = 0
1816
+
top = len(li)-1
1817
+
index = -1
1818
+
while top>=bottom and index==-1:
1819
+
mid = int(math.floor((top+bottom)/2.0))
1820
+
if li[mid]==element:
1821
+
index = mid
1822
+
elif li[mid]>element:
1823
+
top = mid-1
1824
+
else:
1825
+
bottom = mid+1
1826
+
1827
+
return index
1828
+
1829
+
li=[2,5,7,9,11,17,222]
1830
+
print bin_search(li,11)
1831
+
print bin_search(li,12)
1832
+
1833
+
1834
+
1835
+
1836
+
#----------------------------------------#
1837
+
Question:
1838
+
1839
+
Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list.
1840
+
1841
+
1842
+
Hints:
1843
+
Use if/elif to deal with conditions.
1844
+
1845
+
1846
+
Solution:
1847
+
1848
+
import math
1849
+
def bin_search(li, element):
1850
+
bottom = 0
1851
+
top = len(li)-1
1852
+
index = -1
1853
+
while top>=bottom and index==-1:
1854
+
mid = int(math.floor((top+bottom)/2.0))
1855
+
if li[mid]==element:
1856
+
index = mid
1857
+
elif li[mid]>element:
1858
+
top = mid-1
1859
+
else:
1860
+
bottom = mid+1
1861
+
1862
+
return index
1863
+
1864
+
li=[2,5,7,9,11,17,222]
1865
+
print bin_search(li,11)
1866
+
print bin_search(li,12)
1867
+
1868
+
1869
+
1870
+
1871
+
#----------------------------------------#
1872
+
Question:
1873
+
1874
+
Please generate a random float where the value is between 10 and 100 using Python math module.
1875
+
1876
+
1877
+
1878
+
Hints:
1879
+
Use random.random() to generate a random float in [0,1].
1880
+
1881
+
1882
+
Solution:
1883
+
1884
+
import random
1885
+
print random.random()*100
1886
+
1887
+
#----------------------------------------#
1888
+
Question:
1889
+
1890
+
Please generate a random float where the value is between 5 and 95 using Python math module.
1891
+
1892
+
1893
+
1894
+
Hints:
1895
+
Use random.random() to generate a random float in [0,1].
1896
+
1897
+
1898
+
Solution:
1899
+
1900
+
import random
1901
+
print random.random()*100-5
1902
+
1903
+
1904
+
#----------------------------------------#
1905
+
Question:
1906
+
1907
+
Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension.
1908
+
1909
+
1910
+
1911
+
Hints:
1912
+
Use random.choice() to a random element from a list.
1913
+
1914
+
1915
+
Solution:
1916
+
1917
+
import random
1918
+
print random.choice([i for i in range(11) if i%2==0])
1919
+
1920
+
1921
+
#----------------------------------------#
1922
+
Question:
1923
+
1924
+
Please write a program to output a random number, which is divisible by 5 and 7, between 0 and 10 inclusive using random module and list comprehension.
1925
+
1926
+
1927
+
1928
+
Hints:
1929
+
Use random.choice() to a random element from a list.
1930
+
1931
+
1932
+
Solution:
1933
+
1934
+
import random
1935
+
print random.choice([i for i in range(201) if i%5==0 and i%7==0])
1936
+
1937
+
1938
+
1939
+
#----------------------------------------#
1940
+
1941
+
Question:
1942
+
1943
+
Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive.
1944
+
1945
+
1946
+
1947
+
Hints:
1948
+
Use random.sample() to generate a list of random values.
1949
+
1950
+
1951
+
Solution:
1952
+
1953
+
import random
1954
+
print random.sample(range(100), 5)
1955
+
1956
+
#----------------------------------------#
1957
+
Question:
1958
+
1959
+
Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive.
1960
+
1961
+
1962
+
1963
+
Hints:
1964
+
Use random.sample() to generate a list of random values.
1965
+
1966
+
1967
+
Solution:
1968
+
1969
+
import random
1970
+
print random.sample([i for i in range(100,201) if i%2==0], 5)
1971
+
1972
+
1973
+
#----------------------------------------#
1974
+
Question:
1975
+
1976
+
Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive.
1977
+
1978
+
1979
+
1980
+
Hints:
1981
+
Use random.sample() to generate a list of random values.
1758
1982
1759
1983
1984
+
Solution:
1760
1985
1986
+
import random
1987
+
print random.sample([i for i in range(1,1001) if i%5==0 and i%7==0], 5)
1761
1988
1762
1989
#----------------------------------------#
1763
1990
1991
+
Question:
1764
1992
1993
+
Please write a program to randomly print a integer number between 7 and 15 inclusive.
1765
1994
1766
1995
1767
1996
1997
+
Hints:
1998
+
Use random.randrange() to a random integer in a given range.
1999
+
2000
+
2001
+
Solution:
2002
+
2003
+
import random
2004
+
print random.randrange(7,16)
2005
+
2006
+
#----------------------------------------#
2007
+
2008
+
Question:
2009
+
2010
+
Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!".
2011
+
2012
+
2013
+
2014
+
Hints:
2015
+
Use zlib.compress() and zlib.decompress() to compress and decompress a string.
2016
+
2017
+
2018
+
Solution:
2019
+
2020
+
import zlib
2021
+
s = 'hello world!hello world!hello world!hello world!'
2022
+
t = zlib.compress(s)
2023
+
print t
2024
+
print zlib.decompress(t)
2025
+
2026
+
#----------------------------------------#
2027
+
Question:
2028
+
2029
+
Please write a program to print the running time of execution of "1+1" for 100 times.
2030
+
2031
+
2032
+
2033
+
Hints:
2034
+
Use timeit() function to measure the running time.
0 commit comments