4 ms·
>"Though once you have hash/maps/dicts set are with reach by making sets from hashes were keys are the element and values are just true or 1 or something like t
by bogomipz 9y ago
>"Though once you have hash/maps/dicts set are with reach by making sets from hashes were keys are the element and values are just true or 1 or something like that."
Can you elaborate on how you can derive a set from hashes by using values of True or 1? Might you have a link? Thanks.
- afiedler 9y agoRuby's standard library does this. Here's the code: https://github.com/ruby/ruby/blob/trunk/lib/set.rb https://github.com/ruby/ruby/blob/trunk/lib/set.rb
- freyr 9y agoPython essentially does this as well. The idea is that an item is in the set if it's in the hash table. You can add, remove, or test for membership in constant average time. Set operations still need to be built on top of this.
- rdtsc 9y ago> Can you elaborate on how you can derive a set from hashes by using values of True or 1? Might you have a link? Thanks. Sure. Np! I mean that a simplified set is just a hash where the elements of the set are keys of the hash table and the values can be anything. I used 1 or True as example. As in adding an element would be: my_dict[element] = 1 Then membership check is: if element in my_dict Then removal is deleting: del my_dict[element] and so on. In other words, the reason sets are sometimes not explicitly there is because they are easy to implement on top of existing data structures. Basic operations like union, difference, intersection between two sets can be done with a few simple for loops. But like I mentioned in other comment, there is one interesting aspect to set (and hashes) in that the element now have to be hash-able. That kind of depends how mutability and identity works in the particular language.
- bogomipz 9y agoThanks for the clear explanation, this makes total sense. Cheers.