In: Computer Science
Programming language Python
It have to be in Functions with a main function
Samuel is a math teacher at Hogwarts School of Witchcraft and
Wizardry. He loves to give his students multiplication exercises.
However, he doesn’t care about the actual operation result but the
unit sum of its digits.
At Hogwarts School of Witchcraft and Wizardry, they define the unit
sum (US) of N as the unit that it is left after doing the sum of
all the digits of a number over and over again until they get a
number of exactly 1 digit. For example: US(976) = 9 + 7 + 6 = 22
-> 2 + 2 = 4. Another example is US(19) = 1.
Samuel left as an assignment to get the US of any number. In order
to check if a student get the right answer or not, he wants to make
a program that solves it fast enough, but he is too busy for doing
it himself.
¿Can you help Samuel to check his homework?
Python code:
#defining unit sum function
def us(number):
#looping till the number is a single digit
while(len(number)!=1):
#converting the
number(which is a string) into a list of character and mapping it
into
#a list of integer and
finding the sum of the list ans converting the sum to string and
assigning it to number
number=str(sum(map(int,list(str(number)))))
#returning number
return number
def main():
#asking for number from user
number=input("Enter the number: ")
#asking for unit sum from user
ans=input("Enter its unit sum: ")
#checking if answer and unit sum are same
if(ans==us(number)):
#printing correct answer
and the unit sum
print("Congrats "+ans+"
is the correct answer")
else:
#printing wrong answer
and the unit sum
print(ans+" is the wrong
answer, correct answer is "+us(number))
if __name__ == "__main__":
main()
Screenshot:
Input and Output: