Grpa Week 4 Solutions

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

WEEK 4

PROGRAM 1:

In the first line of input, accept a sequence of space-separated words. In the


second line of input, accept a single word. If this word is not present in the
sequence, print NO. If this word is present in the sequence, then print YES and in
the next line of the output, print the number of times the word appears in the
sequence.

SOLUTION 1:

sentence = input()
word = input()
l = sentence.split()
if (word not in l):
print('NO')
else:
count = 0
while(word in l):
count +=1
l.remove(word)
print('YES')
print(count)

PROGRAM 2:

You are given a list marks that has the marks scored by a class of students in a
Mathematics test. Find the median marks and store it in a float variable named
median. You can assume that marks is a list of float values

SOLUTION 2:

def solution(marks):
temp=0
for i in range(0, len(marks)):
for j in range(i+1, len(marks)):
if(marks[i] > marks[j]):
temp = marks[i];
marks[i] = marks[j];
marks[j] = temp;
if(len(marks)%2==0):
center=marks[int((len(marks)/2)-1)]
center1= marks[int((len(marks)/2))]
median=(center+center1)/2
else:
median=marks[len(marks)/2]
### Enter your solution above this line
return median

PROGRAM 3:

Accept two square matrices AA and BB of dimensions n \times nn×n as input and
compute their product ABAB.

SOLUTION 3:

n = int(input())
A = []
B = []
for i in range(n):
row=input()
row = row.split(",")
A.append(row)
for i in range(n):
row = input()
row = row.split(",")
B.append(row)
for k in range(n):
for i in range(n):
sum = 0
for j in range(n):
sum += (int(A[k][j])*int(B[j][i]))
if(i < n-1):
print(sum,end="")
print(",",end="")
else:
print(sum)

PROGRAM 4:

You are given the names and dates of birth of a group of people. Find all pairs of
members who share a common date of birth. Note that this date need not be common
across all pairs. It is sufficient if both members in a pair have the same date of
birth.

SOLUTION 4:

names = input().split(',')
bdays = input().split(',')
n = len(names)
for i in range(n):
bdays[i] = int(bdays[i])

common = [ ]
for i in range(n):
for j in range(n):
if ((i != j) and
(bdays[i] == bdays[j]) and
names[i] < names[j]):
pair = [names[i], names[j]]
common.append(pair)

PROGRAM 5:

You are given a sequence of nn points, (x_i,y_i)(x


i

,y
i

), 1\leq i \leq n1≤i≤n, in the 2-D plane as input. Also, you are given a point PP
with coordinates (x,y)(x,y). Print all points in the sequence that are nearest to
PP. If multiple points have the same least distance from PP, print the points in
the order of their appearance in the sequence.
SOLUTION 5:

n = int(input())
L = [ ]
# Append all points in the sequence to the list L
for i in range(n):
L.append(input())
# Point P
point = input().split(',')
x = int(point[0])
y = int(point[1])

# List to maintain all nearest points


min_list = []
min_dist = 1000
for i in range(n):
# One of the points in the sequence
temp = L[i].split(',')
# Extract the x and y coordinates of this point
temp_x = int(temp[0])
temp_y = int(temp[1])
# Compute the distance
dist = ((x - temp_x) ** 2 + (y - temp_y) ** 2) ** 0.5
# Check if it is the minimum distance
if (dist < min_dist):
min_dist = dist
min_list = [L[i]]
elif dist == min_dist:
min_list.append(L[i])
for point in min_list:
print(point)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy