EXERCISE - 9 FUNCTIONS

A) Write a function nearly_equal to test whether two strings are nearly equal.
Two strings a and b are nearly equal when a can be generated by a single
mutation on b. B) Write a function dups to find all duplicates in the list. C) Write a function unique to find all the unique elements of a list.
A) AIM: Write a function nearly_equal to test whether two strings are nearly
equal. Two strings a and b are nearly equal when a can be generated by a
single mutation on b.
SOURCE CODE:
def nearly_equal(str1,str2): count=0 i=j=0 while(i<len(str1) and j<len(str2)): if(str1[i]!=str2[j]): count=count+1 if(len(str1)>len(str2)): i=i+1 elif(len(str1)==len(str2)): pass else: i=i-1 if(count>1): return False i=i+1 j=j+1 if(count<2): return True str1=input("Enter first string::\n") str2=input("Enter second string::\n") boolean=nearly_equal(str1,str2) if(boolean): print("Strings are nearly equal.") else: print("Strings are not equal.")
OUTPUT: Enter first string:: RISE Enter second string:: RISEE Strings are nearly equal. B) AIM: Write a function dups to find all duplicates in the list. SOURCE CODE: def dups(numlist): duplicates={} for ele in numlist: c=numlist.count(ele) if(c>=2): duplicates[ele]=c print("Duplicate elements are:\n",duplicates) return numlist=[] n=int(input("Enter number of elements to be insert:\n")) for i in range(n): ele=int(input("Enter element")) numlist.append(ele) dups(numlist) OUTPUT: Enter number of elements to be insert: 5 Enter element2 Enter element4 Enter element2 Enter element3 Enter element3 Duplicate elements are: {2: 2, 3: 2} C) AIM: Write a function unique to find all the unique elements of a list. SOURCE CODE: def unique(numlist): uniqueele=[] for ele in numlist: c=numlist.count(ele) if(c==1): uniqueele.append(ele) print("Unique elements are:\n",uniqueele) return numlist=[] n=int(input("Enter number of elements to be insert:\n")) for i in range(n): ele=int(input("Enter element")) numlist.append(ele) unique(numlist) OUTPUT: Enter number of elements to be insert: 5 Enter element2 Enter element3 Enter element4 Enter element3 Enter element2 Unique elements are: [4]