+ 3

can anyone explain this code

t=10 for i in range(t): c, *args = map(str,input().split()) getattr(s,c) (*(int(x) for x in args)) print(sum(s)) input: pop remove 9 discard 9 discard 8 remove 7 pop discard 6 remove 5 pop discard 5 output: 4

2nd Feb 2021, 7:24 AM
Sujithra
1 Respuesta
+ 2
this looks like a menu driven problem.... and i think there are inputs that corresponds to different operations that can be performed on a array or list... i assume the name of the list is s. and there are 10 inputs(10 operations).... c, *args=map(str, input().split()) this divides the input in line into 1+a collection of 0 or more str items... since a list is a class and s is a object, getattr()first argument is the object name, and second argument that need to passed as argument to the attribute that called based on input that is stored in c. then we convert the string inputs into a collection of int items by this (*(int(x) for x in args) ) at the end we are printing the sum of remaining elements in the list.
7th Feb 2021, 8:52 AM
NaSaPaKri
NaSaPaKri - avatar