| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -175,8 +175,10 @@ List<String> list = Lists.asList(boxedArray); | |||
| 175 | 175 | - Enumerate prime numbers, EPI#5.9: [c++](/cpp-algorithm/src/array/enumerate_prime_number.h) | Enumerate prime numbers in the range. | |
| 176 | 176 | - Order elements in an array by even and odd: [c++](/cpp-algorithm/src/array/order_element.h)(`EvenOdd`) | Order even and odd numbers in the array. | |
| 177 | 177 | - Order elements in an array by specified order, EPI#5.8: [c++](/cpp-algorithm/src/array/order_element.h)(`Rearrange`) | Rearrange arrays to have a specific order. | |
| 178 | - - Random data sampling - offline, EPI#5.12: [c++](/cpp-algorithm/src/array/random_data_sampling.h)(`OfflineRandomSampling`) | Randomly select $\textit{k}$ elements from the array. | ||
| 179 | - - Random data sampling - compute permutation, EPI#5.14: [c++](/cpp-algorithm/src/array/random_data_sampling.h)(`ComputeRandomPermutation`) | Compute permutation of the array generated by random sampling. | ||
| 178 | + - Random data sampling: Select the k elements randomly from the array with uniform probability. | ||
| 179 | + - offline, EPI#5.12: [c++](/cpp-algorithm/src/array/random_data_sampling.h)(`OfflineRandomSampling`) | Design an algorithm to return a random subset of $k$ elements from an array. | ||
| 180 | + - online, EPI#5.13: [c++](/cpp-algorithm/src/array/random_data_sampling.h)(`OnlineRandomSampling`) | Design an algorithm that reads data and creates a random subset of size $k$. | ||
| 181 | + - compute permutation, EPI#5.14: [c++](/cpp-algorithm/src/array/random_data_sampling.h)(`ComputeRandomPermutation`) | Compute permutation of the array generated by random sampling. | ||
| 180 | 182 | - Replace elements | |
| 181 | 183 | - replace and remove: [c++](/cpp-algorithm/src/array/replace_element.h)(`ReplaceAndRemoveString1`, `ReplaceAndRemoveString2`) | Replace element and remove element in the array. | |
| 182 | 184 | - telex encoding: [c++](/cpp-algorithm/src/array/replace_element.h)(`TelexEncoding`) | Telex encoding for punctuation marks. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,37 @@ auto RandomDataSampling::OfflineRandomSampling(const int k, std::vector<int>& ar | |||
| 14 | 14 | return std::vector<int>{arr.begin(), arr.begin() + k}; | |
| 15 | 15 | } | |
| 16 | 16 | ||
| 17 | + auto RandomDataSampling::OnlineRandomSampling(std::vector<int>::const_iterator begin, | ||
| 18 | + const std::vector<int>::const_iterator end, | ||
| 19 | + const int k) | ||
| 20 | + -> std::vector<int> | ||
| 21 | + { | ||
| 22 | + std::vector<int> running_sample; | ||
| 23 | + // save the first k elements | ||
| 24 | + for (int i = 0; i < k; ++i) | ||
| 25 | + { | ||
| 26 | + running_sample.emplace_back(*begin++); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + std::default_random_engine seed((std::random_device())()); | ||
| 30 | + int num_seen_so_far = k; | ||
| 31 | + while (begin != end) | ||
| 32 | + { | ||
| 33 | + int x = *begin++; | ||
| 34 | + ++num_seen_so_far; | ||
| 35 | + | ||
| 36 | + // generate a random number in [0, num_seen_so_far]. | ||
| 37 | + // if the generated number exists in [0, k), replace the element with x. | ||
| 38 | + const int idx_to_replace = std::uniform_int_distribution<int>{0, num_seen_so_far - 1}(seed); | ||
| 39 | + if (idx_to_replace < k) | ||
| 40 | + { | ||
| 41 | + running_sample[idx_to_replace] = x; | ||
| 42 | + } | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + return running_sample; | ||
| 46 | + } | ||
| 47 | + | ||
| 17 | 48 | auto RandomDataSampling::ComputeRandomPermutation(const int n) -> std::vector<int> | |
| 18 | 49 | { | |
| 19 | 50 | auto permutation = std::vector<int>(n); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,23 +6,25 @@ | |||
| 6 | 6 | namespace RandomDataSampling | |
| 7 | 7 | { | |
| 8 | 8 | /** | |
| 9 | - * \brief Randomly select k elements from the array. | ||
| 9 | + * \brief Select the k elements randomly from the array with uniform probability. | ||
| 10 | + * Let A be an array with n distinct elements. Design an algorithm to return a random subset of k elements from A, where every subset has an equal chance of being chosen. | ||
| 10 | 11 | * \param k sample size | |
| 11 | 12 | * \param arr input array | |
| 12 | 13 | * \return result array | |
| 13 | 14 | */ | |
| 14 | 15 | auto OfflineRandomSampling(int k, std::vector<int>& arr) -> std::vector<int>; | |
| 15 | 16 | ||
| 16 | - // TODO: Implement OnlineRandomSampling | ||
| 17 | 17 | /** | |
| 18 | - * \brief Randomly select k elements from the array. | ||
| 18 | + * \brief Select the k elements randomly from the array with uniform probability. | ||
| 19 | + * Design an algorithm that reads data and creates a random subset of size k, where each item has an equal chance of being included. | ||
| 19 | 20 | * \param begin begin iterator | |
| 20 | 21 | * \param end end iterator | |
| 21 | 22 | * \param k sample size | |
| 22 | 23 | * \return result array | |
| 23 | 24 | */ | |
| 24 | - auto OnlineRandomSampling(const std::vector<int>::const_iterator& begin, | ||
| 25 | - const std::vector<int>::const_iterator& end, int k) | ||
| 25 | + auto OnlineRandomSampling(std::vector<int>::const_iterator begin, | ||
| 26 | + std::vector<int>::const_iterator end, | ||
| 27 | + int k) | ||
| 26 | 28 | -> std::vector<int>; | |
| 27 | 29 | ||
| 28 | 30 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,22 @@ GTEST_TEST(RandomDataSampling, OfflineRandomSampling) | |||
| 19 | 19 | EXPECT_EQ(k, static_cast<int>(result.size())); | |
| 20 | 20 | } | |
| 21 | 21 | ||
| 22 | + GTEST_TEST(RandomDataSampling, OnlineRandomSampling) | ||
| 23 | + { | ||
| 24 | + constexpr int k = 3; | ||
| 25 | + auto arr = std::vector<int>{3, 7, 5, 11}; | ||
| 26 | + const auto result = RandomDataSampling::OnlineRandomSampling(arr.cbegin(), arr.cend(), k); | ||
| 27 | + | ||
| 28 | + std::stringstream stream; | ||
| 29 | + for (const int& item : result) | ||
| 30 | + { | ||
| 31 | + stream << item << " "; | ||
| 32 | + } | ||
| 33 | + std::cout << stream.str() << std::endl; | ||
| 34 | + | ||
| 35 | + EXPECT_EQ(k, static_cast<int>(result.size())); | ||
| 36 | + } | ||
| 37 | + | ||
| 22 | 38 | GTEST_TEST(RandomDataSampling, ComputeRandomPermutation) | |
| 23 | 39 | { | |
| 24 | 40 | constexpr int k = 3; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments