
Sponsored
Sponsored
This method involves iterating over each row of the DataFrame. We check if the 'quantity' value is null (or equivalent) and set it to zero if it is. This is a straightforward approach that uses conditional checks within loops appropriate for each programming language.
Time Complexity: O(n), where n is the number of products.
Space Complexity: O(1), as we do not use additional space proportional to the input size.
1#include <vector>
struct Product {
std::string name;
int quantity;
int price;
};
void fillMissingQuantities(std::vector<Product>& products) {
for (auto& product : products) {
if (product.quantity == -1) { // assuming -1 stands in for 'None'
product.quantity = 0;
}
}
}
int main() {
std::vector<Product> products = {
{"Wristwatch", -1, 135},
{"WirelessEarbuds", -1, 821},
{"GolfClubs", 779, 9319},
{"Printer", 849, 3051}
};
fillMissingQuantities(products);
for (const auto& product : products) {
std::cout << product.name << ": " << product.quantity << " " << product.price << std::endl;
}
return 0;
}In this C++ solution, we use a struct to hold product data and std::vector to manage a list of products. The function fillMissingQuantities iterates through the products vector to replace a placeholder (-1) with 0 for missing quantities.
This approach involves leveraging built-in functions or libraries available in programming languages, like pandas in Python or LINQ in C#, to perform the task of filling in missing values effectively and efficiently.
Time Complexity: O(n), where n is the number of products.
Space Complexity: O(1), without additional space for the task.
1import pandas as pd
2
3data = {
4 'name': ['Wristwatch', 'WirelessEarbuds', 'GolfClubs', 'Printer'],
5 'quantity': [None, None, 779, 849],
6 'price': [135, 821, 9319, 3051]
7}
8
9products = pd.DataFrame(data)
10
11products['quantity'].fillna(0, inplace=True)
12
13print(products)By employing Python's pandas library, we can use the fillna function to efficiently handle missing values in DataFrames, specifying inplace=True to maintain modifications.