|
36 | 36 | import java.util.function.UnaryOperator; |
37 | 37 |
|
38 | 38 | /** |
39 | | - * Underscore-java11 is a java 11 port of Underscore.js. |
| 39 | + * Underscore-java8 is a java 8 port of Underscore.js. |
40 | 40 | * |
41 | 41 | * @author Valentyn Kolesnikov |
42 | 42 | */ |
@@ -136,15 +136,16 @@ public String apply(Map<K, V> value) { |
136 | 136 | final String escape = TEMPLATE_SETTINGS.get(ESCAPE); |
137 | 137 | String result = template; |
138 | 138 | for (final Map.Entry<K, V> element : value.entrySet()) { |
| 139 | + final String value1 = String.valueOf(element.getValue()).replace("\\", "\\\\"); |
139 | 140 | result = java.util.regex.Pattern.compile(interpolate.replace(ALL_SYMBOLS, |
140 | 141 | S_Q + element.getKey() |
141 | | - + E_S)).matcher(result).replaceAll(String.valueOf(element.getValue())); |
| 142 | + + E_S)).matcher(result).replaceAll(value1); |
142 | 143 | result = java.util.regex.Pattern.compile(escape.replace(ALL_SYMBOLS, |
143 | 144 | S_Q + element.getKey() |
144 | | - + E_S)).matcher(result).replaceAll(escape(String.valueOf(element.getValue()))); |
| 145 | + + E_S)).matcher(result).replaceAll(escape(value1)); |
145 | 146 | result = java.util.regex.Pattern.compile(evaluate.replace(ALL_SYMBOLS, |
146 | 147 | S_Q + element.getKey() |
147 | | - + E_S)).matcher(result).replaceAll(String.valueOf(element.getValue())); |
| 148 | + + E_S)).matcher(result).replaceAll(value1); |
148 | 149 | } |
149 | 150 | return result; |
150 | 151 | } |
@@ -3748,8 +3749,113 @@ public boolean test(T value) { |
3748 | 3749 | }; |
3749 | 3750 | } |
3750 | 3751 |
|
| 3752 | + public static int minimumDays(int rows, int columns, List<List<Integer>> grid) { |
| 3753 | + Queue<int[]> queue = new LinkedList<int[]>(); |
| 3754 | + int cnt = 0; |
| 3755 | + for (int i = 0; i < rows; i++) { |
| 3756 | + for (int j = 0; j < columns; j++) { |
| 3757 | + if (grid.get(i).get(j) == 1) { |
| 3758 | + queue.offer(new int[] {i, j}); |
| 3759 | + cnt++; |
| 3760 | + } |
| 3761 | + } |
| 3762 | + } |
| 3763 | + return getInteger(rows, columns, grid, queue, cnt); |
| 3764 | + } |
| 3765 | + |
| 3766 | + private static int getInteger(int rows, int columns, List<List<Integer>> grid, Queue<int[]> queue, int cnt) { |
| 3767 | + int target = rows * columns; |
| 3768 | + int res = 0; |
| 3769 | + int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; |
| 3770 | + while (!queue.isEmpty()) { |
| 3771 | + int size = queue.size(); |
| 3772 | + if (cnt == target) { |
| 3773 | + return res; |
| 3774 | + } |
| 3775 | + for (int i = 0; i < size; i++) { |
| 3776 | + int[] cur = queue.poll(); |
| 3777 | + for (int[] dir : dirs) { |
| 3778 | + int ni = cur[0] + dir[0]; |
| 3779 | + int nj = cur[1] + dir[1]; |
| 3780 | + if (ni >= 0 && ni < rows && nj >= 0 && nj < columns && grid.get(ni).get(nj) == 0) { |
| 3781 | + cnt++; |
| 3782 | + queue.offer(new int[] {ni, nj}); |
| 3783 | + grid.get(ni).set(nj, 1); |
| 3784 | + } |
| 3785 | + } |
| 3786 | + } |
| 3787 | + res++; |
| 3788 | + } |
| 3789 | + return -1; |
| 3790 | + } |
| 3791 | + |
| 3792 | + public static List<String> topNCompetitors(int numCompetitors, int topNCompetitors, List<String> competitors, |
| 3793 | + int numReviews, List<String> reviews) { |
| 3794 | + if (U.isNull(reviews) || reviews.isEmpty() || U.isNull(competitors) || competitors.isEmpty() |
| 3795 | + || numReviews < 1 || numCompetitors < 1) { |
| 3796 | + return new ArrayList<String>(); |
| 3797 | + } |
| 3798 | + |
| 3799 | + List<String> topNCompetitorsList = new ArrayList<String>(topNCompetitors); |
| 3800 | + |
| 3801 | + Set<String> competitorsSet = new HashSet<String>(competitors); |
| 3802 | + Map<String, Integer> topCompetitorsMap = new HashMap<String, Integer>(); |
| 3803 | + List<Map.Entry<String, Integer>> list = getEntries(reviews, competitorsSet, topCompetitorsMap); |
| 3804 | + |
| 3805 | + for (Map.Entry<String, Integer> item : list) { |
| 3806 | + if (topNCompetitorsList.size() < topNCompetitors) { |
| 3807 | + topNCompetitorsList.add(item.getKey()); |
| 3808 | + } else { |
| 3809 | + break; |
| 3810 | + } |
| 3811 | + } |
| 3812 | + |
| 3813 | + return topNCompetitorsList; |
| 3814 | + } |
| 3815 | + |
| 3816 | + private static List<Map.Entry<String, Integer>> getEntries(List<String> reviews, Set<String> competitorsSet, |
| 3817 | + Map<String, Integer> topCompetitorsMap) { |
| 3818 | + // clean the reviews first: lowercase, remove special characters and split by spaces. |
| 3819 | + for (String review : reviews) { |
| 3820 | + String[] reviewArray = review.toLowerCase().replaceAll("[^a-zA-Z0-9 ]", "").split(" "); |
| 3821 | + Set<String> tempCompetitorSet = new HashSet<String>(); |
| 3822 | + |
| 3823 | + for (String text : reviewArray) { |
| 3824 | + if (competitorsSet.contains(text) && !tempCompetitorSet.contains(text)) { |
| 3825 | + tempCompetitorSet.add(text); |
| 3826 | + if (topCompetitorsMap.containsKey(text)) { |
| 3827 | + topCompetitorsMap.put(text, topCompetitorsMap.get(text) + 1); |
| 3828 | + } else { |
| 3829 | + topCompetitorsMap.put(text, 1); |
| 3830 | + } |
| 3831 | + } |
| 3832 | + } |
| 3833 | + } |
| 3834 | + |
| 3835 | + return getEntries(topCompetitorsMap); |
| 3836 | + } |
| 3837 | + |
| 3838 | + private static List<Map.Entry<String, Integer>> getEntries(Map<String, Integer> topCompetitorsMap) { |
| 3839 | + List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(topCompetitorsMap.entrySet()); |
| 3840 | + Collections.sort(list, new ValueThenKeyComparator<String, Integer>()); |
| 3841 | + return list; |
| 3842 | + } |
| 3843 | + |
| 3844 | + public static class ValueThenKeyComparator<K extends Comparable<? super K>, |
| 3845 | + V extends Comparable<? super V>> implements Comparator<Map.Entry<K, V>> { |
| 3846 | + |
| 3847 | + public int compare(Map.Entry<K, V> a, Map.Entry<K, V> b) { |
| 3848 | + int cmp1 = b.getValue().compareTo(a.getValue()); |
| 3849 | + if (cmp1 != 0) { |
| 3850 | + return cmp1; |
| 3851 | + } else { |
| 3852 | + return a.getKey().compareTo(b.getKey()); |
| 3853 | + } |
| 3854 | + } |
| 3855 | + } |
| 3856 | + |
3751 | 3857 | public static void main(String ... args) { |
3752 | | - final String message = "Underscore-java11 is a java 11 port of Underscore.js.\n\n" |
| 3858 | + final String message = "Underscore-java8 is a java 8 port of Underscore.js.\n\n" |
3753 | 3859 | + "In addition to porting Underscore's functionality, Underscore-java includes matching unit tests.\n\n" |
3754 | 3860 | + "For docs, license, tests, and downloads, see: http://javadev.github.io/underscore-java"; |
3755 | 3861 | System.out.println(message); |
|
0 commit comments