Skip to content

Commit

Permalink
Modify nmr/nmr.py, test/test_nmr.py, test/test_sign.py
Browse files Browse the repository at this point in the history
  • Loading branch information
rec committed Dec 23, 2024
1 parent 7f5aa80 commit 6e5d1be
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions nmr/nmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,11 @@ def __init__(
self._count_words = count_words.CountWords(self.n).count
self.inverse = {w: i for i, w in enumerate(self.words)}

def _encode_to_name(self, num: int) -> Sequence[str]:
if num < 0:
raise ValueError("Only accepts non-negative numbers")
return [self.words[i] for i in self._to_digits(num)]

def decode_from_name(self, name: Sequence[str]) -> int:
name = list(name)
_check_dupes(name)
indexes = [self.inverse[w] for w in reversed(name)]
return self._from_digits(list(self._redupe(indexes))[::-1])

def is_name(self, s: Sequence[str]) -> bool:
return all(i in self.inverse for i in s)

def name_to_type(self, name: Sequence[str]) -> str:
index = self.decode_from_name(name)
index = self._decode_from_name(name)
return types.index_to_type(index)

def type_to_name(self, s: str) -> Sequence[str]:
Expand All @@ -79,6 +68,17 @@ def type_to_name(self, s: str) -> Sequence[str]:
def n(self) -> int:
return len(self.words)

def _encode_to_name(self, num: int) -> Sequence[str]:
if num < 0:
raise ValueError("Only accepts non-negative numbers")
return [self.words[i] for i in self._to_digits(num)]

def _decode_from_name(self, name: Sequence[str]) -> int:
name = list(name)
_check_dupes(name)
indexes = [self.inverse[w] for w in reversed(name)]
return self._from_digits(list(self._redupe(indexes))[::-1])

def _to_digits(self, num: int) -> list[int]:
it = (i + 1 for i in range(self.n) if self._count_words(i + 1) > num)
if (word_count := next(it, None)) is None:
Expand Down
2 changes: 1 addition & 1 deletion test/test_nmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ def test_stability(number, words):
if actual_words != words:
print(actual_words)
assert actual_words == words
assert nmr.decode_from_name(words) == number
assert nmr._decode_from_name(words) == number
2 changes: 1 addition & 1 deletion test/test_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_all_unsigned(n):
for i in range(m):
words = N._encode_to_name(i)
assert len(words) == len(set(words))
assert N.decode_from_name(words) == i, str(words)
assert N._decode_from_name(words) == i, str(words)

err = "Only accepts non-negative numbers"
with pytest.raises(ValueError, match=err):
Expand Down

0 comments on commit 6e5d1be

Please sign in to comment.