Last active
March 1, 2018 01:04
-
-
Save ademidun/6254c9b4f97be041d505aa69c7e6b7a4 to your computer and use it in GitHub Desktop.
Help Lavar pay for school!
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
# Lavar Ball is looking for scholarships to help pay for school. | |
# He has 2 lists, A and B. | |
# A is a cache of Lavar's scholarships as an array of primary keys. | |
# B is an array of scholarship objects matching Lavar's profile. | |
# Lavar needs to sort the objects in B based on the PKs of A, to get a new B (B_2). | |
# Lavar needs it done in less than O(n^2) time so he doesn't get bored and starts browsing Reddit. | |
# e.g. | |
A = [3,139,47] # arbitrary order | |
B = [{pk:139,name:'Scholarship X'},{pk:47,name:'Scholarship Y'},{pk:3,name:'Scholarship Z'}] | |
# Solution: B_2 = [{pk:3,name:'Scholarship Z'},{pk:47,name:'Scholarship Y'},{pk:139,name:'Scholarship Z'}] | |
# Current (very slow) solution. | |
# scholarships = list(scholarships) | |
scholarships = B | |
ranked_scholarships = A | |
# todo fix bottleneck 0(n^2) | |
for pk in user_profile.scholarship_cache: | |
scholarship = [scholarship for scholarship in scholarships if scholarship['pk'] == pk] | |
ranked_scholarships.extend(scholarship) | |
scholarships = ranked_scholarships | |
# Code will be implemented in Django (just FYI if it helps, but solution is platform/framework agnostic) | |
# A = user_profile.scholarship_cache, B = scholarships | |
# scholarships = Scholarship.objects.filter(pk__in=user_profile.scholarship_cache) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment