@@ -26,49 +26,58 @@ def findElementWhichOccursXTimesWhileOthersOccurYTimes(arr, x, y):
26
26
if not maxVal :
27
27
return None
28
28
29
- p = 1
29
+ p = 1
30
30
bitCount = 0
31
31
while p <= maxVal :
32
32
bitCount += 1
33
- p <<= 1
34
-
33
+ p <<= 1
34
+
35
35
bits = [0 ] * bitCount
36
36
for elem in arr :
37
37
v = 1
38
38
for i in range (len (bits )):
39
39
if v & elem :
40
- bits [i ]+= 1
41
- v <<= 1
40
+ bits [i ] += 1
41
+ v <<= 1
42
42
43
- bits = map (lambda a : a % x , bits )
43
+ bits = list ( map (lambda a : a % x , bits ) )
44
44
missingVal = 0
45
45
for i in range (len (bits )):
46
46
if bits [i ] != expectedBitCount and bits [i ] != 0 :
47
47
return None
48
48
if bits [i ]:
49
- missingVal += (2 ** i )
49
+ missingVal += (2 ** i )
50
50
return missingVal
51
51
52
52
53
53
class CodeTest (unittest .TestCase ):
54
54
def testEmpty (self ):
55
- self .assertIsNone (findElementWhichOccursXTimesWhileOthersOccurYTimes ([], 5 , 2 ))
56
-
55
+ self .assertIsNone (
56
+ findElementWhichOccursXTimesWhileOthersOccurYTimes ([], 5 , 2 ))
57
+
57
58
def testXIsSameAsY (self ):
58
- self .assertIsNone (findElementWhichOccursXTimesWhileOthersOccurYTimes ([1 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,6 ,6 ,6 ,6 ,6 ,5 ,5 ,5 ,5 ,5 ], 5 , 5 ))
59
+ self .assertIsNone (findElementWhichOccursXTimesWhileOthersOccurYTimes (
60
+ [1 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 6 , 6 , 6 , 6 , 6 , 5 , 5 , 5 , 5 , 5 ], 5 , 5 ))
59
61
60
62
def testIncorrectLengthWhenXGreaterThanY (self ):
61
- self .assertIsNone (findElementWhichOccursXTimesWhileOthersOccurYTimes ([1 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,6 ,6 ,6 ,6 ,6 ,5 ,5 ,5 ,5 ,5 , 8 , 8 , 8 ], 5 , 2 ))
63
+ self .assertIsNone (findElementWhichOccursXTimesWhileOthersOccurYTimes (
64
+ [1 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 6 , 6 , 6 , 6 , 6 , 5 , 5 , 5 , 5 , 5 , 8 , 8 , 8 ], 5 , 2 ))
62
65
63
66
def testIncorrectLengthWhenXLessThanY (self ):
64
- self .assertIsNone (findElementWhichOccursXTimesWhileOthersOccurYTimes ([1 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,6 ,6 ,6 ,6 ,6 ,5 ,5 ,5 ,5 ,5 , 8 , 8 , 8 ], 5 , 7 ))
67
+ self .assertIsNone (findElementWhichOccursXTimesWhileOthersOccurYTimes (
68
+ [1 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 6 , 6 , 6 , 6 , 6 , 5 , 5 , 5 , 5 , 5 , 8 , 8 , 8 ], 5 , 7 ))
65
69
66
70
def testMaxZeroWhenXLessThanY (self ):
67
- self .assertIsNone (findElementWhichOccursXTimesWhileOthersOccurYTimes ([0 ] * 12 , 5 , 7 ))
71
+ self .assertIsNone (
72
+ findElementWhichOccursXTimesWhileOthersOccurYTimes ([0 ] * 12 , 5 , 7 ))
68
73
69
- def testCorrectInputXLessThanY (self ):
70
- self .assertEqual (findElementWhichOccursXTimesWhileOthersOccurYTimes ([1 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,6 ,6 ,6 ,6 ,6 ,5 ,5 ,5 ,5 ,5 , 8 , 8 , 8 ], 5 , 3 ), 8 )
74
+ def testCorrectInputXGreaterThanY (self ):
75
+ self .assertEqual (findElementWhichOccursXTimesWhileOthersOccurYTimes (
76
+ [1 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 6 , 6 , 6 , 6 , 6 , 5 , 5 , 5 , 5 , 5 , 8 , 8 , 8 ], 5 , 3 ), 8 )
71
77
72
78
def testCorrectInputXLessThanY (self ):
73
- self .assertEqual (findElementWhichOccursXTimesWhileOthersOccurYTimes ([1 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,6 ,6 ,6 ,6 ,6 ,5 ,5 ,5 ,5 ,5 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 ], 5 , 8 ), 8 )
79
+ self .assertEqual (findElementWhichOccursXTimesWhileOthersOccurYTimes (
80
+ [1 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 6 , 6 , 6 , 6 , 6 , 5 , 5 , 5 , 5 , 5 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 ], 5 , 8 ), 8 )
81
+
82
+
74
83
unittest .main ()
0 commit comments