
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
class Product {
public string Name { get; set; }
public int? Quantity { get; set; }
public int Price { get; set; }
}
class FillMissingData {
static void FillMissingQuantities(Product[] products) {
foreach (var product in products) {
if (!product.Quantity.HasValue) {
product.Quantity = 0;
}
}
}
static void Main() {
Product[] products = {
new Product { Name = "Wristwatch", Quantity = null, Price = 135 },
new Product { Name = "WirelessEarbuds", Quantity = null, Price = 821 },
new Product { Name = "GolfClubs", Quantity = 779, Price = 9319 },
new Product { Name = "Printer", Quantity = 849, Price = 3051 }
};
FillMissingQuantities(products);
foreach (var product in products) {
Console.WriteLine($"{product.Name}: {product.Quantity} {product.Price}");
}
}
}C# uses nullable types for quantities to represent missing values. The FillMissingQuantities method checks these values and assigns 0 where necessary. The program outputs the adjusted data to the console.
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 java.util.Arrays;
2import java.util.stream.Stream;
3
4class Product {
5 String name;
6 Integer quantity;
7 Integer price;
8
9 Product(String name, Integer quantity, Integer price) {
10 this.name = name;
11 this.quantity = quantity;
12 this.price = price;
13 }
14}
15
16public class FillMissingData {
17 public static void fillMissingQuantities(Product[] products) {
18 Arrays.stream(products).forEach(product -> {
19 if (product.quantity == null) {
20 product.quantity = 0;
21 }
22 });
23 }
24
25 public static void main(String[] args) {
26 Product[] products = {
27 new Product("Wristwatch", null, 135),
28 new Product("WirelessEarbuds", null, 821),
29 new Product("GolfClubs", 779, 9319),
30 new Product("Printer", 849, 3051)
31 };
32
33 fillMissingQuantities(products);
34
35 Arrays.stream(products).forEach(p -> System.out.println(p.name + ": " + p.quantity + " " + p.price));
36 }
37}Java's Streams API offers a concise approach for iterating through arrays. The forEach method applies a lambda to each product, altering quantities as needed.