Practice Problem 6-Isophic Numbers

Practice Problem 6:

Isophic Numbers: 

Given ‘n’ integers, write an algorithm and the subsequent Python code to print all numbers that are isophic to the first number. Two numbers with non-distinct (numbers in which digits get repeated) digits are said to be isophic if they have the same number of digits in it and the sets of positions having the same digits contains only even positions. Positions of digits are numbered from left to right starting from 1.

For example:

12327 is isophic to 93538 .

Both the numbers are of length five. In 12327, positions 2 and 4 have the same digit 2. Hence the set of positions having the same digit are {2,4}. Similarly , for the number 83538, the set of positions having the same digit is {2,4}. In both the numbers, the sets of positions having the same digit contains only even positions.

1232 is not isophic to 2342 because set of positions having same digits for 1232 is {{2, 4}} and for 2342 is {{1, 4}}.
12 is not isomorphic to 10, since the digits are distinct. No digit gets repeated in 12 and 10.

Write a function to check whether two numbers are isophic. If none of the numbers are isophic then print ‘No isophic’.

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 isophic to first number.

If the none of the numbers are not isophic, then Print “No isophic”
Input:
the number of numbers n

Processing:
def isophic(n):
    set_even = {2,4,6,8,10}
    n = str(n)
    temp = []
    pos_list = []
    temp_pos = []
    flag = True
    for i in n:
        temp.append(int(i))
    if len(set(temp)) == len(temp):
        flag = False
    else:
        for i in set(temp):
            if temp.count(i) >= 2:
                for j in range(len(temp)):
                    if temp[j] == i:
                        temp_pos.append(j+1)
                pos_list.append(temp_pos)
                temp_pos=[]
            for i in pos_list:
                if (set(i) < set_even):
                    flag = True
                    break
                else:
                    flag = False
    return flag
n = int(input())
number_list = []
count = 0
for i in range(n):
    number_list.append(int(input()))
final_list = []
for i in range(1,n):
    if isophic(number_list[0]) and isophic(number_list[i]) and len(str(number_list[0])) == len(str(number_list[i])):
        if final_list == []:
            final_list.append(number_list[0])
        final_list.append(number_list[i])
        count += 1
Output:
display the numbers that are isophic to the first number if none then display No isophic number

Program:
def isophic(n):
    set_even = {2,4,6,8,10}
    n = str(n)
    temp = []
    pos_list = []
    temp_pos = []
    flag = True
    for i in n:
        temp.append(int(i))
    if len(set(temp)) == len(temp):
        flag = False
    else:
        for i in set(temp):
            if temp.count(i) >= 2:
                for j in range(len(temp)):
                    if temp[j] == i:
                        temp_pos.append(j+1)
                pos_list.append(temp_pos)
                temp_pos=[]
            for i in pos_list:
                if (set(i) < set_even):
                    flag = True
                    break
                else:
                    flag = False
    return flag
n = int(input())
number_list = []
count = 0
for i in range(n):
    number_list.append(int(input()))
final_list = []
for i in range(1,n):
    if isophic(number_list[0]) and isophic(number_list[i]) and len(str(number_list[0])) == len(str(number_list[i])):
        if final_list == []:
            final_list.append(number_list[0])
        final_list.append(number_list[i])
        count += 1
if count == 0:
    print('No isophic')
else:
    for i in final_list:
        print(i)

Algorithm:
Step1. define the function isophic with one parameter n
Step1.1 assign set_even as the set of even numbers
Step1.2 assign flag as True
Step1.3 separate the digits of n and assign it to temp
Step1.4 check if the digits are repeated if false then assign flag as False else move to the next step
Step1.5 iterate through temp and find the digits that are repeated and find their positions assign the positions of each repeated digit to temp_pos and append temp_pos to pos_list
Step1.6 iterate through pos_list check if each of the sets of positions are a subset of set_even if not then assign flag and break out of the loop
Step1.7 return flag
Step2. get the number n
Step3. get the n numbers and assign them to number_list and assign count as 0
Step4. check if the first number of number_list and the other numbers are isophic or not by calling the isophic function with the first number and the other numbers and also check if the number of digits of both the numbers are same or not if true then display the number and increment the count by 1
Step5. if count is equal to 0 then display no isophic
Step6. End

Comments

Popular posts from this blog

MATLAB WEEK-2

PRACTICE PROBLEM 8