imashutosh51

Select a random element from the stream of elements

Nov 8th, 2022 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. //This is GFG code,You won't get any practice for this problem.
  2. '''
  3. Logic is simple: We are passing the stream of numbers into the selectRandom function.
  4. We maintain a count of numbers seen so far in a variable `count`.
  5. For each new number, we generate a random number from 0 to count-1.
  6. If the random number equals count-1, we update the probable element to the current number;
  7. otherwise, we keep the previous result. This ensures each element has an equal probability
  8. of being selected from the stream, even if the total number of elements is unknown in advance.
  9.  
  10. Example: [1, 2, 3, 4]
  11.  
  12. - First element (1): Only one element exists, so its probability of being chosen is 100%.
  13.   `probable_element = 1`.
  14.  
  15. - Second element (2): Now we have two elements [1, 2].
  16.   Each element should have a 50% chance of being selected.
  17.   Generating a random number between 0 and 1 gives each element equal probability.
  18.   If the random number is 1, the current number (2) becomes the `probable_element`;
  19.   otherwise, it remains 1.
  20.  
  21. - Third element (3): Now we have three elements [1, 2, 3].
  22.   Each element should have a 1/3 chance of being chosen.
  23.   Generating a random number between 0 and 2 achieves this.
  24.   If the number is 2, we update `probable_element` to 3; otherwise, it remains the previous choice.
  25.  
  26. - Fourth element (4): With four elements [1, 2, 3, 4], each has a 1/4 chance.
  27.   A random number between 0 and 3 is generated.
  28.   If the number is 3, we update `probable_element` to 4; otherwise, we keep the previous element.
  29.  
  30. This approach is known as **reservoir sampling**, allowing us to select a random element from a stream
  31. of unknown size with uniform probability.
  32.  
  33. Note: the n can be anything but at every ith point, all the elements got the equal probabilty.
  34. '''
  35. import random
  36.  
  37. class Solution:
  38.    def __init__(self, nums):
  39.        self.nums = nums
  40.  
  41.    def pick(self, target):
  42.        res = -1   # current chosen index
  43.        count = 0  # number of target indices seen so far
  44.  
  45.        for i, num in enumerate(self.nums):
  46.            if num == target:
  47.                count += 1
  48.                # replace current result with probability 1/count
  49.                if random.randint(0, count-1) == count-1:
  50.                    res = i
  51.        return res
  52.  
  53. # Example
  54. nums = [1, 2, 3, 3, 3]
  55. solution = Solution(nums)
  56. print(solution.pick(3))  # Randomly returns 2, 3, or 4
  57. print(solution.pick(1))  # Returns 0
  58. print(solution.pick(3))  # Randomly returns 2, 3, or 4
  59.  
Advertisement
Add Comment
Please, Sign In to add comment