May-23-2017, 01:03 PM
Hi there,
I have been tasked with writing a function that returns the length of an unknown list. I have successfully done the algorithm for it, and it works, I just can't work out how to pass the value from the class method to my function in the format specified.
The Class and the interface cannot be modified, so using len(my_list) is not what they are looking for. posted here is the Class that I cannot modify, a test that I am to use to test the function, and then finally, my function that I am trying to write. I just need to know how I am supposed to get the value into my function, the closest I have come to is getting the memory location of the List object which is of no use to me.
Here is the Class, this code cannot be modified:
I have been tasked with writing a function that returns the length of an unknown list. I have successfully done the algorithm for it, and it works, I just can't work out how to pass the value from the class method to my function in the format specified.
The Class and the interface cannot be modified, so using len(my_list) is not what they are looking for. posted here is the Class that I cannot modify, a test that I am to use to test the function, and then finally, my function that I am trying to write. I just need to know how I am supposed to get the value into my function, the closest I have come to is getting the memory location of the List object which is of no use to me.
Here is the Class, this code cannot be modified:
[code]class MyClass(object): def __init__(self, l): self._list = l def get(self, index): try: return self._list[index] except IndexError: return None[/code]here is a test case that I am to use:
[code]def test_large_list(): s_list = My_Class([i for i in range(0, 100000)]) assert len(s_list._list) == list_length(s_list)[/code]and here is the function that I have written, it works nicely, but as you can see, the first line of my function was my way of testing my algorithm, I am now stuck as I can't write any more tests until I can get the values properly into my function:
[code]#!/usr/bin/python3 #def list_length(single_method_list): This is what I am supposed to work with from another_file import MyClass def my_function(): # This is how I have done it and it works. a_list = MyClass([i for i in range(0, 234589)]) for x in (10**p for p in range(1, 8)): if a_list.get(x): print("More than", x) first = x else: print("Less than", x) last = x break answer = False while not answer: result = (first + last)/2 result = int(round(result)) print(result) if s_list.get(result): first = result print('first', result) else: last = result print('last', result) if s_list.get(result) and not s_list.get(result + 1): answer = True print(result + 1) my_function()[/code]For some further clarification, the class that has been written for me to use, I would not write in this way, I would not use the word _list as a variable name as it is a python keyword I think, neither would I use the lower case "L" as a variable name as it looks too much like the number 1 and is not clear.