Problem Set 6(Oddophic)

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

12161 is oddophic to 93968 .
Both the  numbers are of length five.  In 12161, positions 1, 3 and 5 have the same digit 1. Hence the set of positions having the same digit are {1,3,5}. Similarly , for the number 93968, the set of positions having the same digit is  {1,3}. In both the numbers, the sets of positions having the same digit contains only odd positions.

1232 is not oddophic to 2342 because set of positions  having same digits for 1232 is {{2, 4}} and for 2342 is {{1, 4}}.
 12 is not oddophic to 10, since the digits are distinct. No digit gets repeated in 12 and 10.
Write a function to check whether two  numbers are oddophic. If none of the numbers are oddophic then print ‘No oddophic’.

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 oddophic to first number.
If the none of the numbers are not oddophic, then Print “No oddophic”

Input:
the number of numbers n

Processing:
def oddophic(n):
    set_odd = {1,3,5,7,9}
    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 not(set(i) < set_odd):
                    flag = False
                    break
    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 oddophic(number_list[0]) and oddophic(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 oddophic to the first number if none then display No oddophic number

Program:
def oddophic(n):
    set_odd = {1,3,5,7,9}
    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 not(set(i) < set_odd):
                    flag = False
                    break
    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 oddophic(number_list[0]) and oddophic(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 oddophic')
else:
    for i in final_list:
        print(i)

Algorithm:
Step1. define the function oddophic with one parameter n
Step1.1 assign set_odd as the set of odd 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_odd 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 oddophic or not by calling the oddophic 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 oddophic
Step6. End

2 comments:

  1. need in java code not in python

    ReplyDelete
    Replies
    1. All the questions and the codes are only available in python.
      Thank you for visiting the blog

      Delete

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