构建一个返回满足约束

时间:2018-04-21 15:41:34

标签: javascript arrays twitter timestamp hashtable

我一直在使用数据集并解决了一些限制,但现在我被卡住了。

非常感谢任何帮助和解释。

问题是:

编写接受活动日志文件的绝对路径的函数bot_spammers,并返回所有垃圾邮件发送者名称的列表

识别垃圾邮件的条件是:

- The user has no original tweets.
- The user has at least one retweet.
- All the retweets of this user occur within 1000 milliseconds of the original tweet.
- In the corner case where the retweet occurs exactly 1000 milliseconds after the original tweet, it should also be considered as a spam retweet.

- The output list must not contain duplicates.

到目前为止,这是我的解决方案:

let dataEntry = data.trim().split('\n').map(dataLog=>{
          return JSON.parse(dataLog);
        });

dataEntry[0];
let users = {};
let secondOutput = []

for(let i = 0; i < dataEntry.length;i++){
  if(!users[dataEntry[i].user]){
      users[dataEntry[i].user] = {
        actions:{
          [dataEntry[i].action]:0},
          bot:false};
  } else{
   users[dataEntry[i].user].actions[dataEntry[i].action]+=1;
  }
}

目前我的用户对象在第一次看到用户时会存储用户,但我也会收到推文作为NaN。我存储用户错了吗?

此处还有我正在使用的示例数据:

let data = `{"action": "tweet", "id": 8270627288848549106, "message": "woop #vim", "timestamp": 1487149317226, "user": "garybernhardt"}
{"action": "retweet", "target_id": 8270627288848549106, "timestamp": 1487149317678, "user": "spambot"}
{"action": "follow", "target": "garybernhardt", "timestamp": 1487149320543, "user": "neovim"}
{"action": "follow", "target": "garybernhardt", "timestamp": 1487149320596, "user": "eevee"}
{"action": "follow", "target": "garybernhardt", "timestamp": 1487149320730, "user": "r00k"}
{"action": "tweet", "id": 9428781849524299055, "message": "woop #chess", "timestamp": 1487149394095, "user": "FabianoCaruana"}
{"action": "follow", "target": "FabianoCaruana", "timestamp": 1487149397523, "user": "GMHikaru"}
{"action": "tweet", "id": 16606634614844517897, "message": "woop #ruby", "timestamp": 1487149445603, "user": "hugopeixoto"}
{"action": "retweet", "target_id": 16606634614844517897, "timestamp": 1487149446020, "user": "spambot"}
{"action": "retweet", "target_id": 16606634614844517897, "timestamp": 1487149446592, "user": "noob_saibot"}
{"action": "tweet", "id": 14936153188153171323, "message": "woop #vim", "timestamp": 1487149463067, "user": "eevee"}
{"action": "follow", "target": "eevee", "timestamp": 1487149466209, "user": "pkoch"}
{"action": "tweet", "id": 1801829421162967878, "message": "woop #processes", "timestamp": 1487149468671, "user": "r00k"}
{"action": "retweet", "target_id": 1801829421162967878, "timestamp": 1487149469555, "user": "noob_saibot"}
{"action": "follow", "target": "r00k", "timestamp": 1487149472418, "user": "pkoch"}
{"action": "tweet", "id": 17893479187194161545, "message": "woop #chess", "timestamp": 1487149492184, "user": "GMHikaru"}
{"action": "retweet", "target_id": 17893479187194161545, "timestamp": 1487149493120, "user": "noob_saibot"}
{"action": "follow", "target": "GMHikaru", "timestamp": 1487149495548, "user": "Vachier_Lagrave"}
{"action": "follow", "target": "GMHikaru", "timestamp": 1487149495615, "user": "MagnusCarlsen"}
{"action": "follow", "target": "GMHikaru", "timestamp": 1487149496034, "user": "SergeyKaryakin"}
{"action": "tweet", "id": 5861128972147966174, "message": "woop #infra", "timestamp": 1487149496888, "user": "Pinboard"}
{"action": "retweet", "target_id": 5861128972147966174, "timestamp": 1487149497698, "user": "noob_saibot"}
{"action": "retweet", "target_id": 5861128972147966174, "timestamp": 1487149497790, "user": "robot"}
{"action": "retweet", "target_id": 5861128972147966174, "timestamp": 1487149498273, "user": "_ko1"}
{"action": "tweet", "id": 15546610455779134048, "message": "woop #infosec", "timestamp": 1487149655432, "user": "tqbf"}
{"action": "retweet", "target_id": 15546610455779134048, "timestamp": 1487149656272, "user": "robot"}
{"action": "follow", "target": "tqbf", "timestamp": 1487149659229, "user": "FioraAeterna"}
{"action": "follow", "target": "tqbf", "timestamp": 1487149659338, "user": "Pinboard"}
{"action": "tweet", "id": 16097723073400512850, "message": "woop #foss", "timestamp": 1487149769185, "user": "sarahjeong"}
{"action": "retweet", "target_id": 16097723073400512850, "timestamp": 1487149769498, "user": "spambot"}
{"action": "retweet", "target_id": 16097723073400512850, "timestamp": 1487149770023, "user": "noob_saibot"}
{"action": "follow", "target": "sarahjeong", "timestamp": 1487149772395, "user": "tqbf"}
{"action": "follow", "target": "sarahjeong", "timestamp": 1487149772583, "user": "moxie"}
{"action": "follow", "target": "sarahjeong", "timestamp": 1487149772811, "user": "hugopeixoto"}
{"action": "tweet", "id": 1645453767952446018, "message": "woop #infra", "timestamp": 1487149868897, "user": "pkoch"}
{"action": "retweet", "target_id": 1645453767952446018, "timestamp": 1487149870292, "user": "_ko1"}
{"action": "follow", "target": "pkoch", "timestamp": 1487149872809, "user": "amix3k"}
{"action": "tweet", "id": 4857568027760499546, "message": "woop #infra", "timestamp": 1487149889607, "user": "amix3k"}
{"action": "tweet", "id": 11656701733571143507, "message": "woop #ruby", "timestamp": 1487149891498, "user": "_ko1"}
{"action": "follow", "target": "_ko1", "timestamp": 1487149895292, "user": "hugopeixoto"}
{"action": "tweet", "id": 1787602799334447130, "message": "woop #vim", "timestamp": 1487150049615, "user": "neovim"}
{"action": "retweet", "target_id": 1787602799334447130, "timestamp": 1487150050440, "user": "noob_saibot"}
{"action": "retweet", "target_id": 1787602799334447130, "timestamp": 1487150050564, "user": "robot"}
{"action": "retweet", "target_id": 1787602799334447130, "timestamp": 1487150050939, "user": "_ko1"}
{"action": "follow", "target": "neovim", "timestamp": 1487150053298, "user": "r00k"}
{"action": "tweet", "id": 8809744136707487373, "message": "woop #pens", "timestamp": 1487150169467, "user": "0xabadidea"}
{"action": "retweet", "target_id": 8809744136707487373, "timestamp": 1487150169781, "user": "spambot"}
{"action": "retweet", "target_id": 8809744136707487373, "timestamp": 1487150170468, "user": "noob_saibot"}
{"action": "retweet", "target_id": 8809744136707487373, "timestamp": 1487150170858, "user": "_ko1"}
{"action": "tweet", "id": 7918310494010412941, "message": "woop #hardware", "timestamp": 1487150194893, "user": "FioraAeterna"}
{"action": "retweet", "target_id": 7918310494010412941, "timestamp": 1487150195198, "user": "spambot"}
{"action": "follow", "target": "FioraAeterna", "timestamp": 1487150198306, "user": "0xabadidea"}
{"action": "follow", "target": "FioraAeterna", "timestamp": 1487150198449, "user": "SwiftOnSecurity"}
{"action": "tweet", "id": 7755774894324805211, "message": "woop #chess", "timestamp": 1487150224598, "user": "MagnusCarlsen"}
{"action": "retweet", "target_id": 7755774894324805211, "timestamp": 1487150225462, "user": "noob_saibot"}
{"action": "follow", "target": "MagnusCarlsen", "timestamp": 1487150228060, "user": "MauriceAshley"}
{"action": "tweet", "id": 12826016841040824010, "message": "woop #ruby", "timestamp": 1487150233715, "user": "Una"}
{"action": "retweet", "target_id": 12826016841040824010, "timestamp": 1487150234096, "user": "spambot"}
{"action": "retweet", "target_id": 12826016841040824010, "timestamp": 1487150234523, "user": "robot"}
{"action": "retweet", "target_id": 12826016841040824010, "timestamp": 1487150234662, "user": "noob_saibot"}
{"action": "follow", "target": "Una", "timestamp": 1487150236840, "user": "hugopeixoto"}
{"action": "follow", "target": "Una", "timestamp": 1487150236850, "user": "_ko1"}
{"action": "tweet", "id": 4421172895615467875, "message": "woop #gamedev", "timestamp": 1487150245711, "user": "ChuckBergeron"}
{"action": "follow", "target": "ChuckBergeron", "timestamp": 1487150249293, "user": "eevee"}
{"action": "tweet", "id": 11109614754327691681, "message": "woop #infosec", "timestamp": 1487150302864, "user": "moxie"}
{"action": "retweet", "target_id": 11109614754327691681, "timestamp": 1487150303352, "user": "spambot"}
{"action": "retweet", "target_id": 11109614754327691681, "timestamp": 1487150304336, "user": "_ko1"}
{"action": "follow", "target": "moxie", "timestamp": 1487150305886, "user": "FioraAeterna"}
{"action": "follow", "target": "moxie", "timestamp": 1487150306165, "user": "Pinboard"}
{"action": "follow", "target": "moxie", "timestamp": 1487150306470, "user": "0xabadidea"}
{"action": "follow", "target": "moxie", "timestamp": 1487150306605, "user": "tqbf"}
{"action": "tweet", "id": 14726206988050621829, "message": "woop #chess", "timestamp": 1487150406492, "user": "SergeyKaryakin"}
{"action": "retweet", "target_id": 14726206988050621829, "timestamp": 1487150407311, "user": "robot"}
{"action": "follow", "target": "SergeyKaryakin", "timestamp": 1487150409851, "user": "MagnusCarlsen"}
{"action": "follow", "target": "SergeyKaryakin", "timestamp": 1487150409997, "user": "Vachier_Lagrave"}
{"action": "tweet", "id": 2834894569111365477, "message": "woop #infosec", "timestamp": 1487150788572, "user": "SwiftOnSecurity"}
{"action": "retweet", "target_id": 2834894569111365477, "timestamp": 1487150789526, "user": "noob_saibot"}
{"action": "retweet", "target_id": 2834894569111365477, "timestamp": 1487150789942, "user": "_ko1"}
{"action": "follow", "target": "SwiftOnSecurity", "timestamp": 1487150791611, "user": "hugopeixoto"}
{"action": "follow", "target": "SwiftOnSecurity", "timestamp": 1487150791850, "user": "amix3k"}
{"action": "follow", "target": "SwiftOnSecurity", "timestamp": 1487150792303, "user": "FioraAeterna"}
{"action": "tweet", "id": 8729815892990076215, "message": "woop #chess", "timestamp": 1487150819870, "user": "MauriceAshley"}
{"action": "retweet", "target_id": 8729815892990076215, "timestamp": 1487150820325, "user": "spambot"}
{"action": "retweet", "target_id": 8729815892990076215, "timestamp": 1487150820748, "user": "noob_saibot"}
{"action": "follow", "target": "MauriceAshley", "timestamp": 1487150823060, "user": "MagnusCarlsen"}
{"action": "tweet", "id": 4049417484390121080, "message": "woop #chess", "timestamp": 1487150969996, "user": "Vachier_Lagrave"}
{"action": "retweet", "target_id": 4049417484390121080, "timestamp": 1487150970474, "user": "spambot"}
{"action": "retweet", "target_id": 4049417484390121080, "timestamp": 1487150971366, "user": "_ko1"}
{"action": "follow", "target": "Vachier_Lagrave", "timestamp": 1487150973029, "user": "FabianoCaruana"}
{"action": "follow", "target": "Vachier_Lagrave", "timestamp": 1487150973245, "user": "GMHikaru"}
{"action": "follow", "target": "Vachier_Lagrave", "timestamp": 1487150973870, "user": "SergeyKaryakin"}
{"action": "tweet", "id": 6255551220274019073, "message": "woop #infosec", "timestamp": 1487151346348, "user": "hugopeixoto"}
{"action": "retweet", "target_id": 6255551220274019073, "timestamp": 1487151346777, "user": "spambot"}
{"action": "retweet", "target_id": 6255551220274019073, "timestamp": 1487151347760, "user": "_ko1"}
{"action": "follow", "target": "hugopeixoto", "timestamp": 1487151349400, "user": "amix3k"}
{"action": "follow", "target": "hugopeixoto", "timestamp": 1487151349759, "user": "moxie"}
{"action": "follow", "target": "hugopeixoto", "timestamp": 1487151350302, "user": "Pinboard"}
{"action": "follow", "target": "hugopeixoto", "timestamp": 1487151350320, "user": "0xabadidea"}
{"action": "tweet", "id": 14172137775635128506, "message": "woop #cosplay", "timestamp": 1487151684539, "user": "eevee"}
{"action": "tweet", "id": 9654574229292990286, "message": "woop #ruby", "timestamp": 1487152103458, "user": "_ko1"}
`;

0 个答案:

没有答案