Created
July 16, 2024 20:47
-
-
Save kevinfiol/e1f56b04855760c75f7b549ee96c49e6 to your computer and use it in GitHub Desktop.
karat samples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const counts = [ "900,google.com", | |
"60,mail.yahoo.com", | |
"10,mobile.sports.yahoo.com", | |
"40,sports.yahoo.com", | |
"300,yahoo.com", | |
"10,stackoverflow.com", | |
"20,overflow.com", | |
"5,com.com", | |
"2,en.wikipedia.org", | |
"1,m.wikipedia.org", | |
"1,mobile.sports", | |
"1,google.co.uk"]; | |
function calculateClicksByDomain(counts = []) { | |
const map = new Map(); | |
for (const count of counts) { | |
let [clicks, url] = count.split(','); | |
clicks = Number(clicks); | |
const parts = url.split('.'); | |
for (let part = '', i = parts.length - 1; i >= 0; i--) { | |
part = parts[i] + (part ? '.' + part : ''); | |
if (!map.has(part)) map.set(part, clicks); | |
else map.set(part, map.get(part) + clicks); | |
} | |
} | |
return map; | |
} | |
const map = calculateClicksByDomain(counts); | |
console.log(map); |
Author
kevinfiol
commented
Jul 16, 2024
const completed_purchase_user_ids = [
"3123122444","234111110", "8321125440", "99911063"]
const ad_clicks = [
"122.121.0.1,2016-11-03 11:41:19,Buy wool coats for your pets",
"96.3.199.11,2016-10-15 20:18:31,2017 Pet Mittens",
"122.121.0.250,2016-11-01 06:13:13,The Best Hollywood Coats",
"82.1.106.8,2016-11-12 23:05:14,Buy wool coats for your pets",
"92.130.6.144,2017-01-01 03:18:55,Buy wool coats for your pets",
"122.121.0.155,2017-01-01 03:18:55,Buy wool coats for your pets",
"92.130.6.145,2017-01-01 03:18:55,2017 Pet Mittens"
]
const all_user_ips = [
"2339985511,122.121.0.155",
"234111110,122.121.0.1",
"3123122444,92.130.6.145",
"39471289472,2001:0db8:ac10:fe01:0000:0000:0000:0000",
"8321125440,82.1.106.8",
"99911063,92.130.6.144"
]
function parseClick(str = '') {
const [ip, date, name] = str.split(',');
return { ip, date, name };
}
function parseUser(str = '') {
const [id, ip] = str.split(',');
return { id, ip };
}
function parse(purchases = [], clickLogs = [], ips = []) {
let output = '';
const results = {};
const users = ips.map(parseUser);
const clicks = clickLogs.map(parseClick);
for (const click of clicks) {
if (!(click.name in results)) {
results[click.name] = { purchases: 0, clicks: 1 };
} else {
results[click.name].clicks += 1;
const user = users.find((u) => u.ip === click.ip);
if (user) results[click.name].purchases += 1;
}
}
for (const [name, result] of Object.entries(results)) {
if (output) output += '\n';
output += `${result.purchases} of ${result.clicks} ${name}`;
}
return output;
}
const res = parse(
completed_purchase_user_ids,
ad_clicks,
all_user_ips
);
console.log(res);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment