0% found this document useful (0 votes)
26 views4 pages

Python Week 5 - 6 GrPA (Made by Unknown)

Python week 5-6 grpa IIT Madras data science

Uploaded by

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

Python Week 5 - 6 GrPA (Made by Unknown)

Python week 5-6 grpa IIT Madras data science

Uploaded by

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

week 5

1.

min=max=None
def get_max(L):
maxi = L[0]
for x in L:
if x > maxi:
maxi = x
return maxi

# get the minimum


def get_min(L):
mini = L[0]
for x in L:
if x < mini:
mini = x
return mini

# get the range


def get_range(L):
maxi = get_max(L)
mini = get_min(L)
return maxi - mini

2.

def is_perfect(num):
# Factor sum
fsum = 0
for f in range(1, num):
if num % f == 0:
fsum += f
# fsum == num is a Boolean expression
# It will evaluate to True if num is a perfect number
# And False otherwise
return fsum == num
print(is_perfect(int(input())))

3.

def distance(word_1,word_2):
if(len(word_1)!=len(word_2)):
return -1
else:
letters='abcdefghijklmnopqrstuvwxyz'
size=len(word_1)
dist=0
for i in range(size):
dist+=abs(letters.index(word_1[i])-letters.index(word_2[i]))
return dist

4.

def is_magic(mat):
# first get the dimension of the matrix
m = len(mat)
# the sum of the two diagonals
d1sum, d2sum = 0, 0
# (i, i) goes from top-left -> bottom-right
# (i, m - i - 1) goes from top-right -> bottom-left
# note that a single loop is enough; no nesting required
for i in range(m):
d1sum += mat[i][i]
d2sum += mat[i][m - i - 1]
# if the two diagonal sums are unequal, we can return NO
# unnecessary computation can be avoided
if not(d1sum == d2sum):
return 'NO'
# get row-sum and column-sum
for i in range(m):
rsum, csum = 0, 0
for j in range(m):
rsum += mat[i][j]
csum += mat[j][i]
if not(rsum == csum == d1sum):
return 'NO'
# if the code reaches this level
# then all requirements of a magic-square are satisfied
# so we can safely return YES
return 'YES'

5.

def transpose(mat):
mat2=[]
row=len(mat)
col=len(mat[0])
for i in range(col):
r=[]
for j in range(row):
r.append(mat[j][i])
mat2.append(r)
return mat2

Week 6 GRPA

1.

def get_toppers(scores_dataset, subject, gender):


# Filter the dataset based on gender
filtered_data = [data for data in scores_dataset if data['Gender'] == gender]

# Find the highest score for the given subject among the filtered data
highest_score = max(filtered_data, key=lambda x: x[subject])[subject]

# Get the names of students who have scored the highest in the subject
toppers = [data['Name'] for data in filtered_data if data[subject] ==
highest_score]

return toppers
2.

def freq_to_words(words):
word_freq_dict = {}

# Count the frequency of each word


for word in words:
word_freq_dict[word] = word_freq_dict.get(word, 0) + 1

# Create a dictionary with the frequency as key and a list of words as value
freq_to_words_dict = {}
for word, freq in word_freq_dict.items():
if freq not in freq_to_words_dict:
freq_to_words_dict[freq] = []
freq_to_words_dict[freq].append(word)

return freq_to_words_dict

3.

def rotate(mat):
# Get the number of rows and columns in the matrix
rows = len(mat)
cols = len(mat[0])

# Create a new matrix with swapped rows and columns


rotated_mat = [[0 for _ in range(rows)] for _ in range(cols)]

# Copy the elements from the original matrix to the rotated matrix
for i in range(rows):
for j in range(cols):
rotated_mat[j][rows - 1 - i] = mat[i][j]

return rotated_mat

4.

def two_level_sort(scores):
# Level-1 sorting: Sort the list of tuples based on marks in ascending order
for i in range(len(scores)):
for j in range(i + 1, len(scores)):
if scores[i][1] > scores[j][1]:
scores[i], scores[j] = scores[j], scores[i]

# Level-2 sorting: Sort the students with equal marks alphabetically by their
names
for i in range(len(scores) - 1):
j = i + 1
while j < len(scores) and scores[i][1] == scores[j][1]:
if scores[i][0] > scores[j][0]:
scores[i], scores[j] = scores[j], scores[i]
j += 1
return scores

You might also like