Google - Find Most People in Chat Log
1. 给你一个chatting log file,format大概是这样的:
A: bla
B: bla bla
C: bla bla bla
要你找出说话最多(看word number) 的K个人
而且代码要从读file开始写
/*
1. 给你一个chatting log file,format大概是这样的:
A: bla
B: bla bla
C: bla bla bla
要你找出说话最多(看word number) 的K个人
而且代码要从读file开始写 */
public class Main {
public static void main(String[] args) {
List<String> lines =new ArrayList<>();
lines.add("A: bla");
lines.add("A: bla");
lines.add("B: bla bla");
lines.add("C: bla bla bla");
lines.add("A: bla"); List<String> list = new Solution().findMostPeopleTest(lines);
for(String name : list){
System.out.println(name);
}
}
} class Solution{
/*
public List<String> findMostPeople (String filePath) throws Exception {
HashMap<String, Integer> map = new HashMap<>();
int maxCounts = 0;
List<String> list = new ArrayList<>();
//read from file
File file = new File(filePath);
BufferedReader br = new BufferedReader(new FileReader(filePath));
String st;
while((st = br.readLine()) != null){
//can be replaced by other string related functions
String[] strs = st.split(": ");
String name = strs[0];
String[] words =strs[1].split(" ");
int wordsCount = words.length;
int totalCounts = map.getOrDefault(name, 0)+wordsCount;
map.put(name, totalCounts);
if(totalCounts > maxCounts){
list.clear();
list.add(name);
}
else if (totalCounts == maxCounts){
list.add(name);
}
}
return list;
}
*/
public List<String> findMostPeopleTest (List<String> lines) {
HashMap<String, Integer> map = new HashMap<>();
int maxCounts = 0;
List<String> list = new ArrayList<>();
//read from file
//File file = new File(filePath);
//BufferedReader br = new BufferedReader(new FileReader(filePath));
for(String st : lines){
//can be replaced by other string related functions
String[] strs = st.split(": ");
String name = strs[0];
String[] words =strs[1].split(" ");
int wordsCount = words.length; int totalCounts = map.getOrDefault(name, 0)+wordsCount;
System.out.println(name + " : "+totalCounts);
map.put(name, totalCounts);
if(totalCounts > maxCounts){
list.clear();
list.add(name);
maxCounts = totalCounts;
}
else if (totalCounts == maxCounts){
list.add(name);
}
}
return list;
} }
Google - Find Most People in Chat Log的更多相关文章
- Google glog 使用
Google glog 使用 1 简介 Googleglog 库实现了应用级的日志记录,提供了C++ 风格的流操作和各种助手宏. 代码示例: #include <glog/logg ...
- Python + logging 输出到屏幕,将log日志写入文件
日志 日志是跟踪软件运行时所发生的事件的一种方法.软件开发者在代码中调用日志函数,表明发生了特定的事件.事件由描述性消息描述,该描述性消息可以可选地包含可变数据(即,对于事件的每次出现都潜在地不同的数 ...
- 百度Apollo解析——2.log系统
Apollo中的glog 在Apollo中google glog 被广泛使用,glog 是 google 的一个 c++ 开源日志系统,轻巧灵活,入门简单,而且功能也比较完善. 1. 安装 以下是官方 ...
- Python + logging输出到屏幕,将log日志写入到文件
logging提供了一组便利的函数,用来做简单的日志.它们是 debug(). info(). warning(). error() 和 critical(). logging函数根据它们用来跟踪的事 ...
- c++ google glog模块安装和基本使用(ubuntu)环境
1,如何安装 1 Git clone https://github.com/google/glog.git 2 cd glog 3 ./autogen.sh 4 ./configure --prefi ...
- Blazor组件自做五 : 使用JS隔离封装Google地图
Blazor组件自做五: 使用JS隔离封装Google地图 运行截图 演示地址 正式开始 1. 谷歌地图API 谷歌开发文档 开始学习 Maps JavaScript API 的最简单方法是查看一个简 ...
- VBV Rate Control
Part 1 <06/05/07 12:08pm> Manao | he is negating a float by printing it, adding a "-" ...
- python自动化运维之路~DAY5
python自动化运维之路~DAY5 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.模块的分类 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数 ...
- How to calculate bits per character of a string? (bpc) to read
http://stackoverflow.com/questions/17797922/how-to-calculate-bits-per-character-of-a-string-bpc up ...
随机推荐
- CORS跨域请求
一.问题: 服务器端代码 from flask import Flask from flask import make_response from flask import jsonify app = ...
- Pandas 基础(12) - Stack 和 Unstack
这节的主题是 stack 和 unstack, 我目前还不知道专业领域是怎么翻译的, 我自己理解的意思就是"组成堆"和"解除堆". 其实, 也是对数据格式的一种 ...
- 常见的Git命令
最近想着需要把工作中做一个备份,除了本地保存之外,上传到码云是个不错的选择,除了Git的一些特点外,也可以让别人看到你的代码,共同修改之类的 首先在上传到码云之前,需要学习Git的一些基础教程,包括国 ...
- 714-Card Trick
思维题,把n个卡片倒着放,然后每个卡片循环放到最底下i次,最后出来的结果就是要求的卡牌顺序 #include<stdio.h> #include<string.h> #incl ...
- [原][算法][earth]三段smooth,传入时间,返回距离。仿谷歌视角飞跃处理
算法需求: 传入[0~1]的时间time,返回[0~1]的路程. 整个路程distance[0~1]分为三段路径: 第一段:在0.25time的时间里,速度从0,位置从distance:0加速移动到距 ...
- 新建vue项目中遇到的报错信息
在npm install的时候会报错,经过上网查阅资料之后,解决方法如下: 0.先升级npm版本:npm install -g npm 有可能是npm版本过低报错 1.然后清理缓存: npm ca ...
- 『TensorFlow』命令行参数解析
argparse很强大,但是我们未必需要使用这么繁杂的东西,TensorFlow自己封装了一个简化版本的解析方式,实际上是对argparse的封装 脚本化调用tensorflow的标准范式: impo ...
- php如何以post形式发送xm并返回xmll数据
post的数据为xml字符串,通过 $xml = simplexml_load_string($post_data);转换成xml对象 $header[] = "Content-type: ...
- 2015-10-12 jQuery4
十. 直接获取.编辑内容 1.获取内容 alert($("#d1").text()); //获取文本内容 alert($("#d1").html()); ...
- 拒绝服务(DoS)理解、防御与实现
一.说明 我一直不明白为什么拒绝服务,初学着喜欢拿来装逼.媒体喜欢吹得神乎其神.公司招聘也喜欢拿来做标准,因为我觉得拒绝服务和社会工程学就是最名不副实的两样东西.当然由于自己不明确拒绝服务在代码上是怎 ...