Armstrong Number of Order N
Coding Problem Keys
Armstrong Number of Order N
Problem Statement
Consider a non-empty string instr. Identify and print a string outstr using the below mentioned logic.
- Find the sum of ASCII value of all the characters present in the instr.
- If sum is an Armstrong number of order n, remove duplicate characters present in the string instr and assign the updated instr to outstr. Perform case-sensitive comparison of characters while removing duplicate characters
- A number with digit is said to be an Armstrong number of order n if the sum of its digits each raised to the power of n is equal to the number i.e. abcd = a⁴ + b⁴ + c⁴ + d⁴.
- If the sum is not an Armstrong number of order n, print -1.
Input Format
Output Format
Example Input/Output 1
Input
Output
Explanation
The sum of ASCII values of all the characters present in instr is 66 + 65 + 78 + 97 + 65 sum i.e. 371 which has 3 digits.
The sum i.e. 371, is an Armstrong number of order n i.e. 3. So remove the duplicate character in instr i.e. 'A', and assign the updated instr, i.e. 'BANa' to outstr.
Example Input/Output 2
Input
Output
Explanation
The sum of ASCII value of all the characters present in instr is 86 + 97 + 110 = 293 which has 3 digits.
The sum i.e. 293, is not an Armstrong number of order n i.e. 3.
Hence the output -1.
Solution
Programming Language: Python 3 Language
import math l = input() s = 0 c = 0 for i in l: s+=ord(i) for j in str(s): c+=pow(int(j),len(str(s))) res = [] flag = False if c==s: res = list(dict.fromkeys(l)) flag = True if flag: for i in res: print(i,end="") else: print(-1)
# Published By PKJCODERS
Alter
l=input() s=0;c=0;r=0;a="" for x in l: s=s+ord(x) n=s while n>0: n=n//10 c=c+1 n=s while n>0: d=n%10 n=n//10 r=r+d**c if s==r: for x in w: if x not in a: a=a+x print(a) else: print(-1)
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments