Python - Check possible bijection between a sequence of characters and digits


In mathematics, a bijection refers to a function that establishes a one-to-one correspondence between two sets. It means that each element in one set has a unique and distinct counterpart in the other set, and vice versa. In other words, a bijection ensures that there are no duplicates or missing elements in the mapping.

In the view of programming, specifically Python, checking for a bijection often involves validating whether a one-to-one mapping exists between elements of two sequences or collections. For example, given two lists, A and B, we can check if there is a bijection between their elements by comparing the lengths of the lists and examining whether each element in one list has a unique and distinct corresponding element in the other list.

Possible bijection between a sequence of characters and digits

In Python to check for a possible bijection i.e. one-to-one mapping between a sequence of characters and digits, we have to follow the below described steps.

  • Ensure the sequences have the same length − Before checking for a bijection, make sure that both the sequence of characters and the sequence of digits have the same length. If they have different lengths, a bijection is not possible.

  • Create two dictionaries to map characters to digits and vice versa − Next we have to initialize two empty dictionaries, one for mapping characters to digits and another for mapping digits to characters.

  • Iterate over the sequences and populate the dictionaries − Now iterate over the characters and digits simultaneously using the zip() function. For each character-digit pair, check if the character already exists in the character-to-digit dictionary and if the digit already exists in the digit-to-character dictionary. If any of these conditions are true, a bijection is not possible. Otherwise, add the character-digit pair to the dictionaries.

  • Check if the mapping is one-to-one − After populating the dictionaries, check if the length of the character-to-digit dictionary is equal to the length of the digit-to-character dictionary. If they have different lengths, a bijection is not possible because it would mean that one or more characters or digits have multiple mappings.

  • Repeat the process for reverse mapping − If the previous step passed i.e., the mapping is one-to-one, we can optionally repeat the process for the reverse mapping of digits to characters to ensure that the bijection is valid in both directions.

Example

Now let’s see an example of implementing the above mentioned steps using the python code.

In this example, the 'check_bijection()' function takes two sequences, 'characters' and 'digits', as input. It first checks if the sequences have the same length. Then, it initializes empty dictionaries for character-to-digit and digit-to-character mappings.

The function then iterates over the 'characters' and 'digits' using 'zip()', checks for duplicates in the dictionaries, and populates the dictionaries if no duplicates are found. Finally, it checks if the length of both dictionaries is equal, indicating a one-to-one mapping.

def check_bijection(characters, digits):
   if len(characters) != len(digits):
      return False
   char_to_digit = {}
   digit_to_char = {}
   for char, digit in zip(characters, digits):
      if char in char_to_digit or digit in digit_to_char:
         return False
      char_to_digit[char] = digit
      digit_to_char[digit] = char
   return len(char_to_digit) == len(digit_to_char)
characters = ['a', 'b', 'c']
digits = [1, 2, 3]
print(check_bijection(characters, digits))  
characters = ['a', 'b', 'c']
digits = [1, 2, 2]  
print(check_bijection(characters, digits))

Output

True
False

Example

In this example, the is_bijection() function creates two dictionaries, char_to_digit and digit_to_char, to track the mapping between characters and digits. It iterates over the characters in the sequence and the range of digits (0 to 9) using the zip() function.

For each character-digit pair, it checks if the character is already mapped to a different digit or if the digit is already mapped to a different character. If either condition is met, it returns False. If the mapping is valid for all pairs, it returns True.

def is_bijection(sequence):
   char_to_digit = {}
   digit_to_char = {}
   for char, digit in zip(sequence, range(10)):
      if char in char_to_digit and char_to_digit[char] != digit:
         return False
      if digit in digit_to_char and digit_to_char[digit] != char:
         return False
      char_to_digit[char] = digit
      digit_to_char[digit] = char
   return True
print(is_bijection("abc123")) 
print(is_bijection("abc122"))

Output

True
False

Updated on: 07-Aug-2023

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements