I am trying to solve this problem on HackerEarth which ran flawlessly on my Jupyter NoteBook but for some reason, I’m getting a Run-Time error of NZEC on HackerEarth.
The question is as follows – You are provided with an array A of size N that contains non-negative integers. Your task is to determine whether the number that is formed by selecting the last digit of all the N numbers is divisible by 10.
Input format
First line: A single integer denoting the size of array A Second line: space-separated integers. Output format
If the number is divisible by 10, then print Yes. Otherwise, print No.
Code - N = int(input()) data = [int(x) for x in input().split(" ")] #print(data) new_list = [ele for ele in str(data)] #print(new_list) new_up = [] for item in range(2,len(new_list),2): #print(new_list[item]) new_up.append(new_list[item]) #print(new_up) final_list = [] #results = [] for obj in range(0,len(new_up),2): #print(new_up[obj]) final_list.append(new_up[obj]) #print(final_list) final_list = list(map(int,final_list)) #print(final_list) result = '' for items in final_list: result += str(items) #print(result) if int(result)%10 == 0: print("Yes") else: print("No")
Advertisement
Answer
One Liner time:
print("Yes") if int("".join(map(str,(i % 10 for i in [int(x) for x in input().split(" ")])))) % 10 == 0 else print("No")
Explanation:
as you already know this part is your Input [int(x) for x in input().split(" ")]
, nothing new expect that the N parameter is completely redundant and therefor is no longer needed in the code.
Next this part i % 10 for i in [Input]
loops the Input creating a List of all the last digits for the Input.
The part int("".join(map(str, list of last digits from Input)))
maps all int to a string, appends these to one another and finally converts them back into a int.
The last part then print("Yes") if appended Integer % 10 == 0 else print("No")
is a simple one liner if else.
Technically the most efficient solution for the whole problem would be to simply get the last element of the Input list and check if its last digit is 0 (as it seems the assignment is only to awnser whether the resultin int can be divided by 10 without any rest or not and for this only the last digit is neccesary) but your question states you want to go through every element and append it. As Following:
print("Yes") if int(input().split(" ")[-1]) % 10 == 0 else print("No")