File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
BitManipulation/count_ones Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ def count_ones (n : int ):
2
+ count = 0
3
+ while n :
4
+ if n & 1 :
5
+ count += 1
6
+ n = n // 2
7
+ return count
8
+
9
+
10
+ if __name__ == '__main__' :
11
+ number = int (input ("Enter an integer: " ))
12
+ print ("One Count: %d" % count_ones (number ))
Original file line number Diff line number Diff line change
1
+ def is_anagram (a : str , b : str ):
2
+ count = [0 for _ in range (26 )]
3
+ count2 = [0 for _ in range (26 )]
4
+ a_length , b_length = len (a ), len (b )
5
+
6
+ if a_length != b_length :
7
+ return False
8
+
9
+ for i in range (a_length ):
10
+ if 'a' <= a [i ] <= 'z' :
11
+ count [ord (a [i ]) - ord ('a' )] += 1
12
+ elif 'A' <= a [i ] <= 'Z' :
13
+ count2 [ord (a [i ]) - ord ('A' )] += 1
14
+ if 'a' <= b [i ] <= 'z' :
15
+ count [ord (b [i ]) - ord ('a' )] -= 1
16
+ elif 'A' <= b [i ] <= 'Z' :
17
+ count2 [ord (b [i ]) - ord ('A' )] -= 1
18
+
19
+ if any (count ) or any (count2 ):
20
+ return False
21
+
22
+ return True
23
+
24
+
25
+ if __name__ == '__main__' :
26
+ a , b = 'Abc' , 'cab'
27
+ if is_anagram (a , b ):
28
+ print ('Anagrams' )
29
+ else :
30
+ print ('Not Anagrams' )
You can’t perform that action at this time.
0 commit comments