在17年3月25日那天早上,德勤对digital班级CRM方向的学员进行了考核,30多个学员中不到15个来参加了笔试部分,现将题目和答案汇总于此,仅供学习!
一、题目
A公司是一家在互联网上销售产品的电子商务公司,他们实施了salesforce CRM,为了更好的追踪客户对他们产品的真实评价,德勤管理咨询的顾问们为其设计了一套基于深度学习的评论分析系统,并要把分析结果在salesforce里共享给各级售前售后人员。现在需要对用户评论进行处理,其中一个需求是统计用户评论(全英文)中出现的单词个数,并将统计结果存入数据库供分析建模使用。
利用Apex提供的函数尝试实现如下接口:
1. 统计文本中出现的单词个数,
Map<String, Integer>wordCount(String text);
输入text为一段英文文本
输出为单词为key, 单词出现数量为value的映射表。
举例:
System.debug(wordCount('I likeyour product, your product is durable! I like Apex and visualforce!'));
输出结果为:
{Apex=1, I=2, and=1, durable=1,is=1, like=2, product=2, visualforce=1, your=2}
2. 对统计结果进行排序的接口
List<String>wordSort(Map<String,Integer> wordCountMap);
输入为单词统计结果的映射表;
输出为按照单词出现次数排序的单词序列;
输入为:
System.debug(wordSort(wordCount('Ilike your product, your product is durable! I like Apex and visualforce!')));
输出为:
(I, like, your, product, is,durable, Apex, and, visualforce)
3. 因为A公司电商规模和评论数量呈指数增长,某些数值统计运算结果或者中间值超出了Apex基本类型的范围。我们不得不提供支持超大数字(只考虑自然数)的加法运算接口。
String add(String a, String b);
输入:
System.debug(add('111222','222333'));
System.debug(add('888888888888888888888888888889','111111111111111111111111111111'));
输出:
333555
1000000000000000000000000000000
二、答案及执行
1、code
public class Deloitte_CountWordsNumbers {
public static Map<String, Integer> wordCount(String text){
if(String.isBlank(text)) return null;
List<String> words = text.split('\\W|_');
Map<String, Integer> wordMap = new Map<String, Integer>();
for(String word:words){
if(String.isBlank(word)) continue;
Integer count = wordMap.get(word);
if(null == count){
wordMap.put(word, 1);
}else{
count++;
wordMap.put(word, count);
}
}
return wordMap;
}
}
String s = 'I like your product, your product is durable! I like Apex and visualforce!';
Map<String, Integer> m = Deloitte_CountWordsNumbers.wordCount(s);
System.debug(m);
2、code
public with sharing class Deloitte_WordsSort {
//选择排序
public static List<String> wordSort(Map<String,Integer> wordCountMap){
if(wordCountMap == null || wordCountMap.isEmpty()){
return null;
}
List<String> sl = new List<String>(wordCountMap.keySet());
if(sl.size()==1) return sl;
for(Integer i=0;i<sl.size()-1;i++){
for(Integer j=i+1;j<sl.size();j++){
if(wordCountMap.get(sl[i]) < wordCountMap.get(sl[j])){
String temp = sl[i];
sl[i] = sl[j];
sl[j] = temp;
}
}
}
return sl;
}
//冒泡排序
public static List<String> wordSort1(Map<String,Integer> wordCountMap){
if(wordCountMap == null || wordCountMap.isEmpty()){
return null;
}
List<String> sl = new List<String>(wordCountMap.keySet());
if(sl.size()==1) return sl;
for(Integer i=0;i<sl.size();i++){
for(Integer j=0;j<sl.size()-1-i;j++){
if(wordCountMap.get(sl[j]) < wordCountMap.get(sl[j+1])){
String temp = sl[j];
sl[j] = sl[j+1];
sl[j+1] = temp;
}
}
}
return sl;
}
//List.sort方法。实现
public class WordCountVO implements Comparable{
public String word{set;get;}
public Integer count{set;get;}
public Integer compareTo(Object compareTo) {
WordCountVO compareToEmp = (WordCountVO)compareTo;
if (this.count == compareToEmp.count) return 0;
if (this.count < compareToEmp.count) return 1;
return -1;
}
public WordCountVO(String word, Integer count){
this.word = word;
this.count = count;
}
public override String toString(){
return this.word;
}
}
public static List<WordCountVO> build(Map<String,Integer> wordCountMap){
if(wordCountMap == null || wordCountMap.isEmpty()){
return null;
}
List<WordCountVO> rs = new List<WordCountVO>();
for(String key:wordCountMap.keySet()){
rs.add(new WordCountVO(key,wordCountMap.get(key)));
}
return rs;
}
public static List<String> wordSort2(Map<String,Integer> wordCountMap){
if(wordCountMap == null || wordCountMap.isEmpty()){
return null;
}
List<WordCountVO> wcvs = build(wordCountMap);
wcvs.sort();
List<String> ss = new List<String>();
for(WordCountVO wcv:wcvs){
ss.add(wcv.toString());
}
return ss;
}
//List.sort方法。实现 END
}
String s = 'I like your product, your product is durable! I like Apex and visualforce!';
Map<String, Integer> m = Deloitte_CountWordsNumbers.wordCount(s);
System.debug(m);
List<String> l1 = Deloitte_WordsSort.wordSort(m);
System.debug(l1);
3、code
public with sharing class Deloitte_ComputeSuperBiggerNumber {
//a = 888888888888888888888888888889
//b = 111111111111111111111111111111
public static String add(String a, String b){
if(a==null || b==null){
return '';
}
String reg ='^\\d+$';
if(Pattern.matches(reg,a)
&& Pattern.matches(reg,b)){
String result = '';
String ar=a.reverse();
String br=b.reverse();
Integer c = 0;//进位
for(Integer i=0;i<ar.length()||i<br.length();i++){
Integer aa = i>=ar.length()?0:ar.charAt(i)-48;//可以使用substring
Integer bb = i>=br.length()?0:br.charAt(i)-48;
Integer sum = (Integer)Math.mod(aa+bb+c,10);
c = (aa+bb+c)/10;
result+=(sum);
}
if(c!=0){
result+=(c);
}
return result.reverse();
} else {
return '';
}
}
}
String a = '888888888888888888888888888889';
String b = '111111111111111111111111111111';
String r = Deloitte_ComputeSuperBiggerNumber.add(a,b);
System.debug(r);
三、执行效果预览