
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
This Java solution defines a Product class. The fillMissingQuantities method iterates through the array of Product objects and assigns zero to any null quantity values.
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.