-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from NoamNol/v0.2.0
V0.2.0: Restyle and add to_number function
- Loading branch information
Showing
7 changed files
with
187 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
from .gematria import Gematria | ||
"""Convert number to Hebrew or Hebrew to number""" | ||
|
||
__version__ = "0.2.0" | ||
|
||
from .gematria import to_hebrew, to_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,72 @@ | ||
class Gematria: | ||
_HEBREW_LETTERS = [ | ||
("א", 1), | ||
("ב", 2), | ||
("ג", 3), | ||
("ד", 4), | ||
("ה", 5), | ||
("ו", 6), | ||
("ז", 7), | ||
("ח", 8), | ||
("ט", 9), | ||
("י", 10), | ||
("כ", 20), | ||
("ל", 30), | ||
("מ", 40), | ||
("נ", 50), | ||
("ס", 60), | ||
("ע", 70), | ||
("פ", 80), | ||
("צ", 90), | ||
("ק", 100), | ||
("ר", 200), | ||
("ש", 300), | ||
("ת", 400) | ||
] | ||
_REVERSED_HEBREW_LETTERS = _HEBREW_LETTERS[::-1] | ||
|
||
def number_to_hebrew(self, number: int) -> str: | ||
""" | ||
Convert number to Hebrew letter(s) | ||
Examples: | ||
2 = "ב" | ||
47 = "מז" | ||
""" | ||
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._REVERSED_HEBREW_LETTERS: | ||
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 | ||
from collections import OrderedDict | ||
|
||
|
||
HEBREW_LETTERS: OrderedDict = OrderedDict([ | ||
("א", 1), | ||
("ב", 2), | ||
("ג", 3), | ||
("ד", 4), | ||
("ה", 5), | ||
("ו", 6), | ||
("ז", 7), | ||
("ח", 8), | ||
("ט", 9), | ||
("י", 10), | ||
("כ", 20), | ||
("ל", 30), | ||
("מ", 40), | ||
("נ", 50), | ||
("ס", 60), | ||
("ע", 70), | ||
("פ", 80), | ||
("צ", 90), | ||
("ק", 100), | ||
("ר", 200), | ||
("ש", 300), | ||
("ת", 400) | ||
]) | ||
|
||
|
||
def to_hebrew(number: int) -> str: | ||
""" | ||
Convert number to Hebrew letter(s). | ||
Examples: | ||
to_hebrew(2) # => "ב" | ||
to_hebrew(47) # => "מז" | ||
""" | ||
if number <= 0: | ||
raise Exception("Number must be bigger than zero") | ||
if number >= 1000: | ||
raise Exception("Number bigger than 999 is not yet supported") | ||
|
||
remainder = number | ||
heb_sum = "" | ||
while remainder > 0: | ||
for heb_letter, val in reversed(HEBREW_LETTERS.items()): | ||
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 | ||
|
||
|
||
def to_number(hebrew: str) -> int: | ||
""" | ||
Convert Hebrew letters to number. Ignore Non-Hebrew letters. | ||
Examples: | ||
to_number("ב") # => 2 | ||
to_number("מז") # => 47 | ||
""" | ||
sum = 0 | ||
for letter in hebrew: | ||
sum += HEBREW_LETTERS.get(letter, 0) | ||
return sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import setuptools | ||
import gematriapy | ||
|
||
with open("README.md", "r", encoding="utf-8") as fh: | ||
long_description = fh.read() | ||
|
||
setuptools.setup( | ||
name="gematriapy", | ||
version="0.1.0", | ||
version=gematriapy.__version__, | ||
author="Noam Nol", | ||
author_email="[email protected]", | ||
description="Convert numbers to Hebrew letters", | ||
description=gematriapy.__doc__, | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/NoamNol/gematriapy", | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import unittest | ||
import gematriapy | ||
|
||
|
||
class TestGematriapy_ToHebrew(unittest.TestCase): | ||
def test_simple_num(self): | ||
self.assertEqual(gematriapy.to_hebrew(3), "ג") | ||
|
||
def test_15_num(self): | ||
self.assertEqual(gematriapy.to_hebrew(15), "טו") | ||
|
||
def test_16_num(self): | ||
self.assertEqual(gematriapy.to_hebrew(16), "טז") | ||
|
||
def test_random_tens_num_v1(self): | ||
self.assertEqual(gematriapy.to_hebrew(24), "כד") | ||
|
||
def test_random_tens_num_v2(self): | ||
self.assertEqual(gematriapy.to_hebrew(77), "עז") | ||
|
||
def test_random_hundreds_num_v1(self): | ||
self.assertEqual(gematriapy.to_hebrew(250), "רנ") | ||
|
||
def test_random_hundreds_num_v2(self): | ||
self.assertEqual(gematriapy.to_hebrew(499), "תצט") | ||
|
||
def test_bigger_than_800_num(self): | ||
self.assertEqual(gematriapy.to_hebrew(822), "תתכב") | ||
|
||
def test_hundreds_num_that_ends_with_15(self): | ||
self.assertEqual(gematriapy.to_hebrew(515), "תקטו") | ||
|
||
def test_hundreds_num_that_ends_with_16(self): | ||
self.assertEqual(gematriapy.to_hebrew(516), "תקטז") | ||
|
||
def test_zero(self): | ||
with self.assertRaises(Exception): | ||
gematriapy.to_hebrew(0) | ||
|
||
def test_negative(self): | ||
with self.assertRaises(Exception): | ||
gematriapy.to_hebrew(-1) | ||
|
||
def test_too_big(self): | ||
with self.assertRaises(Exception): | ||
gematriapy.to_hebrew(1000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import unittest | ||
import gematriapy | ||
|
||
|
||
class TestGematriapy_ToNumber(unittest.TestCase): | ||
def test_single_letter_to_3(self): | ||
self.assertEqual(gematriapy.to_number("ג"), 3) | ||
|
||
def test_single_letter_to_9(self): | ||
self.assertEqual(gematriapy.to_number("ט"), 9) | ||
|
||
def test_long_hebrew_v1(self): | ||
self.assertEqual(gematriapy.to_number("קכו"), 126) | ||
|
||
def test_long_hebrew_v2(self): | ||
self.assertEqual(gematriapy.to_number("רטו"), 215) | ||
|
||
def test_illegal_hebrew_number_v1(self): | ||
""" | ||
In Hebrew, letters representing a numerical value should have an order: | ||
big numbers come before small numbers. | ||
So "ת" should come before "י". | ||
to_number should not care about that and just return the 'gematriapy' | ||
value - the sum of the numerical value of the letters. | ||
""" | ||
self.assertEqual(gematriapy.to_number("יתג"), 413) | ||
|
||
def test_illegal_hebrew_number_v2(self): | ||
self.assertEqual(gematriapy.to_number("אבא"), 4) | ||
|
||
def test_empty(self): | ||
self.assertEqual(gematriapy.to_number(""), 0) | ||
|
||
def test_non_hebrew(self): | ||
self.assertEqual(gematriapy.to_number("abc"), 0) | ||
|
||
def test_hebrew_and_non_hebrew(self): | ||
self.assertEqual(gematriapy.to_number("aאbהc"), 6) |