Problem Set 6(Heterophic Number)

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

12161 is oddophic to 76968 .
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 76968, the set of positions having the same digit is {2,4}. In 12161,the sets of positions having the same digit contains only odd positions and in 76968, the sets of positions having the same digit, contain only even positions.

1232 is not heterophic 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 heterophic. If none of the numbers are heterophic then print ‘No heterophic’.

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

If the none of the numbers are not heterophic, then Print “No heterophic”


Input:
the number of numbers n
the n numbers

Processing:
def heterophic(n):
    set_odd = {1,3,5,7,9}
    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=[]
            flag = []
            for i in pos_list:
                if set(i) < set_odd:
                    flag.append(1)
                elif set(i)<set_even:
                    flag.append(2)
                else:
                    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 heterophic(number_list[0]) != heterophic(number_list[i]) and len(str(number_list[0])) == len(str(number_list[i])) and heterophic(number_list[0]) and heterophic(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 heterophic to the first number if none then display No heterophic number

Program:
def heterophic(n):
    set_odd = {1,3,5,7,9}
    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=[]
            flag = []
            for i in pos_list:
                if set(i) < set_odd:
                    flag.append(1)
                elif set(i)<set_even:
                    flag.append(2)
                else:
                    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 heterophic(number_list[0]) != heterophic(number_list[i]) and len(str(number_list[0])) == len(str(number_list[i])) and heterophic(number_list[0]) and heterophic(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 heterophic')
else:
    for i in final_list:
        print(i)

Algorithm:
Step1. define the function heterophic 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 heterophic or not by calling the heterophic 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 heterophic

Step6. End

3 comments:

  1. there is no answer for the following questions
    1.Pengrams are words or sentences containing every letter of the English alphabet at the most once. Write an algorithm and a subsequent Python code to check whether a string is a pengram or not. Write a function to check if a given string is a pengram. For example, "He is at work" is a pengram. Since every letter of the english alphabet occurs at the most once

    Input format

    First line contains the string to be checked

    Output Format

    Print Pengram or Not pengram

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

    3.Autocube numbers are numbers having "n" digits such that the last n digits of the cube of the number will be the number itself. Write an algorithm and the subsequent Python code to check if the given number is autocube. Write a function to find the cube of a given number.. For example, 25 is a 2 digit autocube number with a cube of 15625 and 376 with its cube 53157376, is a 3 digit autocube number.

    Input Format

    First line contains the number to be checked

    Output Format

    Print Autocube or Not autocube

    ReplyDelete
    Replies
    1. pengram would be uploaded in an hour or so and all the other codes have been uploaded just use the search box on the right hand side of the website

      Delete
    2. already uploaded pengram too...

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