Zuora在线测试题解之Three Page Path

来源:互联网 发布:ubuntu apt 安装jdk8 编辑:程序博客网 时间:2024/06/07 03:23

题目记不清楚了,大概是给出用户访问页面的记录(user, page),要求给出最经常被访问的前M个路径,每个路径至少包含n个页面。

定义类如下,测试通过。


1. 原始数据

package com.zuora.interview.path3page;public class UserPage {private String user;private String page;public UserPage() {// TODO Auto-generated constructor stub}public UserPage(String user, String page) {super();this.user = user;this.page = page;}public String getUser() {return user;}public void setUser(String user) {this.user = user;}public String getPage() {return page;}public void setPage(String page) {this.page = page;}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub}}


2. 中间处理数据

package com.zuora.interview.path3page;public class UserPath {private String user;private String path;public UserPath() {// TODO Auto-generated constructor stub}public UserPath(String user, String path) {super();this.user = user;this.path = path;}public String getUser() {return user;}public void setUser(String user) {this.user = user;}public String getPath() {return path;}public void setPath(String path) {this.path = path;}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub}}


3.处理后的数据

package com.zuora.interview.path3page;public class PathCount {private String path;private int count;public PathCount() {// TODO Auto-generated constructor stub}public PathCount(String path, int count) {super();this.path = path;this.count = count;}public String getPath() {return path;}public void setPath(String path) {this.path = path;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub}}


4.服务调用逻辑

package com.zuora.interview.path3page;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class Solution {private List<UserPage> userPages = new ArrayList<UserPage>();private List<UserPath> userPaths = new ArrayList<UserPath>();private List<PathCount> pathCounts = new ArrayList<PathCount>();public void initUserPages(){userPages.add(new UserPage("U1", "/"));userPages.add(new UserPage("U1", "subscibers"));userPages.add(new UserPage("U2", "/"));userPages.add(new UserPage("U2", "subscibers"));userPages.add(new UserPage("U1", "filter"));userPages.add(new UserPage("U1", "export"));userPages.add(new UserPage("U2", "filter"));userPages.add(new UserPage("U2", "export"));userPages.add(new UserPage("U3", "/"));userPages.add(new UserPage("U3", "catalog"));userPages.add(new UserPage("U3", "edit"));}public void parseUserPage(){for(UserPage up : userPages){setUserPath(up);}}private void setUserPath(UserPage userPage){if(userPage == null)return;boolean isUserExist = false;for(UserPath up : userPaths){if(userPage.getUser().equals(up.getUser())){isUserExist = true;up.setPath(up.getPath() + "->" + userPage.getPage());break;}}if(!isUserExist){userPaths.add(new UserPath(userPage.getUser(), userPage.getPage()));}}public void setPathNCount(int n){if(n <= 0)return;for(UserPath up : userPaths){String[] paths = up.getPath().split("->");if(paths.length > n){setPathNCount(paths, n);}}}private void setPathNCount(String[] path, int n){if(n <= 0 || path == null || path.length < n)return;StringBuilder sb = new StringBuilder();for(int i = 0; i <= path.length-n; i++){for(int j = i; j < n+i; j++){if(sb.length() == 0)sb.append(path[j]);elsesb.append("->" + path[j]);}boolean isPathExist = false;for(PathCount pc : pathCounts){if(sb.toString().equals(pc.getPath())){isPathExist = true;pc.setCount(pc.getCount()+1);break;}}if(!isPathExist)pathCounts.add(new PathCount(sb.toString(), 1));sb.delete(0, sb.length());}}public void sortPathCounts(){Collections.sort(pathCounts, new Comparator<PathCount> (){public int compare(PathCount o1, PathCount o2){if(o1.getCount() > o2.getCount())return 1;return 0;}});}public List<PathCount> getTopXPath(int x){if (x < 0 || x > pathCounts.size())return null;sortPathCounts();List<PathCount> topX = new ArrayList<PathCount>();for(int i = 0; i < x; i++){topX.add(pathCounts.get(i));}return topX;}public void print(List<PathCount> pc){}/** * @param args */public static void main(String[] args) {Solution sl = new Solution();sl.initUserPages();sl.parseUserPage();sl.setPathNCount(3);List<PathCount> pc = sl.getTopXPath(2);sl.print(pc);}}

祝后续的同学们好运!

1 0
原创粉丝点击