We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The solution works fine and is efficient in Python 3 giving O(1) complexity.
But it will not be efficient in Python 2. In Python 2, range() returns a list. Therefore the value self.age will be compared with each element of the returned list resulting in O(n) complexity for each if elif case.
In simple terms, if n was a large number, if x in range(n) would take far longer time to compute than if x < n.
More here
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 4: Class vs. Instance
You are viewing a single comment's thread. Return to all comments →
The solution works fine and is efficient in Python 3 giving O(1) complexity.
But it will not be efficient in Python 2. In Python 2,
range()
returns a list. Therefore the valueself.age
will be compared with each element of the returned list resulting in O(n) complexity for each if elif case.In simple terms, if
n
was a large number,if x in range(n)
would take far longer time to compute thanif x < n
. More here