Ad
Code
Diff
  • import static java.util.stream.IntStream.of;
    
    public class Average {
       public static int averageFinder(int[] arr) {
         return (int)of(arr).average().orElse(0);
       }
    }
    • import java.util.stream.*;
    • import static java.util.stream.IntStream.of;
    • public class Average {
    • public static int averageFinder(int[] arr) {
    • return (int)IntStream.of(arr).average().orElse(0);
    • return (int)of(arr).average().orElse(0);
    • }
    • }
Code
Diff
  • import java.util.stream.*;
    
    public class Average {
       public static int averageFinder(int[] arr) {
         return (int)IntStream.of(arr).average().orElse(0);
       }
    }
    • import java.util.*;
    • import java.util.stream.*;
    • public class Average {
    • public static int averageFinder(int[] arr) {
    • return (int)Arrays.stream(arr).average().orElse(0d);
    • };
    • return (int)IntStream.of(arr).average().orElse(0);
    • }
    • }
Code
Diff
  • import java.util.*;
    
    class Solution {
      	   
        public static int retSmallestPositiveInteger() {       
          for (int i=1; ; i++) {
            final char[] d = digits(i);
            if (hasSameDigits(d,i*2) && hasSameDigits(d,i*3) && hasSameDigits(d,i*4) && hasSameDigits(d,i*5) && hasSameDigits(d,i*6)) {
              return i; 
            }
          }
        }
         
        private static char[] digits(final int x) {
          final char[] d = (""+x).toCharArray();
          Arrays.sort(d);
          return d;
        }
      
        private static boolean hasSameDigits(final char[] xdigits, final int y) {
    		  return Arrays.equals(xdigits, digits(y));
        }
    }
    • import java.util.*;
    • class Solution {
    • public static int retSmallestPositiveInteger() {
    • for(int i=1; ; i++) {
    • if(hasSameDigits(i, i*2) && hasSameDigits(i, i*3) && hasSameDigits(i, i*4) && hasSameDigits(i, i*5) && hasSameDigits(i, i*6))
    • return i;
    • }
    • }
    • private static boolean hasSameDigits(int x, int y) {
    • char[] xdigits = Integer.toString(x).toCharArray();
    • char[] ydigits = Integer.toString(y).toCharArray();
    • Arrays.sort(xdigits);
    • Arrays.sort(ydigits);
    • return Arrays.equals(xdigits, ydigits);
    • public static int retSmallestPositiveInteger() {
    • for (int i=1; ; i++) {
    • final char[] d = digits(i);
    • if (hasSameDigits(d,i*2) && hasSameDigits(d,i*3) && hasSameDigits(d,i*4) && hasSameDigits(d,i*5) && hasSameDigits(d,i*6)) {
    • return i;
    • }
    • }
    • }
    • private static char[] digits(final int x) {
    • final char[] d = (""+x).toCharArray();
    • Arrays.sort(d);
    • return d;
    • }
    • private static boolean hasSameDigits(final char[] xdigits, final int y) {
    • return Arrays.equals(xdigits, digits(y));
    • }
    • }
Code
Diff
  • public class Dinglemouse {
    
      // Use CSS to display a pretty version of the flap display
      // From: https://www.codewars.com/kata/airport-arrivals-slash-departures-number-1/java
      public static String[] prettyPrint(final String[] lines) {
        String s = "";
        for (int y = 0; y < lines.length; y++) {
          s += "<div style=\"height:23px\">";
          for (int x = 0; x < lines[y].length(); x++) {
            s += "<span style=\"font-size:10px;color:yellow;padding:5px;border:1px solid gray;background:black\">"+lines[y].charAt(x)+"</span>";
          }
          s += "</div>";
        }
        System.out.println(s);
        return lines;
      }
    
    }
    • public class Dinglemouse {
    • // Use CSS to display a pretty version of the flap display
    • // From: https://www.codewars.com/kata/airport-arrivals-slash-departures-number-1/java
    • public static String[] prettyPrint(final String[] lines) {
    • String s = "<pre>";
    • String s = "";
    • for (int y = 0; y < lines.length; y++) {
    • s += "<div style=\"height:23px\">";
    • for (int x = 0; x < lines[y].length(); x++) {
    • s += "<span style=\"font-size:10px;color:yellow;padding:5px;border:1px solid gray;background:black\">"+lines[y].charAt(x)+"</span>";
    • }
    • s += "</div>";
    • }
    • s+= "</pre>";
    • System.out.println(s);
    • return lines;
    • }
    • }

This is my Pretty Print code for the Airport Arrivals/Departures flap display

Ref: https://www.codewars.com/kata/airport-arrivals-slash-departures-number-1/java

public class Dinglemouse {

  // Use CSS to display a pretty version of the flap display
  // From: https://www.codewars.com/kata/airport-arrivals-slash-departures-number-1/java
  public static String[] prettyPrint(final String[] lines) {
    String s = "<pre>";
    for (int y = 0; y < lines.length; y++) {
      s += "<div style=\"height:23px\">";
      for (int x = 0; x < lines[y].length(); x++) {
        s += "<span style=\"font-size:10px;color:yellow;padding:5px;border:1px solid gray;background:black\">"+lines[y].charAt(x)+"</span>";
      }
      s += "</div>";
    }
    s+= "</pre>";
    System.out.println(s);
    return lines;
  }

}
Code
Diff
  • package com.mystuff.juststuff;
    
    import java.text.*;
    import java.util.*;
    
    public class Palindrome {
    
    	private static final SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
    
    	public Set<Date> countDatePalindromes(final Date startDate, final Date endDate) {
    		final Set<Date> set = new TreeSet<>();
    		final GregorianCalendar startCal = new GregorianCalendar();
    		final GregorianCalendar endCal = new GregorianCalendar();
    
        // If end date is before start date, flip them.
        startCal.setTime(startDate.before(endDate) ? startDate : endDate);
        endCal.setTime(startDate.after(endDate) ? startDate : endDate);
        
    		for (; startCal.before(endCal) || startCal.equals(endCal); startCal.add(Calendar.DAY_OF_MONTH, 1)) {   
    			final String forward = sdf.format(startCal.getTime());
          final String back = new StringBuilder(forward).reverse().toString();            
    			if (forward.equals(back)) {
            set.add(startCal.getTime()); // Date string looks same both ways	
          }
    		}	
        
    		return set;
    	}
    	
    }
    
    • package com.mystuff.juststuff;
    • import java.text.*;
    • import java.util.Calendar;
    • import java.util.Date;
    • import java.util.GregorianCalendar;
    • import java.util.TreeSet;
    • import java.util.*;
    • public class Palindrome {
    • SimpleDateFormat sdf;
    • public Palindrome() {
    • this.sdf = new SimpleDateFormat("MMddyyyy");
    • sdf.setLenient(false);
    • }
    • public TreeSet<Date> countDatePalindromes(Date startDate, Date endDate) {
    • TreeSet<Date> palSet = new TreeSet<Date>();
    • GregorianCalendar startCal = new GregorianCalendar();
    • GregorianCalendar endCal = new GregorianCalendar();
    • private static final SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
    • /*
    • end date is chronologically before start date. flip them.
    • NOTE: lines 23-30 should be a pre-condition to the countDatePalindromes method,
    • so they should be deleted.
    • */
    • if (startDate.getTime() > endDate.getTime()) {
    • startCal.setTime(endDate);
    • endCal.setTime(startDate);
    • }else {
    • startCal.setTime(startDate);
    • endCal.setTime(endDate);
    • }
    • String regularDate = null;
    • String palDate = null;
    • while ( (startCal.before(endCal)) || (startCal.equals(endCal)) ) {
    • regularDate = sdf.format(startCal.getTime());
    • palDate = palindromify(startCal.getTime());
    • // a date palindrome was found
    • if (regularDate.equals(palDate))
    • palSet.add(startCal.getTime());
    • startCal.add(Calendar.DAY_OF_MONTH, 1);
    • public Set<Date> countDatePalindromes(final Date startDate, final Date endDate) {
    • final Set<Date> set = new TreeSet<>();
    • final GregorianCalendar startCal = new GregorianCalendar();
    • final GregorianCalendar endCal = new GregorianCalendar();
    • // If end date is before start date, flip them.
    • startCal.setTime(startDate.before(endDate) ? startDate : endDate);
    • endCal.setTime(startDate.after(endDate) ? startDate : endDate);
    • for (; startCal.before(endCal) || startCal.equals(endCal); startCal.add(Calendar.DAY_OF_MONTH, 1)) {
    • final String forward = sdf.format(startCal.getTime());
    • final String back = new StringBuilder(forward).reverse().toString();
    • if (forward.equals(back)) {
    • set.add(startCal.getTime()); // Date string looks same both ways
    • }
    • }
    • return palSet;
    • return set;
    • }
    • private String palindromify(final Date arg) {
    • StringBuffer palBuff = new StringBuffer(8);
    • palBuff = sdf.format(arg, palBuff, new FieldPosition(0));
    • return palBuff.reverse().toString();
    • }
    • }
Code
Diff
  • function getFactors (n) {
      if (!Number.isInteger(n) || n < 1) return [];
      
      var divisors = new Set(); 
      var step = n % 2 == 0 ? 1 : 2;
      for (let i = 1; i <= Math.sqrt(n); i += step) {
        if (n % i == 0) divisors.add(i).add(n/i)
      }
      return Array.from(divisors).sort((a,b)=>a-b)
    }
    • function getFactors (n) {
    • if (!Number.isInteger(n) || n < 1) return [];
    • var divisors = new Set();
    • for (let i = 1; i <= Math.sqrt(n); i++) {
    • var divisors = new Set();
    • var step = n % 2 == 0 ? 1 : 2;
    • for (let i = 1; i <= Math.sqrt(n); i += step) {
    • if (n % i == 0) divisors.add(i).add(n/i)
    • }
    • return Array.from(divisors)
    • return Array.from(divisors).sort((a,b)=>a-b)
    • }
Code
Diff
  • function getFactors (n) {
      if (!Number.isInteger(n) || n < 1) return [];
      
      var divisors = new Set();  
      for (let i = 1; i <= Math.sqrt(n); i++) {
        if (n % i == 0) divisors.add(i).add(n/i)
      }
      return Array.from(divisors)
    }
    • function getFactors (n) {
    • if (n < 1 || ! Number.isInteger(n)) return [];
    • if (!Number.isInteger(n) || n < 1) return [];
    • let divisors = [];
    • for (let i = 1; i <= Math.sqrt(n); ++i) {
    • if (n % i === 0) {
    • divisors.push(i);
    • if (n / i !== i) divisors.push(n / i);
    • }
    • var divisors = new Set();
    • for (let i = 1; i <= Math.sqrt(n); i++) {
    • if (n % i == 0) divisors.add(i).add(n/i)
    • }
    • return divisors;
    • return Array.from(divisors)
    • }
Stacks
Arrays
Data Types
Code
Diff
  • import java.util.*;
    
    public class MatchingBrackets {
    
      static final Map<Character, Character> brackets = new HashMap<Character, Character>() {{
        put(')','(');
        put('}','{');
        put(']','[');
      }};
      
      public static boolean isBalanced(final String braces) {
        final Stack<Character> stack = new Stack<>();
        for (final char brace : braces.toCharArray()) {  
          if (!stack.isEmpty() && stack.peek() == brackets.get(brace)) {
            stack.pop();
          } else {
            stack.push(brace);
          }
        }
        return stack.isEmpty();
      }
    }
    • import java.util.*;
    • public class MatchingBrackets {
    • public static boolean isBalanced(String braces) {
    • Stack<String> stack = new Stack<>();
    • Map<String, String> enclosing = new HashMap<>();
    • enclosing.put(")","(");
    • enclosing.put("}","{");
    • enclosing.put("]","[");
    • for (char brace : braces.toCharArray()) {
    • final String strBrace = Character.toString(brace);
    • if (!stack.isEmpty() && stack.peek().equals(enclosing.get(strBrace))) {
    • stack.pop();
    • } else {
    • stack.push(strBrace);
    • }
    • }
    • return stack.isEmpty();
    • static final Map<Character, Character> brackets = new HashMap<Character, Character>() {{
    • put(')','(');
    • put('}','{');
    • put(']','[');
    • }};
    • public static boolean isBalanced(final String braces) {
    • final Stack<Character> stack = new Stack<>();
    • for (final char brace : braces.toCharArray()) {
    • if (!stack.isEmpty() && stack.peek() == brackets.get(brace)) {
    • stack.pop();
    • } else {
    • stack.push(brace);
    • }
    • }
    • return stack.isEmpty();
    • }
    • }
Code
Diff
  • import java.util.stream.*;
    
    class Solution {
    
      public static String largestNumber(final Integer[] nums) {
        return Stream.of(nums)
          .sorted((a,b)->(""+b+a).compareTo(""+a+b))
          .map(n->""+n)
          .collect(Collectors.joining());
      }
    }
    • import java.util.*;
    • import java.util.stream.*;
    • class Solution {
    • public static String largestNumber(final Integer[] nums) {
    • Arrays.sort(nums, (a,b) -> (""+b+a).compareTo(""+a+b) );
    • String result = "";
    • for (final Integer num : nums) {
    • result += num;
    • }
    • return result;
    • return Stream.of(nums)
    • .sorted((a,b)->(""+b+a).compareTo(""+a+b))
    • .map(n->""+n)
    • .collect(Collectors.joining());
    • }
    • }
Code
Diff
  • import java.util.*;
    
    class Solution {
    
      public static String largestNumber(final Integer[] nums) {
      
        Arrays.sort(nums, new Comparator<Integer>() {
          
          @Override
          public int compare(final Integer a, final Integer b) {        
            return (""+b+a).compareTo(""+a+b);
          }
          
        });
        
        String result = "";
        for (final Integer num : nums) {
          result += num;
        }
        
        return result;
      }
    }
    • import java.util.*;
    • class Solution {
    • public static String largestNumber(Integer[] nums) {
    • Arrays.sort( nums, new Comparator<Integer>() {
    • public static String largestNumber(final Integer[] nums) {
    • Arrays.sort(nums, new Comparator<Integer>() {
    • @Override
    • public int compare(Integer a, Integer b) {
    • String aStr = a.toString();
    • String bStr = b.toString();
    • return (aStr + bStr).compareTo(bStr + aStr) * -1;
    • public int compare(final Integer a, final Integer b) {
    • return (""+b+a).compareTo(""+a+b);
    • }
    • } );
    • });
    • String result = "";
    • for(Integer num : nums) {
    • result += num.toString();
    • for (final Integer num : nums) {
    • result += num;
    • }
    • return result;
    • }
    • }