Skip to content

Commit

Permalink
remove py2 support, xrange
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyod committed Sep 5, 2024
1 parent 51a4778 commit d2b0515
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions fenwick/fenwick.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import os
import sys

Expand All @@ -12,18 +10,9 @@
# ************************************************************

_major_version = sys.version_info.major
if _major_version < 2:
if _major_version < 3:
raise RuntimeError("Unsupported version of Python: {}".format(_major_version))


def _range(*args, **kwargs):
if _major_version == 2:
import __builtin__
return __builtin__.xrange(*args, **kwargs)
else:
import builtins
return builtins.range(*args, **kwargs)

# ************************************************************
# * Core
# ************************************************************
Expand Down Expand Up @@ -72,7 +61,7 @@ def __getitem__(self, idx):
def frequencies(self):
"""Retrieves all frequencies in O(n)."""
_frequencies = [0] * self._n
for idx in _range(1, self._n + 1):
for idx in range(1, self._n + 1):
_frequencies[idx - 1] += self._v[idx - 1]
parent_idx = idx + (idx & -idx)
if parent_idx <= self._n:
Expand All @@ -98,7 +87,7 @@ def init(self, frequencies):
if len(frequencies) != self._n:
raise ValueError("Length of frequencies must match length of FenwickTree.")
self._v = list(frequencies)
for idx in _range(1, self._n + 1):
for idx in range(1, self._n + 1):
parent_idx = idx + (idx & -idx) # parent in update tree
if parent_idx <= self._n:
self._v[parent_idx - 1] += self._v[idx - 1]
Expand Down

0 comments on commit d2b0515

Please sign in to comment.