Skip to content

Commit f1db52f

Browse files
Merge pull request matthewsamuel95#599 from shakib609/master
Add count_ones and is_anagram Python3 implementations
2 parents 35b75d5 + c2a9351 commit f1db52f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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))

String/Anagram/is_anagram.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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')

0 commit comments

Comments
 (0)