
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.
1using System;
2using System.Linq;
3
4class Product {
5 public string Name { get; set; }
6 public int? Quantity { get; set; }
7 public int Price { get; set; }
8}
9
10class FillMissingData {
11 static void FillMissingQuantities(Product[] products) {
12 products = products.Select(p =>
13 {
14 if (!p.Quantity.HasValue)
15 p.Quantity = 0;
16 return p;
17 }).ToArray();
18 }
19
20 static void Main() {
21 Product[] products = {
22 new Product { Name = "Wristwatch", Quantity = null, Price = 135 },
23 new Product { Name = "WirelessEarbuds", Quantity = null, Price = 821 },
24 new Product { Name = "GolfClubs", Quantity = 779, Price = 9319 },
25 new Product { Name = "Printer", Quantity = 849, Price = 3051 }
26 };
27
28 FillMissingQuantities(products);
29 foreach (var product in products) {
30 Console.WriteLine($"{product.Name}: {product.Quantity} {product.Price}");
31 }
32 }
33}C#'s LINQ framework facilitates processing transformations with the Select method, allowing streamlined handling of nullable types in objects.