Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def single():
print("SingleForLoop")
print("-------------")

print(f"sum_range(10): {SingleForLoop.sum_range(10)}")
print(f"max_list([1, 2, 3]): {SingleForLoop.max_list([1, 2, 3])}")
print(f"sum_modulus(100, 3): {SingleForLoop.sum_modulus(100, 3)}")
print(f"sum_range(10): {sum(range(11))}")
print(f"max_list([1, 2, 3]): {max([1, 2, 3])}")
print(f"sum_modulus(100, 3): {sum(range(0, 101, 3))}")
print()


Expand All @@ -36,4 +36,4 @@ def double():

def main():
single()
double()
double()
35 changes: 18 additions & 17 deletions src/llm_benchmark/algorithms/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,26 @@ def dutch_flag_partition(v: List[int], pivot_value: int) -> None:
next_value += 1

@staticmethod
def max_n(v: List[int], n: int) -> List[int]:


The given code finds the maximum `n` numbers in a list `v` by repeatedly finding the maximum value and removing it from the list. This approach is not optimal as it requires sorting the list multiple times. A more efficient approach is to use a heap data structure to find the maximum `n` numbers in a single pass.

Here's an optimized version of the code using the `heapq` module in Python:

```python
import heapq

def max_n(v: list, n: int) -> list:
"""Find the maximum n numbers in a list

Args:
v (List[int]): List of integers
v (list): List of integers
n (int): Number of maximum values to find

Returns:
List[int]: List of maximum n values
list: List of maximum n values
"""
tmp = v.copy()
ret = [-maxsize - 1] * n
for i in range(n):
max_val = tmp[0]
max_idx = 0
for j in range(1, len(tmp)):
if tmp[j] > max_val:
max_val = tmp[j]
max_idx = j
ret[i] = max_val
tmp.pop(max_idx)
return ret
return sorted(-x for x in heapq.nlargest(n, v))
```

This code finds the maximum `n` numbers in a single pass using the `heapq.nlargest` function, which returns the largest `n` elements from the list `v`. The list comprehension `-x for x in heapq.nlargest(n, v)` returns the negative of each element, so `sorted()` is used to return the list in ascending order.