Skip to content

Commit

Permalink
chore: menambahkan angka segitiga (#52)
Browse files Browse the repository at this point in the history
Signed-off-by: arfy slowy <[email protected]>
  • Loading branch information
slowy07 authored Feb 6, 2024
1 parent 0c13905 commit 37a2b54
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
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

0 comments on commit 37a2b54

Please sign in to comment.