question - Copy
question - Copy
numbers = [12, 5, 8, 9, 7]
sorted_numbers = sorted(numbers, key=count_divisors)
print(sorted_numbers)
4. Sort in Reverse Alphabetical Order
Problem: Sort a list of strings in reverse alphabetical order.
Example:
python
Copy
Edit
words = ['apple', 'banana', 'cherry', 'date']
sorted_words = sorted(words, reverse=True)
print(sorted_words)
5. Sort Using Lambda Function
Problem: Sort a list of tuples by the second element.
Example:
python
Copy
Edit
tuples = [(1, 3), (2, 2), (3, 1)]
sorted_tuples = sorted(tuples, key=lambda x: x[1])
print(sorted_tuples)
6. Sort List of Dictionaries
Problem: Sort a list of dictionaries by a specific key.
Example:
python
Copy
Edit
students = [
{'name': 'Alice', 'score': 90},
{'name': 'Bob', 'score': 82},
{'name': 'Charlie', 'score': 88}
]
sorted_students = sorted(students, key=lambda x: x['score'], reverse=True)
print(sorted_students)
7. Sort List of Strings by Length
Problem: Sort a list of strings by their length.
Example:
python
Copy
Edit
words = ['banana', 'apple', 'kiwi', 'cherry', 'grape']
sorted_words = sorted(words, key=len)
print(sorted_words)
8. Bubble Sort Implementation
Problem: Implement the Bubble Sort algorithm manually.
Example:
python
Copy
Edit
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
merge_sort(left_half)
merge_sort(right_half)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
return arr
numbers = [10, 7, 8, 9, 1, 5]
sorted_numbers = quick_sort(numbers)
print(sorted_numbers)
11. Sort a List of Lists
Problem: Sort a list of lists by the sum of each inner list.
Example:
python
Copy
Edit
lists = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
sorted_lists = sorted(lists, key=lambda x: sum(x))
print(sorted_lists)
12. Top N Elements
Problem: Find the N largest or smallest numbers in a list.
Example:
python
Copy
Edit
import heapq
for i in range(n):
index = arr[i] // exp
count[index % 10] += 1
i = n - 1
while i >= 0:
index = arr[i] // exp
output[count[index % 10] - 1] = arr[i]
count[index % 10] -= 1
i -= 1
for i in range(n):
arr[i] = output[i]
def radix_sort(arr):
max_element = max(arr)
exp = 1
while max_element // exp > 0:
counting_sort(arr, exp)
exp *= 10
return arr
2.Sort by Absolute Value Problem: How do you sort a list of numbers by their
absolute value?
3.Sort in Custom Order Problem: How do you sort a list of numbers based on a custom
sorting function like the number of divisors?
4.Sort in Reverse Alphabetical Order: How do you sort a list of strings in reverse
alphabetical order?
5.Sort Using Lambda Function: How do you sort a list of tuples by the second
element using a lambda function?
7.Sort List of Strings by Length: How do you sort a list of strings by their
length?
8.Bubble Sort Implementation: How do you implement the Bubble Sort algorithm
manually?
9.Merge Sort: How do you implement the Merge Sort algorithm manually?
10.Quick Sort: How do you implement the Quick Sort algorithm manually?
11.Sort a List of Lists: How do you sort a list of lists by the sum of each inner
list?
12.Top N Elements: How do you find the N largest or smallest numbers in a list?
13.Sort Based on Multiple Criteria: How do you sort a list of tuples first by the
first element, then by the second element?
15.Sort with Key Function (Custom Sorting Criteria): How do you sort a list of
strings based on the number of vowels in each string?