Python program to check if binary representation of two numbers are anagram. (2024)

Given two numbers. Our task is to check whether they are anagrams of each other or not in binary representation. We can solve this problem quickly in python using Counter (iterable) method and Dictionary Comparison.

Examples

Input: a = 8, b = 16 Output : YesBinary representations of both numbers have same 0s and 1s.

Algorithm

Step 1 : Given two numbers.Step 2 : Convert both number into its binary using bin() function and remove first two characters because of bin().Step 3 : Since binary representation of both numbers could differ in length so we will append zeroes in start of shorter string to make both string of equal length.Step 4 : Compare both dictionaries, if value of 0’s and 1’s in both dictionaries are equal then binary representations of two numbers are anagram otherwise not. Binary representations into dictionary. 

Example Code

# function to Check if binary representations # of two numbers are anagram from collections import Counter def anagramoftwonumber(p1,p2): # convert numbers into in binary # and remove first two characters of # output string because bin function #'0b' as prefix in output string bno1 = bin(p1)[2:] bno2 = bin(p2)[2:] # append zeros in shorter string zeros = abs(len(bno1)-len(bno2)) if (len(bno1)>len(bno2)): bno2 = zeros * '0' + bno2 else: bno1 = zeros * '0' + bno1 # convert binary representations # into dictionary dict1 = Counter(bno1) dict2 = Counter(bno2) # compare both dictionaries if dict1 == dict2: print(p1, p2 ,"are two anagram number") else: print(p1 , p2 ,"are not anagram number") # Driver program if __name__ == "__main__": n1 = int(input("Enter First number ::>")) n2 = int(input("Enter Second number ::>")) anagramoftwonumber(n1,n2) 

Output

Enter First number ::>8Enter Second number ::>168 16 are two anagram numberEnter First number ::>3Enter Second number ::>23 2 are not anagram number

Updated on: 30-Jul-2019

524 Views

Related Articles

Kickstart Your Career

Get certified by completing the course

Get Started

Python program to check if binary representation of two numbers are anagram. (31)

Advertisem*nts

';adpushup.triggerAd(ad_id); });

Python program to check if binary representation of two numbers are anagram. (2024)

FAQs

How do you check if binary representations of two numbers are anagram? ›

Convert both output string containing 0 and 1 returned by bin function into dictionary using Counter() function, having 0 and 1 keys and their count as value. Compare both dictionaries, if value of 0's and 1's in both dictionaries are equal then binary representations of two numbers are anagram otherwise not.

How do you check if two numbers are anagrams in Python? ›

Code: def are_anagrams(str1, str2): return sorted(str1) == sorted(str2) # Input strings string1 = input("Enter the first string: ") string2 = input("Enter the second string: ") if are_anagrams(string1, string2): print(f"{string1} and {string2} are anagrams. ") else: print(f"{string1} and {string2} are not anagrams. ")

How do you check if a number is an anagram or not? ›

Just like strings, a number is said to be an anagram of some other number if it can be made equal to the other number by just shuffling the digits in it.

How to compare two binary values in Python? ›

  1. You can compare two binary files in Python by reading the files as binary data and comparing their contents.
  2. Check out the below code snippet:
  3. with open('file1.bin', 'rb') as file1, open('file2.bin', 'rb') as file2:
  4. data1 = file1.read()
  5. data2 = file2.read()
  6. if data1 == data2:
  7. print("The files are identical.")
  8. else:
Dec 18, 2018

How do you check if something is an anagram in Python? ›

Method 1 – Sort both strings and compare

This method first converts both input strings to lowercase and then sorts the strings using the sort() method. The resulting strings are then compared and if they match, they are anagrams. It's as simple as it sounds!

How to check if two strings are anagrams of each other in Python using dictionary? ›

Algorithm
  1. Convert both strings to lists of characters.
  2. Create a dictionary to count the frequency of each character in the first string.
  3. Iterate over the characters of the second string and decrement the corresponding count in the dictionary.
  4. If all counts in the dictionary are zero, the strings are anagrams.
Apr 23, 2023

What is an example of an anagram in Python? ›

An anagram in Python refers to rearranging the letters of one word or phrase to form another word or phrase. For example, "listen" and "silent" are anagrams because they use the same letters.

How do you check if a number is palindrome using Python? ›

To check if a number is a palindrome, convert it to a string and compare it with its reverse. In Python, this can be done with str(num) == str(num)[::-1]. If the original number and its reversed version are the same, the number is a palindrome.

How can you check if two words are anagrams of each other? ›

Two words are anagrams of each other if they contain the same number of characters and the same characters. You should only need to sort the characters in lexicographic order, and determine if all the characters in one string are equal to and in the same order as all of the characters in the other string.

What is an anagram checker? ›

The task is to check whether the given strings are anagrams of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other. Examples: Input: str1 = “listen” str2 = “silent”

How do you know if something is an anagram? ›

Any word or phrase that exactly reproduces the letters in another order is an anagram.

What is an anagram for two numbers? ›

Two integers are considered to be digit anagrams if they contain the same digits. In other words, one can be obtained from the other by rearranging the digits (or trivially, if the numbers are equal). For example, 54275 and 45572 are digit anagrams, but 321 and 782 are not (since they don't contain the same digits).

How do you compare two operands in Python? ›

Common Comparison Operators in Python

- == (Equal): Checks if two values are equal. - != (Not Equal): Determines if two values are not equal. - > (Greater Than): Compares if one value is greater than another.

What commands do we use to compare two values in Python? ›

Comparison operators
OperatorNameExample
==Equal'dog' == 'cat' Comparison result: False
!=Not equal'dog' != 'cat' Comparison result: True
>Greater than'dog' > 'cat' Comparison result: True
<Less than'dog' < 'cat' Comparison result: False
2 more rows
Dec 11, 2023

Which operator is used to compare two values in Python? ›

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and != , except when you're comparing to None .

How do you know if binary representation is palindrome? ›

Note: A binary representation is said to be a palindrome if it reads the same from left-right and right-left.

How do you check if a binary number is a palindrome? ›

Check if actual binary representation of a number is palindrome
  1. Get the number obtained by reversing the bits in the binary representation of n. Refer this post. Let it be rev.
  2. If n == rev, then print “Yes” else “No”.
Aug 1, 2022

How do you check if a binary string is a palindrome? ›

Approach is very simple,
  1. Convert given number into it's binary representation using bin(num) function.
  2. Now reverse binary representation string of number and compare it with original binary represented string, if both are equal that means binary representation of number is palindrome else not.
Mar 14, 2023

How do you identify anagrams? ›

One string is an anagram of another if the second is simply a rearrangement of the first. For example, 'heart' and 'earth' are anagrams. The strings 'python' and 'typhon' are anagrams as well.

Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 6484

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.