def count(num,i):
if num==0:
return 0
if num%i==0:
return 1+count(num/i,i)
else:
return 0
def countzero(list):
count2=0
count5=0
for i in range(len(list)):
count2 += count(list[i], 2)
count5 += count(list[i], 5)
if count2>count5:
return count5 ...- read more
10.lowest common multiple (lcm)
read morea=10 b=24 def fun(a,b): for i in range(max(a,b),a*b+1): if i%a==0 and i%b==0: n=i break return n print (fun(a,b))
9.greatest common divisor(gcd)
read morea=10 b=24 def fun(a,b): for i in range(1,min(a,b)+1): if a%i==0 and b%i==0: n=i return n print (fun(a,b))
8.the median
read moreL=[0,1,4,2,3,4] L.sort() if len(L)%2==1: print L[len(L)//2] else: print (L[len(L)/2]+L[len(L)/2-1])/2.0
7.For the rectangular area
read moreprint a*b,2*(a+b)
6.To solve all the prime Numbers within 100
read moreprimes = [] for i in range(2, 101): for t in range(2, i): if i % t == 0: break else: primes.append(i) print primes print ' '.join(map(str, primes))
5.The output characters of the odd location of the string
read more# method 1 a= "12345" print "".join([a[i] for i in range(len(a)) if i%2==0]) print ( ''.join([ c for i, c in enumerate(a) if i % 2 == 0]) ) # i is list[0, 1, 2, 3, 4], c is ['1', '2', '3', '4', '5'] print ( ''.join([ str(i ...
4.Output the dictionary keys
read morea={1:1,2:2,3:3} # method 1 print ",".join([str(i) for i in a.keys()]) # attention! join string! a.keys() is a list # method 2 print ",".join(map(str, a.keys())) # map(func, list)
3.Reverse a string
read morea="12345" # method 1 print(a[::-1]) # method 2 from functools import reduce print reduce((lambda x, y: y+x), a) # reduce is [ func(func(s1, s2),s3), ... , sn ] # method 3 print "".join(reversed(a)) # reversed(object) return a iterator, type 'reversed', join(iterable)
2.Sort the list
read moreL=[2,8,5,3] L.sort() print L new_list = sorted(L) print new_list