1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.file.FileVisitOption; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Arrays; import java.util.Enumeration; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.Stream.Builder; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import com.jaunt.NotFound; import com.jaunt.ResponseException; public class Toast { public static void main(String[] args) throws ResponseException, NotFound, IOException { Path root = Paths.get(""); Stream<Path> strm1 = Files.find(root.resolve("pens"), 5, (f,ab) -> ab.isRegularFile() && f.getFileName().toString().endsWith(".zip"), new FileVisitOption[0]); Stream<Path> strm2 = Files.find(root.resolve("pens_categorised"), 5, (f,ab) -> ab.isRegularFile() && f.getFileName().toString().endsWith(".zip"), new FileVisitOption[0]); int[] a = {0,0}; String list = Stream.concat(strm1, strm2).flatMap(file -> { try { ZipFile zipfile = new ZipFile(file.toFile()); Enumeration<? extends ZipEntry> entries = zipfile.entries(); final Pattern pattern = Pattern.compile("['\"](http?.+?)['\"]"); while(entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); if(!entry.isDirectory() && entry.getName().matches("(?i).+\\.(?:css|js|html)")) { a[0]++; InputStreamReader isr = new InputStreamReader(zipfile.getInputStream(entry), "utf-8"); BufferedReader reader = new BufferedReader(isr); return reader.lines() .flatMap(s1 -> { Matcher m = pattern.matcher(s1); Builder<String> b = Stream.builder(); while(m.find()) b.accept(m.group(1)); return b.build(); }).onClose(() -> { try { a[1]++; isr.close(); reader.close(); zipfile.close(); } catch (IOException e1) {} }); } } } catch (IOException e) { System.out.println(file+" "+e); } return Stream.empty(); }) .distinct() .collect(Collectors.joining("\n")); Files.write(Paths.get("a.txt"), list.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); System.out.println("DONE"+Arrays.toString(a)); } } |
Friday, 22 September 2017
java - RANDOM_CODE_2
this code extract string starting with http, from files stored in zip file.
Wednesday, 6 September 2017
java - star pyramid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | static void pyramid(int size){ char[] chars = new char[size*2]; Arrays.fill(chars, ' '); chars[size] = '*'; StringBuilder sb = new StringBuilder(); sb.append(chars).append('\n'); int index = size; while((index -= 2) > 0) { chars[index] = '*'; chars[size*2 - index] = '*'; sb.append(chars).append('\n'); } System.out.println(sb); } |
styled using hilite.me
java - is armstrong number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Arrays; import java.util.stream.IntStream; public class _Math { static int[] armstrong_number_generator(int min, int max){ return IntStream.rangeClosed(min, max).filter(_Math::is_armstrong_number).toArray(); } /** * * @param num * @return */ static boolean is_armstrong_number(int num ){ if(num < 10 && num >= 0) return true; int[] nums = digits_in_number(num); return num == IntStream.of(nums).mapToDouble(i -> Math.pow(i, nums.length)).sum(); } } |
styled using hilite.me
Tuesday, 5 September 2017
java - print number large (in ascii)
035 ##### ##### ##### # # # # # # # # # # ##### ##### # # # # # # # # ##### ##### #####
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | // Program that receives a number and prints it out in large size static void printNumberInAscii(String number) { System.out.println(number); System.out.println(); String[][] chars = { { "#####", "# #", "# #", "# #", "# #", "# #", "#####", }, { " # ", " ## ", " # ", " # ", " # ", " # ", "#####" }, { "#####", " #", " #", "#####", "# ", "# ", "#####" }, { "#####", " #", " #", "#####", " #", " #", "#####" }, { "# #", "# #", "# #", "#####", " #", " #", " #" }, { "#####", "# ", "# ", "#####", " #", " #", "#####" }, { "#####", "# ", "# ", "#####", "# #", "# #", "#####" }, { "#####", " #", " #", " #", " #", " #", " #" }, { "#####", "# #", "# #", "#####", "# #", "# #", "#####" }, { "#####", "# #", "# #", "#####", " #", " #", " #" } }; int[] numbers = number.chars().map(i -> i - (int)'0').toArray(); StringBuilder b = new StringBuilder(); for (int j2 = 0; j2 < chars[0].length; j2++) { for (int j : numbers) { b.append(chars[j][j2]).append(' ').append(' '); } b.append('\n'); } System.out.println(b); } |
styled using hilite.me
java - all subsequeces in a string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class _String { /** * Print Print all subsequences of a string * * http://www.geeksforgeeks.org/print-subsequences-string/ * */ public static List<String> all_subsequece_in_a_string(String str) { return powerSet(str.toCharArray()).stream() .filter(c -> c.length != 0) .map(String::new) .distinct() .collect(Collectors.toList()); } } |
styled using hilite.me
java - all substrings in a string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.ArrayList; import java.util.List; public class _String { /** * Print all substrings of a string */ public static List<String> all_substrings_in_a_string(String str) { List<String> list = new ArrayList<>(); char[] chars = str.toCharArray(); int size = 0; while(size++ < chars.length) { char[] c = new char[size]; for (int i = 0; i < chars.length; i++) { if(i + c.length <= chars.length) { for (int j = 0; j < c.length; j++) { c[j] = chars[i+j]; } list.add(new String(c)); } } } return list; } } |
java - all palindrome substrings in a string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public class _String { /** * Count All Palindrome Sub-Strings in a String * * http://www.geeksforgeeks.org/count-palindrome-sub-strings-string-set-2/ * * @param str * @return */ public static List<String> all_palindrome_substrings_in_a_string(String str){ return all_substrings_in_a_string(str).stream() .filter(s -> s.length() > 1 && s.length() < str.length() && is_palindrome(s)) .collect(Collectors.toList()); } } |
styled using hilite.me
java - reverse words of sentence
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; public class _String { static String reverse_words_of_sentence(String s){ Matcher m = Pattern.compile("(\\W+|_+)").matcher(s); ArrayList<String> list = new ArrayList<>(); int start = 0; while(m.find()){ list.add(0, s.substring(start, m.start())); list.add(0, m.group(1)); start = m.end(); } if(start < s.length()) list.add(0, s.substring(start)); return String.join("", list); } } |
styled using hilite.me
java - is string anagram ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Arrays; public class _String { static boolean is_anagram(String s1, String s2){ if(s1.length() != s2.length() || s1.equals(s2)) return false; char[] c1 = s1.toLowerCase().toCharArray(); char[] c2 = s2.toLowerCase().toCharArray(); Arrays.sort(c1); Arrays.sort(c2); return Arrays.equals(c1, c2); } } |
java - is string palindrome ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class _String { static boolean is_palindrome(String s){ char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) chars[i] = Character.toLowerCase(chars[i]); return is_palindrome(chars); } static boolean is_palindrome(char[] chars){ if(chars.length < 2) return true; int start = 0; int end = chars.length - 1; while(start <= end){ if(chars[start++] != chars[end--]) return false; } return true; } } |
styled using hilite.me
java - is leap year
1 2 3 4 5 6 7 8 9 10 | static boolean is_leap_year(int year) { if(year%4 != 0) return false; else if(year%100 != 0) return true; else if(year%400 != 0) return false; else return true; } |
java - sum of cubes of first n even/odd numbers
1 2 3 4 5 6 7 8 9 10 11 | import java.util.stream.LongStream; public class _Math2 { public static long sum_of_cubes_of_first_n_even_numbers(int n) { return (long) LongStream.iterate(2, i -> i + 2).limit(n).mapToDouble(i -> Math.pow(i, 3)).sum(); } public static long sum_of_cubes_of_first_n_odd_numbers(int n) { return (long) LongStream.iterate(1, i -> i + 2).limit(n).mapToDouble(i -> Math.pow(i, 3)).sum(); } } |
styled using hilite.me
java - Remove brackets from an algebraic string containing + and – operators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | public class _Math2 { /** * Remove brackets from an algebraic string containing + and – operators * * http://www.geeksforgeeks.org/remove-brackets-algebraic-string-containing-operators/ * * @param str */ public static String remove_brackets_from_algebric_equation(String str) { char[] chars = str.replaceAll("\\s+", "").toCharArray(); for (int i = 0; i < chars.length; i++) { if(chars[i] == '(') { findBracket(i,chars); break; } } return new String(chars).replaceAll("\\s+", ""); } private static void findBracket(int open, char[] chars) { for (int i = open + 1; i < chars.length; i++) { if(chars[i] == '(') findBracket(i, chars); else if(chars[i] == ')') { removeBrackets(chars, open, i); for (int j = 0; j < chars.length; j++) { if(chars[j] == '(') { findBracket(j , chars); break; } } } } } private static void removeBrackets(char[] chars, int open, int close) { char operator = '+'; for (int i = open - 1; i >= 0; i--) { if(chars[i] == '+' || chars[i] == '-') { operator = chars[i]; break; } } chars[open] = ' '; chars[close] = ' '; if(operator == '+') return; for (int i = open+1; i < chars.length; i++) { char c = chars[i]; chars[i] = c == '+' ? '-' : c == '-' ? '+' : c; } } } |
java - Find next greater number with same set of digits
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import java.util.Arrays; import java.util.stream.IntStream; import array._Array; public class _Math2 { /** * Find next greater number with same set of digits * * http://www.geeksforgeeks.org/find-next-greater-number-set-digits/ * */ static int next_greater_number_with_same_set_of_digits(long num) { System.out.println(num); int zeroChar = (int)'0'; int array[] = String.valueOf(num).chars().map(i -> i - zeroChar).toArray(); int[] temp = Arrays.copyOf(array, array.length); Arrays.sort(temp); //check if array in increasing order if(Arrays.equals(array, temp)) { _Array.swap(array, array.length - 2, array.length - 1); return combineNumber(array); } _Array.reverse(temp); //check if num is max possible number is this set of digits if(Arrays.equals(array, temp)) throw new Error("Not Possible"); temp = Arrays.copyOf(array, array.length); //sort except first number Arrays.sort(temp, 1, array.length); //find first digit which does not match with original numbers digit in same index int start = 1; while(array[start] == temp[start]) start++; //swap it _Array.swap(temp, start - 1, start); return combineNumber(temp); } public static int combineNumber(int[] array) { int sum = 0; for (int j = 0; j < array.length; j++) sum += array[j]*Math.pow(10, array.length - j - 1); return sum; } } |
styled using hilite.me
java - factorial of a number
1 2 3 4 5 6 7 8 9 10 11 | import java.util.stream.IntStream; public class _Math { static int factorial(int num){ if(num == 0 || num == 1) return 1; return IntStream.rangeClosed(1, num).reduce((a,b) -> a*b).getAsInt(); } } |
styled using hilite.me
java - is a prime number ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import java.util.stream.IntStream; public class _Math { static int[] prime_generator(int size){ // return IntStream.iterate(2, a -> a+1).filter(InterviewQuestions::is_prime_method_1).limit(size).toArray(); int array[] = new int[size]; if(size == 0) return array; array[0] = 2; if(size == 1) return array; array[1] = 3; if(size == 2) return array; // all other than 2, 3 can is in form of 6k ± 1 for (int i = 0, index = 2; index < array.length; i++) { int n1 = 6 * i; if(is_prime(n1 - 1)) array[index++] = n1 - 1; if(is_prime(n1 + 1)) array[index++] = n1 + 1; } return array; } /** * https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes * @param upto * @return */ static int[] prime_generator_by_sieve_of_eratosthenes(int upto){ int[] base = IntStream.range(0, upto+1).toArray(); base[0] = base[1] = 0; for (int i = 2; i < base.length; i++) { if(base[i] == 0) continue; for (int j = 2; i*j < base.length; j++) { base[i*j] = 0; } } return IntStream.of(base).filter(i -> i != 0).toArray(); } /** * https://en.wikipedia.org/wiki/Primality_test * @param num * @return */ static boolean is_prime(int num){ if(num < 2) return false; if(num == 2 || num == 3) return true; if(num%2 == 0) return false; // max number which is divide a number is its sqrt, after that is can only divided by itself int max = (int) Math.sqrt(num); for (int i = 3; i <= max; i+=2) if(num%i == 0) return false; return true; } } |
styled using hilite.me
Subscribe to:
Comments (Atom)