Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: menambahkan angka segitiga #52

Merged
merged 1 commit into from
Feb 6, 2024
Merged
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
19 changes: 19 additions & 0 deletions OpenSeries/bilangan_istimewa.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,22 @@ def angka_pronic(angka: int) -> Union[str, error.ErrorTipeData]:
if angka == angka_pangkat * (angka_pangkat + 1)
else bukan.capitalize()
)


def angka_segitiga(angka: int) -> Union[int, error.ErrorTipeData, error.Error]:
"""
bilangan segitiga adalah bilangan yang dapat disusun dalam bentuk segitiga sama sisi

Parameter:
angka (int): angka yang ingin dimasukkan

Return:
int: angka segitiga di posisi yang ditentukan
error.Error: jika angka negatif
error.ErrorTipeData: jika tipe data salah
"""
if not isinstance(angka, int):
return error.ErrorTipeData(["int"])
if angka < 0:
return error.Error("angka tidak boleh negatif")
return angka * (angka + 1) // 2
4 changes: 4 additions & 0 deletions example/bilangan/segitiga.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import OpenSeries.bilangan_istimewa as bilangan

angka = 3
print(f"angka segitiga dari {angka} adalah {bilangan.angka_segitiga(angka)}")
16 changes: 16 additions & 0 deletions testing/bilangan_istimewa_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,19 @@ def test_angka_pronic_return_tipe_data(self):
def test_angka_pronic_negatif(self):
hasil = bilangan.angka_pronic(-30)
self.assertIsInstance(hasil, str)


class TestAngkaSegitiga(unittest.TestCase):
def test_angka_segitiga(self):
self.assertEqual(bilangan.angka_segitiga(0), 0)
self.assertEqual(bilangan.angka_segitiga(3), 6)

def test_angka_negatif(self):
hasil = bilangan.angka_segitiga(-1)
with self.assertRaises(error.Error):
raise hasil

def test_beda_tipe_data(self):
hasil = bilangan.angka_segitiga("12")
with self.assertRaises(error.ErrorTipeData):
raise hasil
2 changes: 2 additions & 0 deletions testing/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
TestAngkaArmstrong,
TestAngkaAutomorphic,
TestAngkaPronic,
TestAngkaSegitiga,
)

from testing.statistika_test import TestFungsiEntropy, TestFungiStandardDeviasi
Expand Down Expand Up @@ -74,6 +75,7 @@
TestAngkaArmstrong,
TestAngkaAutomorphic,
TestAngkaPronic,
TestAngkaSegitiga,
]

all_tests = unittest.TestSuite()
Expand Down
Loading