INLAB 6

INLAB 6

Q.Given ‘n’ integers, write an algorithm and the subsequent Python code to print all numbers that are sum-equivalent to the first number. Two numbers  ‘m’ and ‘n’ are said to be sum-equivalent if  ‘m’ and ‘n’ have the same number of digits and the sum of the digits in ‘m’ and ‘n’ are equal.  12381 is sum-equivalent to 10545. Here both the numbers are five digit numbers. Sum of the digits in 12381 is 1+2+3+8+1=15. Similarly the sum of the digits in 10545 is 15.
Write a function to check whether two numbers are sum-equivalent or not. If none of the numbers are sum-equivalent then print ‘No sum-equivalent’.
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 sum-equivalent to first number.
If the none of the numbers are sum-equivalent , then Print “No sum-equivalent”

Input:
the number of elements n
the n numbers
Processing:
def sum_digit(number):
    total = 0
    count = 0
    temp = number
    while number:
        total += number%10
        number = number//10
        count += 1
    return temp,count,total 
n = int(input())
sum_list = []
for i in range(n):
    number = int(input())
    sum_list.append(sum_digit(number))
count = 0

Output:
the numbers that are the sum equivalents of the first number if none then display no sum equivalent

Program:
def sum_digit(number):
    total = 0
    count = 0
    temp = number
    while number:
        total += number%10
        number = number//10
        count += 1
    return temp,count,total 
n = int(input())
sum_list = []
for i in range(n):
    number = int(input())
    sum_list.append(sum_digit(number))
count = 0
for i in range(1,n):
    if sum_list[0][1] == sum_list[i][1] and sum_list[0][2] == sum_list[i][2]:
        if count == 0:
            print(sum_list[0][0])
        print(sum_list[i][0])
        count += 1
if count == 0:
    print('No sum-equivalent')

Algorithm:
Step1. Define a function sum_digit with parameter as number as follows
Step1.1 assign total, count as 0 and temp as number
Step1.2 repeat till the number is not equal to zero
Step1.2.1 assign total as the sum of total and the remainder obtained when number is divided by 10
Step1.2.2 assign number as the number obtained when number is divided by           10 and the decimal places are removed.
Step1.2.3 increment count by 1
Step1.3 return the tuple temp,count,total
Step2. Get n
Step3. Assign sum_list as an empty list
Step4. Repeat till i is less than n
Step4.1 get the number and call the function sum_digit with the actual parameter as number and the returned value should be appended in sum_list
Step5. Print the first number in sum_list
Step6. Assign count as 0
Step7. Repeat till i is less than where i goes from 1 to n
Step7.1 if the number of digits in the ith element and the total of the ith element match with the number of digits in the 0th element and the total of the 0th element then display the ith number
Step7.2 increment count by 1
Step8. If count is equal to zero then display no sum-equivalent
Step9. End

2 comments:

  1. Please provide the solutions for Isophic numbers and Elizabeth's morning walk.

    ReplyDelete
    Replies
    1. i have uploaded Elizabeth's walk and isophic number and heterophic number would be uploaded today

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