From 0528c320919a20bfcfa8de4703d738ac5c4672f4 Mon Sep 17 00:00:00 2001 From: slowy07 Date: Sun, 20 Apr 2025 18:00:11 +0700 Subject: [PATCH 1/3] chore: menambahkan fungsi gamma fungsi gamma adalah generalisasi dari fungsi faktorial ke bilangan ril dan bilangan kompleks Signed-off-by: slowy07 --- math/fungsi_gamma.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 math/fungsi_gamma.py diff --git a/math/fungsi_gamma.py b/math/fungsi_gamma.py new file mode 100644 index 00000000..51b10a67 --- /dev/null +++ b/math/fungsi_gamma.py @@ -0,0 +1,63 @@ +import math + + +def fungsi_gamma(nilai: float) -> float: + """ + Hitung nilai fungsi gamma untuk `nilai` + fungsi ini hanya mengdukung bilangan bulat seperti + (1, 2, 3, 4 ... n) atau nilai pecahan, berkoma + (0.1, 2.5, 3.4, 3.3 ... n) + + informasi relevan tentang fungsi gamma: + - https://en.wikipedia.org/wiki/Gamma_function + - https://mathworld.wolfram.com/GammaFunction.html + + Parameter: + nilai(float): nilai yang diberikan + + Return: + (float): hasil kalkulasi fungsi gamma + + Contoh: + >>> fungsi_gamma(0.5) + 1.7724538509055159 + >>> hitung_gamma(1) + 1.0 + >>> hitung_gamma(3.5) + 3.3233509704478426 + """ + # memastikan nilai tidak kurang atau sama dengan 0 + # atau negatif + if nilai <= 0: + raise ValueError("nilai harus lebih besar dari nol") + + # membuat batasan maks dari nilai yang diberikan + # fungsi logika ini hanya bersifat opsional + if nilai > 175.5: + raise OverflowError("nilai rentang terlalu besar") + + # memastikan nilai adalh bilangan bulat atau pecahan + if nilai - int(nilai) not in (0, 0.5): + raise NotImplementedError("nilai harus bilangan bulat atau berkoma") + + # buat basis rekursi dari gamma gamma(0.5) = sqrt(pi) + if nilai == 0.5: + return math.sqrt(math.pi) + + # buat basis rekursi jika gamma(1) = 1 + if nilai == 1: + return 1.0 + + return (nilai - 1) * fungsi_gamma(nilai - 1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + # contoh pemanggilan + # print(fungsi_gamma(1)) + + # nilai: float = 2.5 + # print(f"hasil fungsi_gamma({nilai}) adalah: {fungsi_gamma(nilai)}") From 357ce1b7fdf0dcfea4b5f3e94bedb2d6ef0131e3 Mon Sep 17 00:00:00 2001 From: slowy07 Date: Thu, 8 May 2025 01:46:42 +0700 Subject: [PATCH 2/3] chore: menambahkan algoritma gnome sorting Signed-off-by: slowy07 --- algorithm/sorting/gnome_sorting.py | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 algorithm/sorting/gnome_sorting.py diff --git a/algorithm/sorting/gnome_sorting.py b/algorithm/sorting/gnome_sorting.py new file mode 100644 index 00000000..865667b2 --- /dev/null +++ b/algorithm/sorting/gnome_sorting.py @@ -0,0 +1,38 @@ +def gnome_sorting(daftar: list) -> list: + """ + Implementasi dari algoritma gnome sorting + + fungsi yang menerima daftar yang dapat nantinya diubah + dan diurut dengan elemen heterogen yang bisa dibandingkan, + lalu mengembalikan list tersebut dalam urutan menaik + + Parameter: + daftar (list): data yang ingin diberikan + + Return: + (list): hasil sorting gnome + + Contoh: + >>> gnome_sorting([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + """ + if len(daftar) <= 1: + return daftar + + i = 1 + while i < len(daftar): + if daftar[i - 1] <= daftar[i]: + i += 1 + else: + # tukar elemen jika tidak dalam urutan yang bener + daftar[i - 1], daftar[i] = daftar[i], daftar[i - 1] + i -= 1 + if i == 0: + i = 1 + return daftar + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) From ff0a7fbe2408e8370fab884a8ace1090cb04902d Mon Sep 17 00:00:00 2001 From: github-actions Date: Wed, 7 May 2025 18:47:28 +0000 Subject: [PATCH 3/3] docs: update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 81c386f8..255f3636 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -149,6 +149,7 @@ * [Bubble Sort Ascii](https://github.com/bellshade/Python/blob/main/algorithm/sorting/bubble_sort_ascii.py) * [Bucket Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/bucket_sort.py) * [Circle Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/circle_sort.py) + * [Gnome Sorting](https://github.com/bellshade/Python/blob/main/algorithm/sorting/gnome_sorting.py) * [Merge Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/merge_sort.py) * [Pop Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/pop_sort.py) * [Quick Sorting](https://github.com/bellshade/Python/blob/main/algorithm/sorting/quick_sorting.py)