Problem Set 6(Isophic number)

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 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

13 comments:

  1. code did not pass the execution, for this isophic numbers question.

    ReplyDelete
    Replies
    1. It does...i have checked all the conditions and its working...

      Delete
  2. it does not pass for the test case for multiple sets of repeating numbers such as 83538 in which both 8 and 3 are repititive but it does not append to final_list!!!!!!!!!

    ReplyDelete
    Replies
    1. 83538 shouldn't be in the final list because the repeated positions of 8 are {1,5} which are odd and hence shouldn't be included...but the expected output requires the 83538 to be include i would change the code to match it with the answer...the question asks us to append only those numbers whose repeated digits are found in even positions.

      Delete
    2. Updated the code for matching it with the answer...check it out now...thanks for pointing out

      Delete
    3. Working Fine Now. Thanks a lot.

      Delete
  3. plz upload the code for isomorphic numbers

    ReplyDelete
  4. I got this while running this code,
    Code did not pass the execution
    Private (Hidden) Test cases Failed.

    ReplyDelete
    Replies
    1. not possible bro... personally checked it on skill rack and also worked for other guys

      Delete
    2. me too getting the same thing bro.

      Delete
    3. i don't understand guys it's working perfectly...u must have misplaced the tabs or something like that...else it should work perfectly

      Delete
  5. n=int(input())
    a=[]
    b=[]
    c=[]
    for i in range(n):
    x=int(input())
    a.append(x)
    l=str(a[0])
    print(l)
    for i in range(len(l)):
    if l.count(l[i])>1:
    b.append(l.index(l[i]))
    for j in range(1,len(a)):
    count=[]
    p=str(a[j])
    for q in range(len(p)):
    if p.count(p[q])>1:
    count.append(p.index(p[q]))
    b.sort()
    count.sort()
    if len(l)==len(p) and b==count:
    c.append(a[j])
    if len(c)==0:
    print("No isomorph")
    else:
    for i in c:
    print(i)

    ReplyDelete

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...