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);
       }
    }
  • 1
    import java.util.stream.*;
    
    1+
    import static java.util.stream.IntStream.of;
    
    22
    33
    public class Average {
    
    44
       public static int averageFinder(int[] arr) {
    
    5
         return (int)IntStream.of(arr).average().orElse(0);
    
    5+
         return (int)of(arr).average().orElse(0);
    
    66
       }
    
    77
    }
    
Code
Diff
  • import java.util.stream.*;
    
    public class Average {
       public static int averageFinder(int[] arr) {
         return (int)IntStream.of(arr).average().orElse(0);
       }
    }
  • 1
    import java.util.*;
    
    1+
    import java.util.stream.*;
    
    2+
    22
    public class Average {
    
    33
       public static int averageFinder(int[] arr) {
    
    4
         return  (int)Arrays.stream(arr).average().orElse(0d);
    
    5
       };
    
    5+
         return (int)IntStream.of(arr).average().orElse(0);
    
    6+
       }
    
    66
    }
    
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));
        }
    }
  • 11
    import java.util.*;
    
    2+
    22
    class Solution {
    
    3
      
    
    4
    	   
    
    5
         public static int retSmallestPositiveInteger() {
    
    6
             for(int i=1; ; i++) {
    
    7
                if(hasSameDigits(i, i*2) && hasSameDigits(i, i*3) && hasSameDigits(i, i*4) && hasSameDigits(i, i*5) && hasSameDigits(i, i*6))
    
    8
                   return i; 
    
    9
             }
    
    10
         }
    
    11
      
    
    12
    	    private static boolean hasSameDigits(int x, int y) {
    
    13
    		      char[] xdigits = Integer.toString(x).toCharArray();
    
    14
    		      char[] ydigits = Integer.toString(y).toCharArray();
    
    15
    		      Arrays.sort(xdigits);
    
    16
    		      Arrays.sort(ydigits);
    
    17
    		      return Arrays.equals(xdigits, ydigits);
    
    4+
      	   
    
    5+
        public static int retSmallestPositiveInteger() {       
    
    6+
          for (int i=1; ; i++) {
    
    7+
            final char[] d = digits(i);
    
    8+
            if (hasSameDigits(d,i*2) && hasSameDigits(d,i*3) && hasSameDigits(d,i*4) && hasSameDigits(d,i*5) && hasSameDigits(d,i*6)) {
    
    9+
              return i; 
    
    10+
            }
    
    1818
          }
    
    12+
        }
    
    13+
         
    
    14+
        private static char[] digits(final int x) {
    
    15+
          final char[] d = (""+x).toCharArray();
    
    16+
          Arrays.sort(d);
    
    17+
          return d;
    
    18+
        }
    
    19+
      
    
    20+
        private static boolean hasSameDigits(final char[] xdigits, final int y) {
    
    21+
    		  return Arrays.equals(xdigits, digits(y));
    
    22+
        }
    
    1919
    }
    
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;
      }
    
    }
  • 11
    public class Dinglemouse {
    
    22
    33
      // Use CSS to display a pretty version of the flap display
    
    44
      // From: https://www.codewars.com/kata/airport-arrivals-slash-departures-number-1/java
    
    55
      public static String[] prettyPrint(final String[] lines) {
    
    6
        String s = "<pre>";
    
    6+
        String s = "";
    
    77
        for (int y = 0; y < lines.length; y++) {
    
    88
          s += "<div style=\"height:23px\">";
    
    99
          for (int x = 0; x < lines[y].length(); x++) {
    
    1010
            s += "<span style=\"font-size:10px;color:yellow;padding:5px;border:1px solid gray;background:black\">"+lines[y].charAt(x)+"</span>";
    
    1111
          }
    
    1212
          s += "</div>";
    
    1313
        }
    
    14
        s+= "</pre>";
    
    1515
        System.out.println(s);
    
    1616
        return lines;
    
    1717
      }
    
    1818
    1919
    }
    

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;
    	}
    	
    }
    
  • 11
    package com.mystuff.juststuff;
    
    22
    33
    import java.text.*;
    
    4
    import java.util.Calendar;
    
    5
    import java.util.Date;
    
    6
    import java.util.GregorianCalendar;
    
    7
    import java.util.TreeSet;
    
    4+
    import java.util.*;
    
    88
    99
    public class Palindrome {
    
    10
    	SimpleDateFormat sdf;
    
    11
    	
    
    12
    	public Palindrome() {
    
    13
    		this.sdf = new SimpleDateFormat("MMddyyyy");
    
    14
    		sdf.setLenient(false);
    
    15
    	}
    
    16
    17
    	public TreeSet<Date> countDatePalindromes(Date startDate, Date endDate) {
    
    18
    		TreeSet<Date> palSet = new TreeSet<Date>();
    
    19
    		GregorianCalendar startCal = new GregorianCalendar();
    
    20
    		GregorianCalendar endCal = new GregorianCalendar();
    
    2121
    8+
    	private static final SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
    
    2222
    23
        /*
    
    24
          end date is chronologically before start date. flip them.
    
    25
          NOTE: lines 23-30 should be a pre-condition to the countDatePalindromes method,
    
    26
          so they should be deleted.
    
    27
        */
    
    28
    		if (startDate.getTime() > endDate.getTime()) {
    
    43
    			if (regularDate.equals(palDate))
    
    44
    				palSet.add(startCal.getTime());
    
    45
            
    
    46
    			startCal.add(Calendar.DAY_OF_MONTH, 1);
    
    10+
    	public Set<Date> countDatePalindromes(final Date startDate, final Date endDate) {
    
    11+
    		final Set<Date> set = new TreeSet<>();
    
    12+
    		final GregorianCalendar startCal = new GregorianCalendar();
    
    13+
    		final GregorianCalendar endCal = new GregorianCalendar();
    
    14+
    15+
        // If end date is before start date, flip them.
    
    16+
        startCal.setTime(startDate.before(endDate) ? startDate : endDate);
    
    17+
        endCal.setTime(startDate.after(endDate) ? startDate : endDate);
    
    18+
        
    
    19+
    		for (; startCal.before(endCal) || startCal.equals(endCal); startCal.add(Calendar.DAY_OF_MONTH, 1)) {   
    
    20+
    			final String forward = sdf.format(startCal.getTime());
    
    21+
          final String back = new StringBuilder(forward).reverse().toString();            
    
    22+
    			if (forward.equals(back)) {
    
    23+
            set.add(startCal.getTime()); // Date string looks same both ways	
    
    24+
          }
    
    4747
    		}	
    
    48
    		return palSet;
    
    26+
        
    
    27+
    		return set;
    
    4949
    	}
    
    5050
    	
    
    51
    	private String palindromify(final Date arg) {
    
    52
    		StringBuffer palBuff = new StringBuffer(8);
    
    53
        palBuff = sdf.format(arg, palBuff, new FieldPosition(0));
    
    54
    		return palBuff.reverse().toString();
    
    55
    	}
    
    56
    5757
    }
    
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)
    }
  • 11
    function getFactors (n) {
    
    22
      if (!Number.isInteger(n) || n < 1) return [];
    
    33
      
    
    4
      var divisors = new Set();  
    
    5
      for (let i = 1; i <= Math.sqrt(n); i++) {
    
    4+
      var divisors = new Set(); 
    
    5+
      var step = n % 2 == 0 ? 1 : 2;
    
    6+
      for (let i = 1; i <= Math.sqrt(n); i += step) {
    
    66
        if (n % i == 0) divisors.add(i).add(n/i)
    
    77
      }
    
    8
      return Array.from(divisors)
    
    9+
      return Array.from(divisors).sort((a,b)=>a-b)
    
    99
    }
    
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)
    }
  • 11
    function getFactors (n) {
    
    2
      if (n < 1 || ! Number.isInteger(n)) return [];
    
    2+
      if (!Number.isInteger(n) || n < 1) return [];
    
    33
      
    
    4
      let divisors = [];
    
    5
      
    
    6
      for (let i = 1; i <= Math.sqrt(n); ++i) {
    
    7
        if (n % i === 0) {
    
    8
            divisors.push(i);
    
    9
            if (n / i !== i) divisors.push(n / i);
    
    10
        }
    
    4+
      var divisors = new Set();  
    
    5+
      for (let i = 1; i <= Math.sqrt(n); i++) {
    
    6+
        if (n % i == 0) divisors.add(i).add(n/i)
    
    1111
      }
    
    12
      
    
    13
      return divisors;
    
    8+
      return Array.from(divisors)
    
    1414
    }
    
Stacks
Arrays
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();
      }
    }
  • 11
    import java.util.*;
    
    22
    33
    public class MatchingBrackets {
    
    44
    5
      public static boolean isBalanced(String braces) {
    
    6
            Stack<String> stack = new Stack<>();
    
    7
            Map<String, String> enclosing = new HashMap<>();
    
    8
            enclosing.put(")","(");
    
    9
            enclosing.put("}","{");
    
    10
            enclosing.put("]","[");
    
    11
            for (char brace : braces.toCharArray()) {
    
    12
                final String strBrace = Character.toString(brace);
    
    13
                if (!stack.isEmpty() && stack.peek().equals(enclosing.get(strBrace))) {
    
    14
                    stack.pop();
    
    15
                } else {
    
    16
                    stack.push(strBrace);
    
    17
                }
    
    18
            }
    
    19
            return stack.isEmpty();
    
    5+
      static final Map<Character, Character> brackets = new HashMap<Character, Character>() {{
    
    6+
        put(')','(');
    
    7+
        put('}','{');
    
    8+
        put(']','[');
    
    9+
      }};
    
    10+
      
    
    11+
      public static boolean isBalanced(final String braces) {
    
    12+
        final Stack<Character> stack = new Stack<>();
    
    13+
        for (final char brace : braces.toCharArray()) {  
    
    14+
          if (!stack.isEmpty() && stack.peek() == brackets.get(brace)) {
    
    15+
            stack.pop();
    
    16+
          } else {
    
    17+
            stack.push(brace);
    
    18+
          }
    
    2020
        }
    
    20+
        return stack.isEmpty();
    
    21+
      }
    
    2121
    }
    
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());
      }
    }
  • 1
    import java.util.*;
    
    1+
    import java.util.stream.*;
    
    22
    33
    class Solution {
    
    44
    55
      public static String largestNumber(final Integer[] nums) {
    
    6
      
    
    7
        Arrays.sort(nums, (a,b) -> (""+b+a).compareTo(""+a+b) );
    
    8
        
    
    9
        String result = "";
    
    10
        for (final Integer num : nums) {
    
    11
          result += num;
    
    12
        }
    
    13
        
    
    14
        return result;
    
    6+
        return Stream.of(nums)
    
    7+
          .sorted((a,b)->(""+b+a).compareTo(""+a+b))
    
    8+
          .map(n->""+n)
    
    9+
          .collect(Collectors.joining());
    
    1515
      }
    
    1616
    }
    
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;
      }
    }
  • 11
    import java.util.*;
    
    22
    33
    class Solution {
    
    4
      public static String largestNumber(Integer[] nums) {
    
    5
        Arrays.sort( nums, new Comparator<Integer>() {
    
    4+
    5+
      public static String largestNumber(final Integer[] nums) {
    
    6+
      
    
    7+
        Arrays.sort(nums, new Comparator<Integer>() {
    
    66
          
    
    77
          @Override
    
    8
          public int compare(Integer a, Integer b) {
    
    9
            String aStr = a.toString();
    
    10
            String bStr = b.toString();
    
    11
            
    
    12
            return (aStr + bStr).compareTo(bStr + aStr) * -1;
    
    10+
          public int compare(final Integer a, final Integer b) {        
    
    11+
            return (""+b+a).compareTo(""+a+b);
    
    1313
          }
    
    1414
          
    
    15
        } );
    
    14+
        });
    
    1616
        
    
    1717
        String result = "";
    
    18
        for(Integer num : nums) {
    
    19
          result += num.toString();
    
    17+
        for (final Integer num : nums) {
    
    18+
          result += num;
    
    2020
        }
    
    2121
        
    
    2222
        return result;
    
    2323
      }
    
    2424
    }