0% found this document useful (0 votes)
13 views6 pages

Assignment5 20243129

Uploaded by

Kanha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Assignment5 20243129

Uploaded by

Kanha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

assignment5-20243129

November 10, 2024

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()

#shallow copy example


#any changes made to a copy of an object do reflect in original object
array2 = array1.view()
#array2 = copy.copy(array1) ...not used for shallow copy in array...
print("array2 =",array2)
print()

array2[0]=50
print("array1 and array2 after updating array2[0] to 50")
print(array1)
print(array2)
print()

#deep copy example


#any changes made to a copy of an object do not reflect in original object
array3 = copy.deepcopy(array1)
print("array3 =",array3)
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]

array1 and array2 after updating array2[0] to 50


[50 3 4]
[50 3 4]

2
array3 = [50 3 4]

array1 and array3 after updating array3[0] to 25


[50 3 4]
[25 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

# math module functions


x = 0.5
print("math.acos(", x, ") =", math.acos(x))
print("math.ceil(", x, ") =", math.ceil(x))
print("math.exp(", x, ") =", math.exp(x))
print("math.gcd(12, 18) =", math.gcd(12, 18))

# cmath module functions


z = 1 + 1j
print("cmath.tan(", z, ") =", cmath.tan(z))
print("cmath.log10(", z, ") =", cmath.log10(z))
print("cmath.isclose(1.0, 1.00002) =", cmath.isclose(1.0, 1.00002))

math.acos( 0.5 ) = 1.0471975511965979


math.ceil( 0.5 ) = 1
math.exp( 0.5 ) = 1.6487212707001282
math.gcd(12, 18) = 6
cmath.tan( (1+1j) ) = (0.2717525853195118+1.0839233273386946j)
cmath.log10( (1+1j) ) = (0.15051499783199057+0.3410940884604603j)
cmath.isclose(1.0, 1.00002) = False
Q4. implement mid semester paper.
q2
[ ]: reallist=[-2,-3,4,-1,-2,1,5,-3]
maxlist = []
for a in range(len(reallist)):
for b in range(a,len(reallist)+1):
newlist=reallist[a:b]
if sum(maxlist)<sum(newlist):
maxlist=newlist

print(maxlist)

[4, -1, -2, 1, 5]

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 (reverse=true):


[(5, 4), (1, 2), (3, 1)]

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 1st para:is this code right?


enter 2nd para:is the code right?
1st para and 2nd para are not same for the first time at 5 th index
q6
[ ]: a=int(input("enter a number:"))
k=0
for i in range (1,2*a):
j=a-k
while (j>0) :
print(j,end=" ")
j-=1
if i<=a-1 : k+=1
else : k-=1
print()

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

You might also like