Skip to content

Commit 68be49c

Browse files
Add 1-sparse-recovery streaming algorithm (keon#762)
* feat:(first draft for the misra gries algorithm) keon#1 * feat:(Added examples and changed to correct name) keon#1 * feat:(Added init file for testing) keon#2 * test:(Added tests for misras_gries function) keon#2 * feat:(add 1-sparse recovery algorithm) keon#7 * Add finalized 1-sparse-recovery algorithm * Renamed sparse function name to work with import * Tests added for 1-sparse-recovery function * Tests added for 1-sparse-recovery function Co-authored-by: callmeGoldenboy <[email protected]>
1 parent 40f240a commit 68be49c

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ If you want to uninstall algorithms, it is as simple as:
305305
- [is_consecutive](algorithms/stack/is_consecutive.py)
306306
- [remove_min](algorithms/stack/remove_min.py)
307307
- [is_sorted](algorithms/stack/is_sorted.py)
308+
- [streaming](algorithms/streaming)
309+
- [1-sparse-recovery](algorithms/streaming/one_sparse_recovery.py)
308310
- [strings](algorithms/strings)
309311
- [fizzbuzz](algorithms/strings/fizzbuzz.py)
310312
- [delete_reoccurring](algorithms/strings/delete_reoccurring.py)

algorithms/streaming/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .one_sparse_recovery import *
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
""" Non-negative 1-sparse recovery problem. This algorithm assumes we have a non negative dynamic stream.
2+
Given a stream of tuples, where each tuple contains a number and a sign (+/-), it check if the stream is 1-sparse, meaning if the elements
3+
in the stream cancel eacheother out in such a way that ther is only a unique number at the end.
4+
5+
Examples:
6+
#1
7+
Input: [(4,'+'), (2,'+'),(2,'-'),(4,'+'),(3,'+'),(3,'-')],
8+
Output: 4
9+
Comment: Since 2 and 3 gets removed.
10+
#2
11+
Input: [(2,'+'),(2,'+'),(2,'+'),(2,'+'),(2,'+'),(2,'+'),(2,'+')]
12+
Output: 2
13+
Comment: No other numbers present
14+
#3
15+
Input: [(2,'+'),(2,'+'),(2,'+'),(2,'+'),(2,'+'),(2,'+'),(1,'+')]
16+
Output: None
17+
Comment: Not 1-sparse
18+
"""
19+
20+
def one_sparse(array):
21+
sum_signs = 0
22+
bitsum = [0]*32
23+
sum_values = 0
24+
for val,sign in array:
25+
if sign == "+":
26+
sum_signs += 1
27+
sum_values += val
28+
else:
29+
sum_signs -= 1
30+
sum_values -= val
31+
32+
_get_bit_sum(bitsum,val,sign)
33+
34+
if sum_signs > 0 and _check_every_number_in_bitsum(bitsum,sum_signs):
35+
return int(sum_values/sum_signs)
36+
else:
37+
return None
38+
39+
#Helper function to check that every entry in the list is either 0 or the same as the
40+
#sum of signs
41+
def _check_every_number_in_bitsum(bitsum,sum_signs):
42+
for val in bitsum:
43+
if val != 0 and val != sum_signs :
44+
return False
45+
return True
46+
47+
# Adds bit representation value to bitsum array
48+
def _get_bit_sum(bitsum,val,sign):
49+
i = 0
50+
if sign == "+":
51+
while(val):
52+
bitsum[i] += val & 1
53+
i +=1
54+
val >>=1
55+
else :
56+
while(val):
57+
bitsum[i] -= val & 1
58+
i +=1
59+
val >>=1
60+
61+

tests/test_streaming.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from algorithms.streaming import (
2+
one_sparse
3+
)
4+
import unittest
5+
6+
class TestOneSparse(unittest.TestCase):
7+
def test_one_sparse_correct(self):
8+
self.assertEqual(4,one_sparse([(4,'+'), (2,'+'),(2,'-'),(4,'+'),(3,'+'),(3,'-')]))
9+
self.assertEqual(2,one_sparse([(2,'+'),(2,'+'),(2,'+'),(2,'+'),(2,'+'),(2,'+'),(2,'+')]))
10+
11+
12+
def test_one_sparse_incorrect(self):
13+
self.assertEqual(None,one_sparse([(2,'+'),(2,'+'),(2,'+'),(2,'+'),(2,'+'),(2,'+'),(1,'+')])) #Two values remaining
14+
self.assertEqual(None,one_sparse([(2,'+'),(2,'+'),(2,'+'),(2,'+'),(2,'-'),(2,'-'),(2,'-'),(2,'-')])) # No values remaining
15+
self.assertEqual(None,one_sparse([(2,'+'),(2,'+'),(4,'+'),(4,'+')])) # Bitsum sum of sign is inccorect

0 commit comments

Comments
 (0)