Posts

Showing posts from 2017

Insertion Sort

In this sorting algorithm, we check every element with it's previous element. If it is lesser than the previous element than we swap. We keeping swapping until the element achieves it's proper place. It's time complexity is O(n^2). In case the given list is already sorted, this algorithm works very fast. A simple code implementation is:- def insertionSort(l):   for i in range(len(l)):     pos=i     while(l[pos]>l[pos-1] and pos>0):       (l[pos],l[pos-1])=(l[pos-1],l[pos])       pos-=1   return l

Selection Sort

The following is the basic code of Selection Sort algorithm:- def selectionSort(l):   for i in range(len(l)):     minpos=i     for j in range(i,len(l)):       if l[j]<l[minpos]:         minpos=j     (l[i],l[minpos])=(l[minpos],l[i])           return l         

Sum of squares

Q.  A positive integer   n   is a sum of squares if   n = i 2  + j 2   for integers   i,j   such that   i ≥ 1   and   j ≥ 1 . For instance, 10 is a sum of squares because 10 = 1 2   + 3 2 , and so is 25 (3 2   + 4 2 ). On the other hand, 11 and 3 are not sums of squares. Write a Python function  sumofsquares(n)  that takes a positive integer argument and returns  True  if the integer is a sum of squares, and  False  otherwise. Ans- def sumofsquares(n):   (i,j)=(1,1)   for i in range(1,n):     for j in range(1,n):       if n == (i*i) + (j*j):         return (True)            return False   

Non-decreasing list (using recursion)

Q.  A list is a non-decreasing if each element is at least as big as the preceding one. For instance  [] ,  [7] ,  [8,8,11]  and  [3,19,44,44,63,89]  are non-decreasing, while  [3,18,4]  and  [23,14,3,14,3,23]  are not. Here is a recursive function to check if a list is non-decreasing. You have to fill in the missing argument for the recursive call. Ans- def nondecreasing(l):   if l==[] or len(l) == 1:     return(True)   else:     return( l[0] <= l[1] and nondecreasing(l[1:]))

Each element in its input list is at most as big as the one before it

Question:- Define a Python function "descending(l)" that returns True if each element in its input list is at most as big as the one before it For instance: >>> descending([]) True >>> descending([4,4,3]) True >>> descending([19,17,18,7]) False Answer:- def descending(l):   a=True   if l==[]:     return True   for x in range(0,len(l)-1):     if l[x]<l[x+1]:         a=False         break         return a

Sum of prime numbers in list

Question:-  Write a function sumprimes(l) that takes as input a list of integers and returns the sum of all the prime numbers in l. Here are some examples to show how your function should work. >>> sumprimes([3,3,1,13]) 19 >>> sumprimes([2,4,6,9,11]) 13 >>> sumprimes([-3,1,6]) 0 Solution:- def prime (a): for j in range ( 2 ,a): if a % j == 0 : return False if a == 1 : return False if a <= 0 : return False return True def sumprimes (l): sum = 0 for i in range ( 0 , len (l)): if prime(l[i]): sum = sum + l[i] return sum

Bracket Checking

Question:- Write a function matched(s) that takes as input a   string s and checks if the brackets "(" and ")" in s are   matched: that is, every "(" has a matching ")" after it and   every ")" has a matching "(" before it.    Your function should   ignore all other symbols that appear in s.    Your function   should return True if s has matched brackets and False if it   does not. Here are some examples to show how your function should work. >>> matched("zb%78") True >>> matched("(7)(a") False >>> matched("a)*(?") False >>> matched("((jkl)78(A)&l(8(dd(FJI:),):)?)") True Solution:- def matched (s): a = 0 for i in range ( 0 , len (s)): if s[i] == '(' : if a >= 0 : a = a + 1 elif s[i] == ')' : a = a - 1 if a == 0 : return True else : return False

Reversing a number

Question:- Write a function intreverse(n) that takes as input a positive integer n and returns the integer obtained by reversing the digits in n. Here are some examples of how your function should work. >>> intreverse(783) 387 >>> intreverse(242789) 987242 >>> intreverse(3) 3 Solution:- def intreverse (n): a = "" while n > 0 : b = str (n % 10 ) a = a + b n = n // 10 return int (a)