diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-21 19:15:55 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-21 19:15:55 +0000 |
| commit | aed0ecf95f923b0c10ae9f79327dd2ef33f108f1 (patch) | |
| tree | 8c87f44a895d6f220259f9a6e4494149a02f80c4 /challenge-257/steven-wilson/python/ch-1.py | |
| parent | 558acc41d8b3b5e564b5ddd9f5b46572a236f61f (diff) | |
| parent | 3440c4cebac802435618d2c841974b13e779ffb2 (diff) | |
| download | perlweeklychallenge-club-aed0ecf95f923b0c10ae9f79327dd2ef33f108f1.tar.gz perlweeklychallenge-club-aed0ecf95f923b0c10ae9f79327dd2ef33f108f1.tar.bz2 perlweeklychallenge-club-aed0ecf95f923b0c10ae9f79327dd2ef33f108f1.zip | |
Merge pull request #9622 from oWnOIzRi/week257
update task 1 with more efficient counting algorithm
Diffstat (limited to 'challenge-257/steven-wilson/python/ch-1.py')
| -rw-r--r-- | challenge-257/steven-wilson/python/ch-1.py | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/challenge-257/steven-wilson/python/ch-1.py b/challenge-257/steven-wilson/python/ch-1.py index 9ceaaf849f..82296444ec 100644 --- a/challenge-257/steven-wilson/python/ch-1.py +++ b/challenge-257/steven-wilson/python/ch-1.py @@ -1,7 +1,5 @@ #!/usr/bin/env python3 -from collections import Counter - def smaller_than_current(*integers): ''' Given an array of integers, find out how many integers are smaller than @@ -14,13 +12,24 @@ def smaller_than_current(*integers): (0, 1) >>> smaller_than_current(9, 4, 9, 2) (2, 1, 2, 0) + >>> smaller_than_current(1, 0, 0, 0, 1, 2) + (3, 0, 0, 0, 3, 5) + >>> smaller_than_current() + () ''' - integer_counter = Counter(integers) - results = [] + # Counting algorithm + if not integers: + return () + + max_i = max(integers) + count = [0] * (max_i + 1) for i in integers: - results.append(sum(count for key, count in integer_counter.items() - if key < i)) - return tuple(results) + count[i] += 1 + + for pos in range(1, max_i): + count[pos] += count[pos-1] + + return tuple(count[i - 1] if i != 0 else 0 for i in integers) if __name__ == "__main__": |
