Skip to content

Commit c70a7e4

Browse files
committed
added bubble sort
1 parent fdd421e commit c70a7e4

8 files changed

+66
-5
lines changed

codes/project_euler_721.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,44 @@
1717
#%%
1818
#mpm.mp.dps = 5; mpm.mp.pretty = False
1919
#%%
20-
def f(a,n):
20+
def f(a, n):
21+
"""
22+
Calculates a mathematical expression involving square roots and exponents.
23+
24+
Args:
25+
a (mpf): The base number for calculations.
26+
n (int): The exponent to be applied.
27+
28+
Returns:
29+
mpf: The floor of the result of (ceil(sqrt(a)) + sqrt(a))^n.
30+
31+
Note:
32+
This function uses the mpmath library (mpm) for high-precision arithmetic.
33+
"""
2134
return mpm.floor(
22-
mpm.fadd( mpm.ceil(mpm.sqrt(a)), mpm.sqrt(a))**n
35+
mpm.fadd(mpm.ceil(mpm.sqrt(a)), mpm.sqrt(a))**n
2336
)
2437

2538
def g(n):
39+
"""
40+
Computes the sum of f(a, a^2) for a range of values.
41+
42+
Args:
43+
n (int): The upper limit of the range (inclusive).
44+
45+
Returns:
46+
mpf: The sum of f(a, a^2) for a from 1 to n.
47+
48+
Note:
49+
This function uses the mpmath library (mpm) for high-precision arithmetic
50+
and range generation.
51+
"""
2652
return mpm.fsum(
27-
[f(a, a**2) for a in mpm.arange(1, (n+ 1))]
28-
)
53+
[f(a, a**2) for a in mpm.arange(1, (n + 1))]
54+
)
55+
2956
#%%
30-
__init__ = main():
57+
if __name__ == '__main__':
3158
print('f(5, 2) = '.format(f(5, 2)))
3259
print('f(5, 5) = '.format(f(5, 5)))
3360
print('***************************')

codes/sorting_algo.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def bubble_sort(my_list):
2+
"""
3+
Sorts a list in ascending order using the bubble sort algorithm.
4+
5+
Args:
6+
my_list (list): The input list to be sorted.
7+
8+
Returns:
9+
list: The sorted list in ascending order.
10+
11+
Time complexity: O(n^2), where n is the number of elements in the list.
12+
Space complexity: O(1), as sorting is done in-place.
13+
14+
Note:
15+
- If the input list is empty, an empty list is returned.
16+
- This implementation modifies the original list in-place.
17+
18+
Example:
19+
>>> bubble_sort([64, 34, 25, 12, 22, 11, 90])
20+
[11, 12, 22, 25, 34, 64, 90]
21+
"""
22+
if not my_list:
23+
return []
24+
n = len(my_list)
25+
for i in range(n):
26+
for j in range(n - i - 1):
27+
if(my_list[j] > my_list[j + 1]):
28+
my_list[j], my_list[j + 1] = my_list[j + 1], my_list[j]
29+
return my_list
30+
31+
32+
if __name__ == '__main__':
33+
num = bubble_sort([64, 34, 25, 12, 22, 11, 90])
34+
print(num)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)