Can somebody help me to understand this code? (Step by step If possible) Would be immensely grateful. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can somebody help me to understand this code? (Step by step If possible) Would be immensely grateful.

https://code.sololearn.com/cMH0x3s7MK4F/?ref=app

29th Mar 2020, 7:35 PM
JrnJr
JrnJr - avatar
3 Answers
+ 1
Program only runs when the module is run directly (not imported), that is 'if name == main'. Function main is called, which is the entry point of this little program. There, an instance of Myclass is created, using the value v. This value is used to create a sample with all the numbers from 0 to v. Basically it creates the numbers and shuffle them (which is an awkward way to use random.sample). The list is stored in the attribute ml. Then, from main, the instance's method is called, using the value 3.14. Now for a range of length of ml, used as index, we check for all of our randomly shuffled numbers, if one of them equals 3.14, which is not the case, because all the numbers are integers from 0 to 313, randomly shuffled. Therefore the result is -1. The class attribute ml is used nowhere. Even after I've explained this code top to bottom, I have not the slightest idea what it's even supposed to do.
29th Mar 2020, 11:22 PM
HonFu
HonFu - avatar
+ 1
Thank you! I was intrigued about the fact that the class atribute were never used. I thought I was wrong. About the code, It wasn't written by me, instead It was a question in a public tender test. Anyway, thank you so much for your time. I Hope It wasn't a nuisance.
29th Mar 2020, 11:36 PM
JrnJr
JrnJr - avatar
0
I am wondering about method1: What is it supposed to do? Check if e is in ml and return its index? However, method1 only checks the first element and then returns -1 or 0, right?
30th Mar 2020, 7:21 AM
Lisa
Lisa - avatar