Python Program to print all distinct uncommon digits present in two given numbers

PythonServer Side ProgrammingProgramming

When it is required to print all the distinct uncommon digits that are present in two numbers, a method is defined that takes two integers as parameters. The method ‘symmetric_difference’ is used to get the uncommon digits.

Example

Below is a demonstration of the same

def distinct_uncommon_nums(val_1, val_2):

   val_1 = str(val_1)
   val_2 = str(val_2)
   list_1 = list(map(int, val_1))
   list_2 = list(map(int, val_2))
   list_1 = set(list_1)
   list_2 = set(list_2)
   my_list = list_1.symmetric_difference(list_2)
   my_list = list(my_list)
   my_list.sort(reverse = True)

   for i in my_list:
      print(i)

num_1 = 567234
num_2 = 87953573214
print("The value of first number is")
print(num_1)
print("The value of first number is")
print(num_2)
distinct_uncommon_nums(num_1, num_2)

Output

The value of first number is
567234
The value of first number is
87953573214
9
8
6
1

Explanation

  • A method named ‘distinct_uncommon_nums’ is defined that takes two integers as parameters.

  • Both these integers are converted to string type, and then they are mapped to integer type, and converted to a list.

  • It is then converted to a set to retain the unique values of the list.

  • Then, the ‘symmetric_difference’ method is used to get the uncommon digits in both the lists.

  • This difference is converted to a list.

  • It is then sorted in a reverse order.

  • It is displayed on the console.

  • Outside the method, two numbers are defined and are displayed on the console.

  • The method is called by passing the two numbers as parameters.

  • The output is displayed on the console.

raja
Published on 21-Sep-2021 08:18:23
Advertisements