Python Week 6 GRPA
Python Week 6 GRPA
1.
# 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 = {}
# 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])
# 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