Practice Problem 6-Non-Isomorphic Numbers
Practice Problem 6-Non-Isomorphic Numbers
Non-Isomorphic Numbers
Q.Given ‘n’ integers, write an algorithm and the subsequent Python code to print all numbers that are Non-isomorphic to the first number. Two numbers with non-distinct digits ( numbers in which digits are repeated) are said to be non-isomorphic if they have the same number of digits in it and the sets of positions having the same digits are different. For example:
- 12321 is not non-isomorphic to 83538 .
Both the numbers are of length five. In 12321, positions 2 and 4 have the same digit 2, Positions 1 and 5 have same digit 1. Hence the set of positions having the same digit are {{1,5},{2,4}}. Similarly , for the number 83538, the set of positions having the same digit is {{1,5}, {2,4}}. Since the sets of positions having the same digit are same , the numbers are not non-isomorphic
- 1232 is non-isomorphic to 2342 because set of positions having same digits for 1232 is {{2, 4}} and for 2342 is {{1, 4}} . The sets of positions having same digits are different. Positions are numbered from left to right starting from 1.
- 12 is not non-isomorphic to 10.
Write a function to check whether two numbers are non-isomorphic. If none of the numbers are non-isomorphic then print ‘No non-isomorphic’.
Input Format
First line contains the number of elements, n
Next ‘n’ line contains the numbers
Output Format
Print first number in the first line
Next few lines contain the numbers that are non-isomorphic to first number.
If the none of the numbers are not non-isomorphic, then Print “No non-isomorphic
Input:
the number n, n numbers
Processing:
def isomorphic(number):
number_list = list(map(int,str(number)))
length = len(number_list)
common_pos = []
temp_pos = []
for i in range(len(list(set(number_list)))):
if number_list.count(number_list[i]) >= 2:
for j in range(len(number_list)):
if number_list[i] == number_list[j]:
temp_pos.append(j)
common_pos.append(temp_pos)
temp_pos = []
return length,common_pos
final_num_list = [number]
count = 0
final_list = []
final_list = []
first_number = isomorphic(number)
for i in range(n-1):
temp = int(input())
if first_number != isomorphic(temp) and first_number[0] == isomorphic(temp)[0] and isomorphic(temp)[1]:
if final_list == []:
final_list.append(number)
final_list.append(temp)
if final_list == []:
final_list.append(number)
final_list.append(temp)
count += 1
Output:
Display the numbers that are isomorphic to first number if none are present then print No non-isomorphic
Program:
def isomorphic(number):
number_list = list(map(int,str(number)))
length = len(number_list)
common_pos = []
temp_pos = []
for i in range(len(list(set(number_list)))):
if number_list.count(number_list[i]) >= 2:
for j in range(len(number_list)):
if number_list[i] == number_list[j]:
temp_pos.append(j)
common_pos.append(temp_pos)
temp_pos = []
return length,common_pos
n = int(input())
number = int(input())
final_num_list = [number]
count = 0
first_number = isomorphic(number)
final_list = []
final_list = []
for i in range(n-1):
temp = int(input())
if first_number != isomorphic(temp) and first_number[0] == isomorphic(temp)[0] and isomorphic(temp)[1] != []:
if final_list == []:
final_list.append(number)
final_list.append(temp)
count += 1
if count == 0:
print('No non-isomorphic')
else:
for i in final_list:
print(i)
else:
for i in final_list:
print(i)
Algorithm:
Step1. Define the function isomorphic with one parameter number
Step1.1 convert the number to individual digits and assign it to number_list and assign length as the number of digits of the number
Step1.2 repeat till i is less than the number of unique digits
Step1.2.1 check whether the ith digit in the number_list is repeated if yes find all the positions and add these positions to temp_pos list and append temp_pos to common_pos
Step1.3 return the number of digits and common_pos list
Step2. Get n i.e. the number of numbers to be entered
Step3. Get the first number and call the isomorphic function with the first number and store the value that is returned in first_number variable
Step4. Repeat till i is less than n-1
Step4.1 get the number and check if the value returned when the number is called with isomorphic function with first_number is not same and also check if both the numbers have the same number of digits if both conditions are satisfied display the number.
Step4.2 increment count by 1 which was initially zero
Step5. If count is equal 0 then display no non-isomorphic
Step6.End
Comments
Post a Comment