Created
December 11, 2012 01:11
-
-
Save beng/4254889 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def largest(items, index): | |
""" | |
-create a dictionary where the key=item, value=occurrence of item | |
-get minimum key in dictionary (smallest element) | |
-add key to ordered list v times (add element number of times it occurs) | |
-remove pair from dictionary | |
-return kth element from ordered list | |
""" | |
d = collections.defaultdict(int) | |
order = [] | |
for item in items: | |
d[item] += 1 | |
while d: | |
key = min(d.keys()) | |
value = d[key] | |
order.extend([key] * value) | |
d.pop(order[-1]) | |
return order[k-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment