Skip to content

Commit

Permalink
Merge pull request matthewsamuel95#599 from shakib609/master
Browse files Browse the repository at this point in the history
Add count_ones and is_anagram Python3 implementations
  • Loading branch information
matthewsamuel95 authored Oct 17, 2018
2 parents 35b75d5 + c2a9351 commit f1db52f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions BitManipulation/count_ones/count_ones.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def count_ones(n: int):
count = 0
while n:
if n & 1:
count += 1
n = n // 2
return count


if __name__ == '__main__':
number = int(input("Enter an integer: "))
print("One Count: %d" % count_ones(number))
30 changes: 30 additions & 0 deletions String/Anagram/is_anagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def is_anagram(a: str, b: str):
count = [0 for _ in range(26)]
count2 = [0 for _ in range(26)]
a_length, b_length = len(a), len(b)

if a_length != b_length:
return False

for i in range(a_length):
if 'a' <= a[i] <= 'z':
count[ord(a[i]) - ord('a')] += 1
elif 'A' <= a[i] <= 'Z':
count2[ord(a[i]) - ord('A')] += 1
if 'a' <= b[i] <= 'z':
count[ord(b[i]) - ord('a')] -= 1
elif 'A' <= b[i] <= 'Z':
count2[ord(b[i]) - ord('A')] -= 1

if any(count) or any(count2):
return False

return True


if __name__ == '__main__':
a, b = 'Abc', 'cab'
if is_anagram(a, b):
print('Anagrams')
else:
print('Not Anagrams')

0 comments on commit f1db52f

Please sign in to comment.