Any better way to typecast variables in my function given below ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Any better way to typecast variables in my function given below ?

https://code.sololearn.com/c3sxM7IrO2SL/?ref=app def diff(l,n): a=int(l[0]) b=int(l[0]) c=0 for i in range(n): if a>int(l[i]): a=int(l[i]) if b<int(l[i]): b=int(l[i]) c=b-a return c

14th Jun 2020, 11:37 AM
Nikhil Balai
Nikhil Balai - avatar
2 Answers
+ 2
use list comprehension in the function to converts the elements (currently strings) to a list of integers....then use the max and min functions and subtract them and return it. (no need to pass the 'n'.)
14th Jun 2020, 12:38 PM
rodwynnejones
rodwynnejones - avatar
0
rodwynnejones Thank you for the help, it does work. Yes, built in functions are there, but I was just trying to explore through codes. It would be helpful in complex programs though
14th Jun 2020, 5:56 PM
Nikhil Balai
Nikhil Balai - avatar