
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.
1#include <stdio.h>
2#define ROWS 4
3
4typedef struct {
5 char* name;
6 int quantity;
7 int price;
8} Product;
9
10void fillMissingQuantities(Product products[]) {
11 for (int i = 0; i < ROWS; i++) {
12 if (products[i].quantity == -1) { // assume -1 as the place for 'None'
13 products[i].quantity = 0;
14 }
15 }
16}
17
18int main() {
19 Product products[ROWS] = {
20 {"Wristwatch", -1, 135},
21 {"WirelessEarbuds", -1, 821},
22 {"GolfClubs", 779, 9319},
23 {"Printer", 849, 3051}
24 };
25
26 fillMissingQuantities(products);
27
28 for (int i = 0; i < ROWS; i++) {
29 printf("%s: %d %d\n", products[i].name, products[i].quantity, products[i].price);
30 }
31 return 0;
32}Here, we stick with the manual way as C does not have a specific library for DataFrame handling. We assume -1 indicates a missing value, replaced by 0.