Skip to content

Commit

Permalink
Add to_number function
Browse files Browse the repository at this point in the history
  • Loading branch information
NoamNol committed Jul 5, 2021
1 parent 4579948 commit c30a2c0
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 46 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
![CI](https://github.com/NoamNol/gematriapy/workflows/CI/badge.svg)
![PyPI Deploy](https://github.com/NoamNol/gematriapy/workflows/PyPI%20Deploy/badge.svg)

Convert numbers to Hebrew letters
Convert number to Hebrew or Hebrew to number

## Install
```bash
pip install gematriapy
```

## Usage
**`to_hebrew`**:
```python
import gematriapy
gematriapy.to_hebrew(3) # => "ג"
Expand All @@ -24,3 +25,19 @@ gematriapy.to_hebrew(822) # => "תתכב"
```

> **NOTE**: Numbers greater than 999 are not supported yet.
<br/>

**`to_number`**:
```python
import gematriapy
gematriapy.to_number("ג") # => 3
```

```python
gematriapy.to_number("טו") # => 15
```

```python
gematriapy.to_number("אבא") # => 4
```
4 changes: 2 additions & 2 deletions gematriapy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Convert numbers to Hebrew letters"""
"""Convert number to Hebrew or Hebrew to number"""

__version__ = "0.2.0"

from .gematria import to_hebrew
from .gematria import to_hebrew, to_number
100 changes: 57 additions & 43 deletions gematriapy/gematria.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,71 @@


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)
("א", 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:
Examples:
to_hebrew(2) # => "ב"
to_hebrew(47) # => "מז"
"""
if number <= 0:
raise Exception("Number must be bigger than zero")
if number >= 1000:
"""
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:
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
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
39 changes: 39 additions & 0 deletions tests/test_to_number.py
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)

0 comments on commit c30a2c0

Please sign in to comment.