המרת מספר לגימטריה
לאחרונה הייתי צריך להמיר ערך מספרי לגימטריה העברית שלו.דוגמאות:
2 - ב
18 - יח
47 - מז
283 - רפג
411 - תיא
הנה המחלקה והבדיקות שיצרתי:
עריכה: עכשיו המחלקה זמינה להורדה מהירה עם pip. למידע נוסף.
# gematria_test.py
import unittest
from unittest.case import expectedFailure
def main():
gematria = Gematria()
for number in range(1, 1000):
heb_value = gematria.number_to_hebrew(number)
print(heb_value)
class Gematria:
_heb_letters = [
("א", 1),
("ב", 2),
("ג", 3),
("ד", 4),
("ה", 5),
("ו", 6),
("ז", 7),
("ח", 8),
("ט", 9),
("י", 10),
("כ", 20),
("ל", 30),
("מ", 40),
("נ", 50),
("ס", 60),
("ע", 70),
("פ", 80),
("צ", 90),
("ק", 100),
("ר", 200),
("ש", 300),
("ת", 400)
]
def number_to_hebrew(self, number):
if number <= 0:
raise Exception("Number must be bigger than zero")
if number >= 1000:
raise Exception("Number bigger than 999 is not supported yet")
remainder = number
heb_sum = ""
while remainder > 0:
for heb_letter, val in self._heb_letters[::-1]:
if (val <= remainder):
if remainder == 15:
heb_sum += "טו"
remainder = 0
elif remainder == 16:
heb_sum += "טז"
remainder = 0
else:
heb_sum += heb_letter
remainder -= val
break
return heb_sum
class Test_Gematria(unittest.TestCase):
gematria = Gematria()
def test_simple_num(self):
self.assertEqual(self.gematria.number_to_hebrew(3), "ג")
def test_15_num(self):
self.assertEqual(self.gematria.number_to_hebrew(15), "טו")
def test_16_num(self):
self.assertEqual(self.gematria.number_to_hebrew(16), "טז")
def test_random_tens_num_1(self):
self.assertEqual(self.gematria.number_to_hebrew(24), "כד")
def test_random_tens_num_2(self):
self.assertEqual(self.gematria.number_to_hebrew(77), "עז")
def test_random_hundreds_num_1(self):
self.assertEqual(self.gematria.number_to_hebrew(250), "רנ")
def test_random_hundreds_num_2(self):
self.assertEqual(self.gematria.number_to_hebrew(499), "תצט")
def test_bigger_than_800_num(self):
self.assertEqual(self.gematria.number_to_hebrew(822), "תתכב")
def test_hundreds_num_that_ends_with_15(self):
self.assertEqual(self.gematria.number_to_hebrew(515), "תקטו")
def test_hundreds_num_that_ends_with_16(self):
self.assertEqual(self.gematria.number_to_hebrew(516), "תקטז")
def test_zero(self):
with self.assertRaises(Exception):
result = self.gematria.number_to_hebrew(0)
def test_minus(self):
with self.assertRaises(Exception):
result = self.gematria.number_to_hebrew(-1)
def test_too_big(self):
with self.assertRaises(Exception):
result = self.gematria.number_to_hebrew(1000)
if __name__== "__main__":
main()
# unittest.main()
פשוט וקל.
גאון
השבמחק