+ 1
Can somebody help me to understand this code? (Step by step If possible) Would be immensely grateful.
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.
+ 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.
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?