Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Display Page
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static List<String> displayPages2(List<String> input, int pageSZ) { List<String> res = new ArrayList<String>(); if(input == null || input.size() == 0) return res; Iterator<String> iter = input.iterator(); int cnt = 0; boolean end = false; Set<String> hosts = new HashSet<String>(); while(iter.hasNext()) { String cur = iter.next(); String host = cur.split(",")[0]; if(cnt < pageSZ && !hosts.contains(host) || end) { res.add(cur); hosts.add(host); iter.remove(); cnt++; iter = input.iterator(); } if(cnt == pageSZ) { if(!input.isEmpty()) res.add(" "); hosts.clear(); iter = input.iterator(); cnt = 0; end = false; } if(!iter.hasNext()) { hosts.clear(); iter = input.iterator(); end = true; } } return res; } public static void main(String args[]) { //hostId, ListId, points, city List<String> input = new ArrayList<>(); input.add("1,28,310.6,SF"); input.add("1,5,204.1,SF"); input.add("20,7,203.2,Oakland"); input.add("6,8,202.2,SF"); input.add("6,10,199.1,SF"); input.add("1,16,190.4,SF"); input.add("6,29,185.2,SF"); input.add("7,20,180.1,SF"); input.add("6,21,162.1,SF"); input.add("2,18,161.2,SF"); input.add("2,30,149.1,SF"); input.add("3,76,146.2,SF"); input.add("2,14,141.1,San Jose"); List<String> result = displayPages2(input, 5); for (int i = 0; i < result.size(); i++) { System.out.println(result.get(i)); } System.out.println("test case1 ... \n"); String[] strs = new String[]{ "1,28,300.1,SanFrancisco", "4,5,209.1,SanFrancisco", "20,7,208.1,SanFrancisco", "23,8,207.1,SanFrancisco", "16,10,206.1,Oakland", "1,16,205.1,SanFrancisco", "6,29,204.1,SanFrancisco", "7,20,203.1,SanFrancisco", "8,21,202.1,SanFrancisco", "2,18,201.1,SanFrancisco", "2,30,200.1,SanFrancisco", "15,27,109.1,Oakland", "10,13,108.1,Oakland", "11,26,107.1,Oakland", "12,9,106.1,Oakland", "13,1,105.1,Oakland", "22,17,104.1,Oakland", "1,2,103.1,Oakland", "28,24,102.1,Oakland", "18,14,11.1,SanJose", "6,25,10.1,Oakland", "19,15,9.1,SanJose", "3,19,8.1,SanJose", "3,11,7.1,Oakland", "27,12,6.1,Oakland", "1,3,5.1,Oakland", "25,4,4.1,SanJose", "5,6,3.1,SanJose", "29,22,2.1,SanJose", "30,23,1.1,SanJose" }; input = new ArrayList<>(Arrays.asList(strs)); result = displayPages2(input, 12); assert(32 == (result.size())); assert("1,28,300.1,SanFrancisco".equals(result.get(0))); assert("11,26,107.1,Oakland".equals(result.get(11))); assert(" ".equals(result.get(12))); assert("1,16,205.1,SanFrancisco".equals(result.get(13))); assert("2,30,200.1,SanFrancisco".equals(result.get(14))); assert("25,4,4.1,SanJose".equals(result.get(24))); assert(" ".equals(result.get(25))); assert("1,2,103.1,Oakland".equals(result.get(26))); assert("3,11,7.1,Oakland".equals(result.get(27))); assert("30,23,1.1,SanJose".equals(result.get(30))); assert("1,3,5.1,Oakland".equals(result.get(31))); for (int i = 0; i < result.size(); i++) { System.out.println(result.get(i)); } } }
run
|
edit
|
history
|
help
0
Minimo quadrato somma di cubi
4.C
Shortest distance between words
Hello world
PE #5
Print Longest String
Print Count Strings
Problem: Single_digit
1a
1