Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
424 views

Python Week 6 GRPA

The document contains code definitions for 4 functions: 1. get_toppers returns the names of students with the highest score in a given subject and gender. 2. freq_to_words counts the frequency of each word in a list and returns a dictionary with frequencies as keys and list of words as values. 3. rotate takes a matrix and returns a new matrix with rows and columns swapped, effectively rotating the original matrix by 90 degrees. 4. two_level_sort performs a two level sorting of a list of tuples based on marks - first in ascending order of marks, then alphabetically by name for tuples with equal marks.

Uploaded by

venkymandyam
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
424 views

Python Week 6 GRPA

The document contains code definitions for 4 functions: 1. get_toppers returns the names of students with the highest score in a given subject and gender. 2. freq_to_words counts the frequency of each word in a list and returns a dictionary with frequencies as keys and list of words as values. 3. rotate takes a matrix and returns a new matrix with rows and columns swapped, effectively rotating the original matrix by 90 degrees. 4. two_level_sort performs a two level sorting of a list of tuples based on marks - first in ascending order of marks, then alphabetically by name for tuples with equal marks.

Uploaded by

venkymandyam
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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