Untitled
unknown
plain_text
5 months ago
2.4 kB
2
Indexable
package helloWorld; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class HWD { public static Set<String> calculateLoyalCustomers(String logFile1, String logFile2) { // List<String> result = new ArrayList<>(); Set<String> res = new HashSet<>(); String[] file1 = logFile1.split("\n"); String[] file2 = logFile2.split("\n"); for(int i=0; i<file1.length; i++) { // System.out.println("File1: "+file1[i]); String cust1[] = new String[3]; cust1 = file1[i].split(","); String pageId = cust1[0]; String custId = cust1[1]; // System.out.println("pageId: "+pageId); // System.out.println("custId: "+custId); for(int j=0; j<file2.length; j++) { // System.out.println("File2: "+file2[j]); String cust2[] = new String[3]; cust2 = file2[j].split(","); // System.out.println("custId/cust2[1]: "+cust2[1]); // System.out.println("pageId/cust2[0]: "+cust2[0]); if(custId.equals(cust2[1]) && pageId != cust2[0]) { // System.out.println("Inside if"); res.add(custId); } } } return res; } //TC: O(N) // SC: public static void main(String[] args) { // TODO Auto-generated method stub //Calculating Loyal Users Via Log Files : // Our website abc.com is generating log files for each day having lines that look like // [pageId, customerId, timestamp] // Our job is given 2 logs files for day1 and day2 we have to figure out the most loyal cusomters having the following properties // = customer should have visited the website on both days // = cusomter should have visited atleast 2 unique pages // Focus on writing the method // === calculateLoyalCustomers(logFile1, logFile2) === String logFile1 = """ page1,customer1,time1 page1,customer2,time2 page2,customer1,time3 page1,customer1,time5 page4,customer2,time4 """; String logFile2 = """ page1,customer3,time1 page1,customer4,time2 page5,customer5,time3 page1,customer2,time4 """; Set<String> output = HWD.calculateLoyalCustomers(logFile1, logFile2); // for(int i=0; i<output.size(); i++) { System.out.println(output); // } //log file 1 // [1, 1, 9.00] // [1, 1, 10.00] // [2, 2, 8.00] //file 2 // [1, 1, 8.00] // [2, customerId, timestamp] // [3, customerId, timestamp] } }
Editor is loading...
Leave a Comment