Skip to content

Commit

Permalink
added 10+ questions
Browse files Browse the repository at this point in the history
  • Loading branch information
xcao21 committed Jun 17, 2012
1 parent 7280506 commit 3c748f8
Showing 1 changed file with 294 additions and 9 deletions.
303 changes: 294 additions & 9 deletions 100+ Python challenging programming exercises.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,9 @@ print re.findall("\d+",s)


#----------------------------------------#
Question:


Print a unicode string "hello world".

Hints:
Expand Down Expand Up @@ -1532,8 +1535,7 @@ Solution:
# -*- coding: utf-8 -*-

#----------------------------------------#

8
Question:

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

Expand Down Expand Up @@ -1561,8 +1563,7 @@ print sum


#----------------------------------------#

8
Question:

Write a program to compute:

Expand Down Expand Up @@ -1598,7 +1599,8 @@ print f(n)

#----------------------------------------#

8
Question:


The Fibonacci Sequence is computed based on the following formula:

Expand Down Expand Up @@ -1639,7 +1641,7 @@ print f(n)

#----------------------------------------#

8
Question:

The Fibonacci Sequence is computed based on the following formula:

Expand Down Expand Up @@ -1681,7 +1683,7 @@ print ",".join(values)

#----------------------------------------#

8
Question:

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.

Expand Down Expand Up @@ -1719,8 +1721,7 @@ print ",".join(values)

#----------------------------------------#


8
Question:

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.

Expand Down Expand Up @@ -1755,13 +1756,297 @@ print ",".join(values)

#----------------------------------------#

Question:


Please write assert statements to verify that every number in the list [2,4,6,8] is even.



Hints:
Use "assert expression" to make assertion.


Solution:

li = [2,4,6,8]
for i in li:
assert i%2==0


#----------------------------------------#
Question:

Please write a program which accepts basic mathematic expression from console and print the evaluation result.

Example:
If the following string is given as input to the program:

35+3

Then, the output of the program should be:

38

Hints:
Use eval() to evaluate an expression.


Solution:

expression = raw_input()
print eval(expression)


#----------------------------------------#
Question:

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.


Hints:
Use if/elif to deal with conditions.


Solution:

import math
def bin_search(li, element):
bottom = 0
top = len(li)-1
index = -1
while top>=bottom and index==-1:
mid = int(math.floor((top+bottom)/2.0))
if li[mid]==element:
index = mid
elif li[mid]>element:
top = mid-1
else:
bottom = mid+1

return index

li=[2,5,7,9,11,17,222]
print bin_search(li,11)
print bin_search(li,12)




#----------------------------------------#
Question:

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.


Hints:
Use if/elif to deal with conditions.


Solution:

import math
def bin_search(li, element):
bottom = 0
top = len(li)-1
index = -1
while top>=bottom and index==-1:
mid = int(math.floor((top+bottom)/2.0))
if li[mid]==element:
index = mid
elif li[mid]>element:
top = mid-1
else:
bottom = mid+1

return index

li=[2,5,7,9,11,17,222]
print bin_search(li,11)
print bin_search(li,12)




#----------------------------------------#
Question:

Please generate a random float where the value is between 10 and 100 using Python math module.



Hints:
Use random.random() to generate a random float in [0,1].


Solution:

import random
print random.random()*100

#----------------------------------------#
Question:

Please generate a random float where the value is between 5 and 95 using Python math module.



Hints:
Use random.random() to generate a random float in [0,1].


Solution:

import random
print random.random()*100-5


#----------------------------------------#
Question:

Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension.



Hints:
Use random.choice() to a random element from a list.


Solution:

import random
print random.choice([i for i in range(11) if i%2==0])


#----------------------------------------#
Question:

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.



Hints:
Use random.choice() to a random element from a list.


Solution:

import random
print random.choice([i for i in range(201) if i%5==0 and i%7==0])



#----------------------------------------#

Question:

Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive.



Hints:
Use random.sample() to generate a list of random values.


Solution:

import random
print random.sample(range(100), 5)

#----------------------------------------#
Question:

Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive.



Hints:
Use random.sample() to generate a list of random values.


Solution:

import random
print random.sample([i for i in range(100,201) if i%2==0], 5)


#----------------------------------------#
Question:

Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive.



Hints:
Use random.sample() to generate a list of random values.


Solution:

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

#----------------------------------------#

Question:

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



Hints:
Use random.randrange() to a random integer in a given range.


Solution:

import random
print random.randrange(7,16)

#----------------------------------------#

Question:

Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!".



Hints:
Use zlib.compress() and zlib.decompress() to compress and decompress a string.


Solution:

import zlib
s = 'hello world!hello world!hello world!hello world!'
t = zlib.compress(s)
print t
print zlib.decompress(t)

#----------------------------------------#
Question:

Please write a program to print the running time of execution of "1+1" for 100 times.



Hints:
Use timeit() function to measure the running time.

Solution:

from timeit import Timer
t = Timer("for i in range(100):1+1")
print t.timeit()

#----------------------------------------#


#----------------------------------------#



#----------------------------------------#


#----------------------------------------#

0 comments on commit 3c748f8

Please sign in to comment.