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 81b147a

Browse files
committed
added more questions
1 parent 3c748f8 commit 81b147a

File tree

1 file changed

+324
-0
lines changed

1 file changed

+324
-0
lines changed

‎100+ Python challenging programming exercises.txt‎

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,13 +2040,337 @@ t = Timer("for i in range(100):1+1")
20402040
print t.timeit()
20412041

20422042
#----------------------------------------#
2043+
Question:
2044+
2045+
Please write a program to shuffle and print the list [3,6,7,8].
2046+
2047+
2048+
2049+
Hints:
2050+
Use shuffle() function to shuffle a list.
2051+
2052+
Solution:
2053+
2054+
from random import shuffle
2055+
li = [3,6,7,8]
2056+
shuffle(li)
2057+
print li
2058+
2059+
#----------------------------------------#
2060+
Question:
2061+
2062+
Please write a program to shuffle and print the list [3,6,7,8].
2063+
2064+
2065+
2066+
Hints:
2067+
Use shuffle() function to shuffle a list.
2068+
2069+
Solution:
2070+
2071+
from random import shuffle
2072+
li = [3,6,7,8]
2073+
shuffle(li)
2074+
print li
2075+
2076+
2077+
2078+
#----------------------------------------#
2079+
Question:
2080+
2081+
Please write a program to generate all sentences where subject is in ["I", "You"] and verb is in ["Play", "Love"] and the object is in ["Hockey","Football"].
2082+
2083+
Hints:
2084+
Use list[index] notation to get a element from a list.
2085+
2086+
Solution:
2087+
2088+
subjects=["I", "You"]
2089+
verbs=["Play", "Love"]
2090+
objects=["Hockey","Football"]
2091+
for i in range(len(subjects)):
2092+
for j in range(len(verbs)):
2093+
for k in range(len(objects)):
2094+
sentence = "%s %s %s." % (subjects[i], verbs[j], objects[k])
2095+
print sentence
2096+
2097+
2098+
#----------------------------------------#
2099+
Please write a program to print the list after removing delete even numbers in [5,6,77,45,22,12,24].
2100+
2101+
Hints:
2102+
Use list comprehension to delete a bunch of element from a list.
2103+
2104+
Solution:
2105+
2106+
li = [5,6,77,45,22,12,24]
2107+
li = [x for x in li if x%2!=0]
2108+
print li
2109+
2110+
#----------------------------------------#
2111+
Question:
2112+
2113+
By using list comprehension, please write a program to print the list after removing delete numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155].
2114+
2115+
Hints:
2116+
Use list comprehension to delete a bunch of element from a list.
2117+
2118+
Solution:
2119+
2120+
li = [12,24,35,70,88,120,155]
2121+
li = [x for x in li if x%5!=0 and x%7!=0]
2122+
print li
2123+
2124+
2125+
#----------------------------------------#
2126+
Question:
2127+
2128+
By using list comprehension, please write a program to print the list after removing the 0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155].
2129+
2130+
Hints:
2131+
Use list comprehension to delete a bunch of element from a list.
2132+
Use enumerate() to get (index, value) tuple.
2133+
2134+
Solution:
2135+
2136+
li = [12,24,35,70,88,120,155]
2137+
li = [x for (i,x) in enumerate(li) if i%2!=0]
2138+
print li
2139+
2140+
#----------------------------------------#
2141+
2142+
Question:
2143+
2144+
By using list comprehension, please write a program generate a 3*5*8 3D array whose each element is 0.
2145+
2146+
Hints:
2147+
Use list comprehension to make an array.
2148+
2149+
Solution:
2150+
2151+
array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)]
2152+
print array
2153+
2154+
#----------------------------------------#
2155+
Question:
2156+
2157+
By using list comprehension, please write a program to print the list after removing the 0th,4th,5th numbers in [12,24,35,70,88,120,155].
2158+
2159+
Hints:
2160+
Use list comprehension to delete a bunch of element from a list.
2161+
Use enumerate() to get (index, value) tuple.
2162+
2163+
Solution:
2164+
2165+
li = [12,24,35,70,88,120,155]
2166+
li = [x for (i,x) in enumerate(li) if i not in (0,4,5)]
2167+
print li
2168+
2169+
2170+
2171+
#----------------------------------------#
2172+
2173+
Question:
2174+
2175+
By using list comprehension, please write a program to print the list after removing the value 24 in [12,24,35,24,88,120,155].
2176+
2177+
Hints:
2178+
Use list's remove method to delete a value.
2179+
2180+
Solution:
2181+
2182+
li = [12,24,35,24,88,120,155]
2183+
li = [x for x in li if x!=24]
2184+
print li
2185+
2186+
2187+
#----------------------------------------#
2188+
Question:
2189+
2190+
With two given lists [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a program to make a list whose elements are intersection of the above given lists.
20432191

2192+
Hints:
2193+
Use set() and "&=" to do set intersection operation.
2194+
2195+
Solution:
2196+
2197+
set1=set([1,3,6,78,35,55])
2198+
set2=set([12,24,35,24,88,120,155])
2199+
set1 &= set2
2200+
li=list(set1)
2201+
print li
2202+
2203+
#----------------------------------------#
2204+
2205+
With a given list [12,24,35,24,88,120,155,88,120,155], write a program to print this list after removing all duplicate values with original order reserved.
2206+
2207+
Hints:
2208+
Use set() to store a number of values without duplicate.
2209+
2210+
Solution:
2211+
2212+
def removeDuplicate( li ):
2213+
newli=[]
2214+
seen = set()
2215+
for item in li:
2216+
if item not in seen:
2217+
seen.add( item )
2218+
newli.append(item)
2219+
2220+
return newli
2221+
2222+
li=[12,24,35,24,88,120,155,88,120,155]
2223+
print removeDuplicate(li)
2224+
2225+
2226+
#----------------------------------------#
2227+
Question:
2228+
2229+
Define a class Person and its two child classes: Male and Female. All classes have a method "getGender" which can print "Male" for Male class and "Female" for Female class.
2230+
2231+
Hints:
2232+
Use Subclass(Parentclass) to define a child class.
2233+
2234+
Solution:
2235+
2236+
class Person(object):
2237+
def getGender( self ):
2238+
return "Unknown"
2239+
2240+
class Male( Person ):
2241+
def getGender( self ):
2242+
return "Male"
2243+
2244+
class Female( Person ):
2245+
def getGender( self ):
2246+
return "Female"
2247+
2248+
aMale = Male()
2249+
aFemale= Female()
2250+
print aMale.getGender()
2251+
print aFemale.getGender()
2252+
2253+
2254+
2255+
#----------------------------------------#
2256+
Question:
2257+
2258+
Please write a program which count and print the numbers of each character in a string input by console.
2259+
2260+
Example:
2261+
If the following string is given as input to the program:
2262+
2263+
abcdefgabc
2264+
2265+
Then, the output of the program should be:
2266+
2267+
a,2
2268+
c,2
2269+
b,2
2270+
e,1
2271+
d,1
2272+
g,1
2273+
f,1
2274+
2275+
Hints:
2276+
Use dict to store key/value pairs.
2277+
Use dict.get() method to lookup a key with default value.
2278+
2279+
Solution:
2280+
2281+
dic = {}
2282+
s=raw_input()
2283+
for s in s:
2284+
dic[s] = dic.get(s,0)+1
2285+
print '\n'.join(['%s,%s' % (k, v) for k, v in dic.items()])
2286+
2287+
#----------------------------------------#
2288+
2289+
Question:
2290+
2291+
Please write a program which accepts a string from console and print it in reverse order.
2292+
2293+
Example:
2294+
If the following string is given as input to the program:
2295+
2296+
rise to vote sir
2297+
2298+
Then, the output of the program should be:
2299+
2300+
ris etov ot esir
2301+
2302+
Hints:
2303+
Use list[::-1] to iterate a list in a reverse order.
2304+
2305+
Solution:
2306+
2307+
s=raw_input()
2308+
s = s[::-1]
2309+
print s
20442310

20452311
#----------------------------------------#
20462312

2313+
Question:
2314+
2315+
Please write a program which accepts a string from console and print the characters that have even indexes.
2316+
2317+
Example:
2318+
If the following string is given as input to the program:
2319+
2320+
H1e2l3l4o5w6o7r8l9d
20472321

2322+
Then, the output of the program should be:
2323+
2324+
Helloworld
20482325

2326+
Hints:
2327+
Use list[::2] to iterate a list by step 2.
2328+
2329+
Solution:
2330+
2331+
s=raw_input()
2332+
s = s[::2]
2333+
print s
20492334
#----------------------------------------#
20502335

20512336

2337+
Question:
2338+
2339+
Please write a program which prints all permutations of [1,2,3]
2340+
2341+
2342+
Hints:
2343+
Use itertools.permutations() to get permutations of list.
2344+
2345+
Solution:
2346+
2347+
import itertools
2348+
print list(itertools.permutations([1,2,3]))
2349+
20522350
#----------------------------------------#
2351+
Question:
2352+
2353+
Write a program to solve a classic ancient Chinese puzzle:
2354+
We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have?
2355+
2356+
Hint:
2357+
Use for loop to iterate all possible solutions.
2358+
2359+
Solution:
2360+
2361+
def solve(numheads,numlegs):
2362+
ns='No solutions!'
2363+
for i in range(numheads+1):
2364+
j=numheads-i
2365+
if 2*i+4*j==numlegs:
2366+
return i,j
2367+
return ns,ns
2368+
2369+
numheads=35
2370+
numlegs=94
2371+
solutions=solve(numheads,numlegs)
2372+
print solutions
2373+
2374+
#----------------------------------------#
2375+
2376+

0 commit comments

Comments
(0)

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