a=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))
- read more
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
9. Palindrome Number
read moreclass Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool """ y = str(x) if len(y)==1: return True else: pal=True i=0 while i < len(y) and pal: if y[i]==y[len(y)-1-i]: i+=1 else: pal=False return pal if __name__=="__main__ ...