On Sunday, 15 February 2015 at 17:18:08 UTC, Steve D wrote:
Python (built-in)

dict1 = {"a":1,"b":2}
tup1  = (0,1,2,3)
arr1  = [0,1,2,3]  # list type
str1  = "abcd"

print "b" in dict1    # True
O(1) lookup

print 3 in tup1       # True
O(n) lookup

print 3 in arr1       # True
O(n) lookup

print "c" in str1     # True
O(n) lookup

This has been discussed to death here. D is better off than Python in this case, as it doesn't hide the fact that it's doing an O(n) operation sometimes and an O(1) operation other times.


print tup1.index(2)   # 2
No way to do this in D

print arr1.index(2)   # 2
arr1.countUntil(2)

print str1.index("c") # 2
str1.countUntil("c")


There is some sort of consistency of use, though they are different sequence/collection types.

The problem is mainly with Tuple, which is not a range (arrays and strings are). You can, however, write a small utility function for this. Now that I think about it, it can probably be made O(1) as well.

Reply via email to