Python Code
Python Code
Q1: Given two matrices please print the product of those two matrices
Ex 1: A = [[1 3 4]
[2 5 7]
[5 9 6]]
B = [[1 0 0]
[0 1 0]
[0 0 1]]
A*B = [[1 3 4]
[2 5 7]
[5 9 6]]
Ex 2: A = [[1 2]
[3 4]]
B = [[1 2 3 4 5]
[5 6 7 8 9]]
A*B = [[11 14 17 20 23]
[18 24 30 36 42]]
Ex 3: A = [[1 2]
[3 4]]
B = [[1 4]
[5 6]
[7 8]
[9 6]]
A*B =Not possible
In [3]: # Q1: Given two matrices please print the product of those two matrices.
def makeMatrix():
'''This function creats a matrix by taking row and column values as inputs''
r = int(input("Enter the max row :"))
c = int(input("Enter the max column :"))
print("Enter the numbers in a row wise :")
matrix = [[int(input("Element ({0},{1}):".format(i, j)))for j in range(c)]for
return matrix
def printMatrix(matrix):
'''This function prints the matrix by taking matrix as argument'''
for r in matrix:
print(r)
Element (1,0):1
Element (1,1):2
Matrices are
First matrix
[1, 2]
[1, 2]
Second matrix
[1, 0]
[0, 1]
Resultant matrix
[1, 2]
[1, 2]
Ex 1: A = [0 5 27 6 13 28 100 45 10 79]
let f(x) denote the number of times x getting selected in 100 experiment
s.
f(100) > f(79) > f(45) > f(28) > f(27) > f(13) > f(10) > f(6) > f(5) > f
(0)
In [7]: # Q2: Select a number randomly with probability proportional to its magnitude fro
lst = []
size = int(input("Enter the size of the list : "))
print("Enter {0} numbers ".format(size))
for i in range(size):
lst.append(int(input()))
#A bigger list for storing the values repetitively based on their magnitudes
bigList = []
for i in lst:
for j in range(i):
bigList.append(i)
print("\n\nRandom picking 100 numbers from the list based on probability proporti
for i in range(100):
x = int(uniform(0, bigListLen))
print(bigList[x], end = ", ")
Random picking 100 numbers from the list based on probability proportional to i
ts magnitude
100, 100, 100, 100, 23, 100, 23, 100, 23, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 12, 100, 100, 23, 100, 100, 100, 100, 4,
100, 100, 100, 100, 100, 100, 100, 100, 23, 100, 100, 100, 100, 10
0, 12, 23, 100, 100, 12, 4, 100, 100, 100, 100, 100, 100, 100, 10
0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 10
0, 100, 100, 100, 100, 100, 100, 23, 100, 23, 100, 100, 100, 23,
100, 100, 100, 100, 100, 100, 100, 23, 100, 100, 100, 23, 100, 23,
100, 4, 100, 23,
import re
def digit2Hash(string):
_string = ""
pattern = re.compile(r'\d')
matches = pattern.findall(string)
for match in matches:
_string += "#"
return _string
Ex 1:
Students=['student1','student2','student3','student4','student5','studen
t6','student7','student8','student9','student10']
Marks = [45, 78, 12, 14, 48, 43, 47, 98, 35, 80]
a.
student8 98
student10 80
student2 78
student5 48
student7 47
b.
student3 12
student4 14
student9 35
student6 43
student1 45
c.
student9 35
student6 43
student1 45
student7 47
student5 48
Len = len(Students)
keepTrack = dict()
for i in range(Len):
keepTrack[Students[i]] = Marks[i]
_keepMarks = sorted(keepTrack.values(), reverse = True)
def findKey(value):
for k, v in keepTrack.items():
if v == value:
return k
# d. Who got marks between >25th percentile <75th percentile, in the increasing o
print("\n>Students who got marks between >25th percentile <75th percentile, in th
for i in range(numOfStudents):
value = _keepMarks[i]
if (0.25*totalMarks) < value < (0.75*totalMarks):
value = _keepMarks[i]
print(findKey(value),"-",value)
Enter Names
Name of student 1 : a
Name of student 2 : b
Name of student 3 : c
Name of student 4 : d
Name of student 5 : e
Name of student 6 : f
Name of student 7 : g
Name of student 8 : h
Name of student 9 : i
Name of student 10 : j
j - 10
i - 9
h - 8
g - 7
f - 6
a - 1
b - 2
c - 3
d - 4
e - 5
>Students who got marks between >25th percentile <75th percentile, in the inc
reasing order of marks
c - 3
d - 4
e - 5
f - 6
g - 7
Cosine distance between two points (x,y) and (p,q) is defined as 𝑐𝑜𝑠−1 ( √(𝑥2+(𝑥⋅𝑝+𝑦⋅𝑞)
𝑦2 )⋅√(𝑝2 +𝑞2 ) )
Ex:
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1)(6,0),(1,-1)]
P= (3,-4)
Output:
(6,-7)
(1,-1)
(6,0)
(-5,-8)
(-1,-1)
import math
S = []
numOfPoints = int(input("Enter the total number of close points : "))
print("Enter points")
for i in range(numOfPoints):
S.append(tuple(int(x) for x in input().split(',')))
print(S)
sLen= len(S)
P= tuple(int(x) for x in input("\nEnter the point from which close points has to
resList = []
p, q = P
for i in S:
x, y = i
resList.append(math.acos((x*p+y*q)/(math.sqrt(x*x + y*y)*math.sqrt(p*p + q*q)
distanceDict = {}
for i in range(sLen):
distanceDict[S[i]] = resList[i]
distanceValue = sorted(distanceDict.values())
def findKey(value):
for k, v in distanceDict.items():
if v == value:
return k
print("\nClosest points :\n")
for i in range(5):
key = findKey(distanceValue[i])
print(key, distanceValue[i])
Enter the point from which close points has to be found : 3,-4
Closest points :
In [ ]:
Red =[(R11,R12),(R21,R22),(R31,R32),(R41,R42),(R51,R52),..,(Rn1,Rn2)]
Blue=[(B11,B12),(B21,B22),(B31,B32),(B41,B42),(B51,B52),..,(Bm1,Bm2)]
and set of line equations(in the string format, i.e list of strings)
Your task here is to print "YES"/"NO" for each line given. You should print YES, if all the red points
are one side of the line and blue points are on other side of the line, otherwise you should print
NO.
Ex:
Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]
Output:
YES
NO
NO
YES
import re
Red= []
Blue= []
Lines=[]
seperateRed = []
seperateBlue = []
#If all the red and blue points are seperated return Yes else No
if(all(seperateRed) and all(seperateBlue)):
print("Yes")
else:
print("No")
seperateRed.clear()
seperateBlue.clear()
doesItSeperate(a, b, c)
Ex 4: _, _, 30, _, _, _, 50, _, _
==> we will fill the missing values from left to right
a. first we will distribute the 30 to left two missing values (10, 1
0, 10, _, _, _, 50, _, _)
b. now distribute the sum (10+50) missing values in between (10, 10,
12, 12, 12, 12, 12, _, _)
c. now we will distribute 12 to right side missing values (10, 10, 1
2, 12, 12, 12, 4, 4, 4)
for a given string with comma seprate values, which will have both missing values numbers like ex:
"_, _, x, _, _, _" you need fill the missing values
Q: your program reads a string like ex: "_, _, x, _, _, _" and returns the filled sequence
Ex:
Input1: "_,_,_,24"
Output1: 6,6,6,6
Input2: "40,_,_,_,60"
Output2: 20,20,20,20,20
Input3: "80,_,_,_,_"
Output3: 16,16,16,16,16
Input4: "_,_,30,_,_,_,50,_,_"
Output4: 10,10,12,12,12,12,4,4,4
for i in range(right):
if seperated[i] == "_":
seperated[i] = 0
else:
seperated[i] = int(seperated[i])
1. The first column F will contain only 5 uniques values (F1, F2, F3, F4, F5)
2. The second column S will contain only 3 uniques values (S1, S2, S3)
Ex:
[[F1,S1],[F2,S2],[F3,S3],[F1,S2],[F2,S3],[F3,S2],[F2,S1],[F4,S1],[F4,S
3],[F5,S1]]
Ex:
S1 = S1.split(" ")
S1 = set(S1)
S2 = S2.split(" ")
S2 = set(S2)
print()
# a. Number of common words between S1, S2
print("a> Number of common words between Sentence 1, Sentence 2 : ",len(S1.inters
# b. Words in S1 but not in S2
print("b> Words in Sentence 1 but not in Sentence 2 : ",S1 - S2)
# b. Words in S2 but not in S1
print("b> Words in Sentence 2 but not in Sentence 1 : ",S2 - S1)
Ex:
[[1, 0.4], [0, 0.5], [0, 0.9], [0, 0.3], [0, 0.6], [1, 0.1], [1, 0.9],
[1, 0.8]]
output:
0.44982
−1 ⋅ ((1 ⋅ 𝑙𝑜𝑔10 (0.4) + 0 ⋅ 𝑙𝑜𝑔10 (0.6)) + (0 ⋅ 𝑙𝑜𝑔10 (0.5) + 1 ⋅ 𝑙𝑜𝑔10 (0.5))+...+(1 ⋅ 𝑙𝑜𝑔10 (0.8) +
8
import math
X = [[1, 0.4],
[0, 0.5],
[0, 0.9],
[0, 0.3],
[0, 0.6],
[1, 0.1],
[1, 0.9],
[1, 0.8]]
def errorFunction(X):
xLen = len(X)
A = []
B = []
for i in range(xLen):
A.append(X[i][0])
B.append(X[i][1])
res = 0
for i in range(xLen):
y = A[i]
Y = B[i]
res += ((y*math.log10(Y)+(1-y)*math.log10(1-Y)))
return res
0.42430993457031635
In [ ]: