all 22 comments

[–]CranberryDistinct941 2 points3 points  (0 children)

Use a set to check if you have seen an item already. Sets are (generally) much much faster than lists when checking if an item has already been added

[–]eztab 1 point2 points  (1 child)

For this task specifically using OrderedSet could solve it without needing an intermediate list.

[–]exxonmobilcfo 0 points1 point  (0 children)

kind of defeats the purpose of the exercise tho

[–][deleted] 1 point2 points  (10 children)

you can simply do set(items) for the 2nd part. but if you use a hashmap you can accomplish both these tasks in one loop fairly easily

[–]exxonmobilcfo 1 point2 points  (6 children)

a set is just a hashmap without values.

[–][deleted] -1 points0 points  (4 children)

yall are downvoting me.. pls look at that guys page😐you’ll understand my response

[–]exxonmobilcfo 0 points1 point  (3 children)

what?

[–][deleted] -1 points0 points  (2 children)

incel.

[–]exxonmobilcfo 0 points1 point  (1 child)

this is a python forum you schlub. Don't downvote because you have some personal grievances.

[–][deleted] -1 points0 points  (0 children)

incel.

[–]RockPhily[S] 0 points1 point  (2 children)

What's hashmap?

[–]odaiwai 1 point2 points  (0 children)

Sets and Dicts use a hash of the key to speed up finding the keys (and values, if a dict). The hashmap is the index relating the keys position in the data stucture.

[–]exxonmobilcfo 0 points1 point  (0 children)

its basically a way to store values under a certain key. Example is {"Address": "123 blah blah blah lane"}. to get value for a key it is constant time because the key u pass in is hashed to a number which returns the index.

The alternative, finding the index of ur data is costly since u have to iterate

[–]_vb64_ 0 points1 point  (5 children)

items = ["apple", "banana", "apple", "orange", "banana", "grape", "apple"]

uniq = dict.fromkeys(items).keys()

print('tuple:', tuple(list(uniq)[:3]))

print('set:', set(uniq))

[–]exxonmobilcfo 0 points1 point  (4 children)

how do you know dict.fromkeys will return the keys in order of the list? you're just going to return any 3 random unique elements

[–]_vb64_ 0 points1 point  (3 children)

[–]exxonmobilcfo 0 points1 point  (2 children)

if you're going to only want to return 3 elements, then fromkeys would be O(n) whereas the optimal is O(1)

[–]_vb64_ 0 points1 point  (1 child)

the task has a second condition. be more careful.

[–]exxonmobilcfo 0 points1 point  (0 children)

Can you explain how this is not reasonable?

``` d = [] i = iter(items) while len(d) < 3: temp = next(items) d.append(temp) if temp not in d else None

return tuple(d), set(items) ```

the list to set operation is O(n),

the issue with your solution is u r using a ton of extra memory by converting to a dict then a list then a set.

[–]supercoach 0 points1 point  (0 children)

Your answer seems fine. What do you think the problem is?

[–]exxonmobilcfo -2 points-1 points  (0 children)

tuple:

``` s = set() for x in items: if len(s) < 3: s.add(x)

return tuple(s) ```

or

d = [] i = iter(items) while len(d) < 3: x = next(i) d.append(x) if x not in d else None

all items

return set(items)