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 3c748f8

Browse files
committed
added 10+ questions
1 parent 7280506 commit 3c748f8

File tree

1 file changed

+294
-9
lines changed

1 file changed

+294
-9
lines changed

‎100+ Python challenging programming exercises.txt

Lines changed: 294 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,9 @@ print re.findall("\d+",s)
14961496

14971497

14981498
#----------------------------------------#
1499+
Question:
1500+
1501+
14991502
Print a unicode string "hello world".
15001503

15011504
Hints:
@@ -1532,8 +1535,7 @@ Solution:
15321535
# -*- coding: utf-8 -*-
15331536

15341537
#----------------------------------------#
1535-
1536-
8
1538+
Question:
15371539

15381540
Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).
15391541

@@ -1561,8 +1563,7 @@ print sum
15611563

15621564

15631565
#----------------------------------------#
1564-
1565-
8
1566+
Question:
15661567

15671568
Write a program to compute:
15681569

@@ -1598,7 +1599,8 @@ print f(n)
15981599

15991600
#----------------------------------------#
16001601

1601-
8
1602+
Question:
1603+
16021604

16031605
The Fibonacci Sequence is computed based on the following formula:
16041606

@@ -1639,7 +1641,7 @@ print f(n)
16391641

16401642
#----------------------------------------#
16411643

1642-
8
1644+
Question:
16431645

16441646
The Fibonacci Sequence is computed based on the following formula:
16451647

@@ -1681,7 +1683,7 @@ print ",".join(values)
16811683

16821684
#----------------------------------------#
16831685

1684-
8
1686+
Question:
16851687

16861688
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.
16871689

@@ -1719,8 +1721,7 @@ print ",".join(values)
17191721

17201722
#----------------------------------------#
17211723

1722-
1723-
8
1724+
Question:
17241725

17251726
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.
17261727

@@ -1755,13 +1756,297 @@ print ",".join(values)
17551756

17561757
#----------------------------------------#
17571758

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.
17581982

17591983

1984+
Solution:
17601985

1986+
import random
1987+
print random.sample([i for i in range(1,1001) if i%5==0 and i%7==0], 5)
17611988

17621989
#----------------------------------------#
17631990

1991+
Question:
17641992

1993+
Please write a program to randomly print a integer number between 7 and 15 inclusive.
17651994

17661995

17671996

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.
2035+
2036+
Solution:
2037+
2038+
from timeit import Timer
2039+
t = Timer("for i in range(100):1+1")
2040+
print t.timeit()
2041+
2042+
#----------------------------------------#
2043+
2044+
2045+
#----------------------------------------#
2046+
2047+
2048+
2049+
#----------------------------------------#
2050+
2051+
2052+
#----------------------------------------#

0 commit comments

Comments
(0)

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