Advertisement
Guest User

Untitled

a guest
Feb 9th, 2022
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. # Step 1)
  2. #
  3. # Imagine an Airbnb-like vacation rental service, where users in different cities can exchange their apartment with
  4. # another user for a week. Each user compiles a wishlist of the apartments they like. These wishlists are ordered,
  5. # so the top apartment on a wishlist is that user's first choice for where they would like to spend a vacation.
  6. # You will be asked to write part of the code that will help an algorithm find pairs of users who would like to
  7. # swap with each other.
  8. #
  9. # Given a set of users, each with an *ordered* wishlist of other users' apartments:
  10. #
  11. # a's wishlist: c d
  12. # b's wishlist: d a c
  13. # c's wishlist: a b
  14. # d's wishlist: c a b
  15. #
  16. # The first user in each wishlist is the user's first-choice for whose apartment they would like to swap into.
  17. # Write a function called has_mutual_first_choice() which takes a username and returns true if that user and
  18. # another user are each other's first choice, and otherwise returns false.
  19. #
  20. # has_mutual_first_choice('a') // true (a and c)
  21. # has_mutual_first_choice('b') // false (b's first choice does not *mutually* consider b as their first choice)
  22. #
  23. # Then expand the base case beyond just "first" choices, to include all "mutually ranked choices". Write
  24. # another function which takes a username and an option called "rank" to indicate the wishlist rank to query
  25. # on. If given a rank of 0, you should check for a first choice pair, as before. If given 1, you should check
  26. # for a pair of users who are each others' second-choice. Call your new function has_mutual_pair_for_rank()
  27. # and when done, refactor has_mutual_first_choice() to depend on your new function.
  28. #
  29. # has_mutual_pair_for_rank('a', 0) // true (a and c)
  30. # has_mutual_pair_for_rank('a', 1) // true (a and d are mutually each others' second-choice)
  31.  
  32. # import unittest
  33.  
  34. # data = {
  35. # 'a': ['c', 'd'],
  36. # 'b': ['d', 'a', 'c'],
  37. # 'c': ['a', 'b'],
  38. # 'd': ['c', 'a', 'b'],
  39. # }
  40.  
  41.  
  42. # Step 2)
  43. #
  44. # Every wishlist entry in the network is either "mutually ranked" or "not mutually ranked" depending on the rank
  45. # the other user gives that user's apartment in return.
  46. #
  47. # The most common operation in the network is incrementing the rank of a single wishlist entry on a single user.
  48. # This swaps the entry with the entry above it in that user's list. Imagine that, when this occurs, the system must
  49. # recompute the "mutually-ranked-ness" of any pairings that may have changed.
  50. #
  51. # Write a function that takes a username and a rank representing the entry whose rank is being bumped up. Return an
  52. # array of the users whose pairings with the given user *would* gain or lose mutually-ranked status as a result of
  53. # the change, if it were to take place. Call your function changed_pairings()
  54. #
  55. # // if d's second choice becomes their first choice, a and d will no longer be a mutually ranked pair
  56. # changed_pairings('d', 1) // returns ['a']
  57. #
  58. # // if b's third choice becomes their second choice, c and b will become a mutually ranked pair (mutual second-choices)
  59. # changed_pairings('b', 2) // returns ['c']
  60. #
  61. # // if b's second choice becomes their first choice, no mutually-ranked pairings are affected
  62. # changed_pairings('b', 1) // returns []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement