Assignment5 20243129
Assignment5 20243129
cc
Q1. Create a NumPy and apply methods such as append(), insert(),pop(), remove(), index(),
sort(), and reverse.
[ ]: import numpy as np
ary = np.array([1,2,3,4,5])
print("ary= ",ary)
#append()
ary = np.append(ary,6)
print("after append:",ary)
#insert()
ary = np.insert(ary,1,25) #inserts 25 at 1st index
print("after insert:",ary)
#pop()
ary = np.delete(ary,0) # pops elemnt at 0 index
print("after pop:",ary)
#remove()
ary = np.delete(ary,np.where(ary==25)) # removes 25
print("after remove:",ary)
#index()
index = np.where(ary==4)[0][0]
print("index of 4:",index)
#sort
ary = np.sort(ary)
print("after sort:",ary)
#reverse
ary = ary[::-1]
print("after reverse:",ary)
ary= [1 2 3 4 5]
after append: [1 2 3 4 5 6]
1
after insert: [ 1 25 2 3 4 5 6]
after pop: [25 2 3 4 5 6]
after remove: [2 3 4 5 6]
index of 4: 2
after sort: [2 3 4 5 6]
after reverse: [6 5 4 3 2]
Q2. write a program to copy array with assignment operator,shallow copy method and deep copy
method.
[ ]: import numpy as np
import copy
array1= np.array([0,3,4])
print("array1 =",array1)
print()
array2[0]=50
print("array1 and array2 after updating array2[0] to 50")
print(array1)
print(array2)
print()
array3[0]=25
print("array1 and array3 after updating array3[0] to 25")
print(array1)
print(array3)
print()
array1 = [0 3 4]
array2 = [0 3 4]
2
array3 = [50 3 4]
Q3. write a program for math and cmath module functuonality in your program. E.g.,math.acos(),
math.ceil(), math.exp(), math.gcd(), cmath.tan(x), cmath.log10(x), cmath.isclose().
[ ]: import math
import cmath
print(maxlist)
3
q3
[ ]: string = "aaaabbbccd"
string1=string+" "
newstr=""
a=string[0]
count=1
b=1
while (b<len(string1)):
c=string1[b]
if (c==a):
count+=1
else:
newstr=newstr+str(string1[b-1])+str(count)
count=1
b+=1
a=c
print(newstr)
a4b3c2d1
q4
[ ]: def custom_sort(access, reverse=False):
dict={}
for i in range(len(access)):
dict[access[i][1]] = access[i]
ele2=list(dict.keys())
ele2.sort(reverse=reverse)
ans=[dict[ele2[i]] for i in range(len(ele2))]
print(ans)
print("custom_sort output (reverse=true): ")
custom_sort([(1, 2), (3, 1), (5, 4)], True)
print()
print("custom_sort output: ")
custom_sort([(1, 2), (3, 1), (5, 4)])
custom_sort output:
[(3, 1), (1, 2), (5, 4)]
q5
[ ]: str1=str(input("enter 1st para:"))
str2=str(input("enter 2nd para:"))
4
a=0
i=0
less = len(str1) if len(str1)<len(str2) else len(str2)
for i in range (less):
if(str1[i]!=str2[i]):
a=1
b=i
break
if(a==0):
print("1st para and 2nd para are same")
else:
print("1st para and 2nd para are not same for the first time at ",b,"th␣
↪index ")
enter a number:7
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
q7
5
[ ]: str1=str(input("enter a line in which you want to hide numbers : "))
l1=str1.split()
for elm in l1:
if(elm.isdigit()):
b=len(elm)
c='x'*b
str1=str1.replace(str(elm),c)
print(str1)
enter a line in which you want to hide numbers : user id is john 17, pasword is
5423
user id is john 17, pasword is xxxx