Skip to main content
Commonmark migration
Source Link

#Java 10, 145 129 107 106 103 93 77 bytes

Java 10, 145 129 107 106 103 93 77 bytes

Loads of bytes saved thanks to @Frozn, and by using Object as return-type, we can comply with OP's rules of returning the input-array when the array is empty, despite Java's one-return-type-only nature regarding methods.

m->{int f=1;for(var a:m)f=m[0].length!=a.length?0:f;return m.length<1?m:f>0;}

Try it online.

Explanation:

m->{                  // Method with integer-matrix parameter and Object return-type
  int f=1;            //  Flag-integer, starting at 1
  for(var a:m)        //  Loop over the rows of the matrix
    f=m[0].length!=a.length?
                      //   If the length of the first and current row aren't the same:
       0              //    Change `f` to 0
      :               //   Else:
       f;             //    Leave `f` the same
  return m.length<1?  //  If there is only one row:
          m           //   Return the input-matrix as result
         :            //  Else:
          f>0;}       //   Return whether the flag `f` is still 1 as boolean

#Java 10, 145 129 107 106 103 93 77 bytes

Loads of bytes saved thanks to @Frozn, and by using Object as return-type, we can comply with OP's rules of returning the input-array when the array is empty, despite Java's one-return-type-only nature regarding methods.

m->{int f=1;for(var a:m)f=m[0].length!=a.length?0:f;return m.length<1?m:f>0;}

Try it online.

Explanation:

m->{                  // Method with integer-matrix parameter and Object return-type
  int f=1;            //  Flag-integer, starting at 1
  for(var a:m)        //  Loop over the rows of the matrix
    f=m[0].length!=a.length?
                      //   If the length of the first and current row aren't the same:
       0              //    Change `f` to 0
      :               //   Else:
       f;             //    Leave `f` the same
  return m.length<1?  //  If there is only one row:
          m           //   Return the input-matrix as result
         :            //  Else:
          f>0;}       //   Return whether the flag `f` is still 1 as boolean

Java 10, 145 129 107 106 103 93 77 bytes

Loads of bytes saved thanks to @Frozn, and by using Object as return-type, we can comply with OP's rules of returning the input-array when the array is empty, despite Java's one-return-type-only nature regarding methods.

m->{int f=1;for(var a:m)f=m[0].length!=a.length?0:f;return m.length<1?m:f>0;}

Try it online.

Explanation:

m->{                  // Method with integer-matrix parameter and Object return-type
  int f=1;            //  Flag-integer, starting at 1
  for(var a:m)        //  Loop over the rows of the matrix
    f=m[0].length!=a.length?
                      //   If the length of the first and current row aren't the same:
       0              //    Change `f` to 0
      :               //   Else:
       f;             //    Leave `f` the same
  return m.length<1?  //  If there is only one row:
          m           //   Return the input-matrix as result
         :            //  Else:
          f>0;}       //   Return whether the flag `f` is still 1 as boolean
added 126 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#Java 710, 145 129 107 106 103 9393 77 bytes

Object c(int[][]a){for(int[]x:a)if(a[0].length!=x.length)return 0>1;return a.length<1?a:1>0;}

Ungolfed / test code:

m->{int f=1;for(var a:m)f=m[0].length!=a.length?0:f;return m.length<1?m:f>0;}

Try it here.Try it online.

class Main{
    static Object c(int[][] a){
        for (int[] x : a){
            if (a[0].length != x.length){
                return 0 > 1; // false
            }
        }
        return a.length < 1
            ? a
            : 1 > 0; // true
    }

    public static void main(String[] a){
        System.out.println(c(new int[0][0]));
        System.out.println(c(new int[][]{ new int[]{1}, new int[]{1}, new int[]{2} }));
        System.out.println(c(new int[][]{ new int[]{1} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4, }, new int[]{2}, new int[]{12,314123} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4}, new int[]{1} }));
    }
}

OutputExplanation:

[[I@15db9742
true
true
true
false
false
m->{                  // Method with integer-matrix parameter and Object return-type
  int f=1;            //  Flag-integer, starting at 1
  for(var a:m)        //  Loop over the rows of the matrix
    f=m[0].length!=a.length?
                      //   If the length of the first and current row aren't the same:
       0              //    Change `f` to 0
      :               //   Else:
       f;             //    Leave `f` the same
  return m.length<1?  //  If there is only one row:
          m           //   Return the input-matrix as result
         :            //  Else:
          f>0;}       //   Return whether the flag `f` is still 1 as boolean

#Java 7, 145 129 107 106 103 93 bytes

Object c(int[][]a){for(int[]x:a)if(a[0].length!=x.length)return 0>1;return a.length<1?a:1>0;}

Ungolfed / test code:

Try it here.

class Main{
    static Object c(int[][] a){
        for (int[] x : a){
            if (a[0].length != x.length){
                return 0 > 1; // false
            }
        }
        return a.length < 1
            ? a
            : 1 > 0; // true
    }

    public static void main(String[] a){
        System.out.println(c(new int[0][0]));
        System.out.println(c(new int[][]{ new int[]{1}, new int[]{1}, new int[]{2} }));
        System.out.println(c(new int[][]{ new int[]{1} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4, }, new int[]{2}, new int[]{12,314123} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4}, new int[]{1} }));
    }
}

Output:

[[I@15db9742
true
true
true
false
false

#Java 10, 145 129 107 106 103 93 77 bytes

m->{int f=1;for(var a:m)f=m[0].length!=a.length?0:f;return m.length<1?m:f>0;}

Try it online.

Explanation:

m->{                  // Method with integer-matrix parameter and Object return-type
  int f=1;            //  Flag-integer, starting at 1
  for(var a:m)        //  Loop over the rows of the matrix
    f=m[0].length!=a.length?
                      //   If the length of the first and current row aren't the same:
       0              //    Change `f` to 0
      :               //   Else:
       f;             //    Leave `f` the same
  return m.length<1?  //  If there is only one row:
          m           //   Return the input-matrix as result
         :            //  Else:
          f>0;}       //   Return whether the flag `f` is still 1 as boolean
Changed order of checks to save some more bytes (thanks to @Frozn)
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

#Java 7, 145 129 107 106106 103 93 bytes

Loads of bytes saved thanks to @Frozn, and by using Object as return-type, we can comply with OP's rules of returning the input-array when the array is empty, despite Java's one-return-type-only nature regarding methods.

Object c(int[][]a){if(a.length<1)return null;forfor(int[]x:a)if(a[0].length!=x.length)return 0>1;return a.length<1?a:1>0;}

NOTE: Since Java can't have multiple different return-types, I settled by using a Boolean. It now returns null when the list is empty. (I suggest not restricting output like this to allow more programming languages to enter the challenge next time..)

Ungolfed / test code:

Try it here.Try it here.

class Main{
    static Object c(int[][] a){
        if (a.length < 1){
            return null;
        }
        for (int[] x : a){
            if (a[0].length != x.length){
                return 0 > 1; // false
            }
        }
        return a.length < 1
            ? a
            : 1 > 0; // true
    }

    public static void main(String[] a){
        System.out.println(c(new int[0][0]));
        System.out.println(c(new int[][]{ new int[]{1}, new int[]{1}, new int[]{2} }));
        System.out.println(c(new int[][]{ new int[]{1} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4, }, new int[]{2}, new int[]{12,314123} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4}, new int[]{1} }));
    }
}

Output:

null[[I@15db9742
true
true
true
false
false

#Java 7, 145 129 107 106 bytes

Object c(int[][]a){if(a.length<1)return null;for(int[]x:a)if(a[0].length!=x.length)return 0>1;return 1>0;}

NOTE: Since Java can't have multiple different return-types, I settled by using a Boolean. It now returns null when the list is empty. (I suggest not restricting output like this to allow more programming languages to enter the challenge next time..)

Ungolfed / test code:

Try it here.

class Main{
    static Object c(int[][] a){
        if (a.length < 1){
            return null;
        }
        for (int[] x : a){
            if (a[0].length != x.length){
                return 0 > 1; // false
            }
        }
        return 1 > 0; // true
    }

    public static void main(String[] a){
        System.out.println(c(new int[0][0]));
        System.out.println(c(new int[][]{ new int[]{1}, new int[]{1}, new int[]{2} }));
        System.out.println(c(new int[][]{ new int[]{1} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4, }, new int[]{2}, new int[]{12,314123} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4}, new int[]{1} }));
    }
}

Output:

null
true
true
true
false
false

#Java 7, 145 129 107 106 103 93 bytes

Loads of bytes saved thanks to @Frozn, and by using Object as return-type, we can comply with OP's rules of returning the input-array when the array is empty, despite Java's one-return-type-only nature regarding methods.

Object c(int[][]a){for(int[]x:a)if(a[0].length!=x.length)return 0>1;return a.length<1?a:1>0;}

Ungolfed / test code:

Try it here.

class Main{
    static Object c(int[][] a){
        for (int[] x : a){
            if (a[0].length != x.length){
                return 0 > 1; // false
            }
        }
        return a.length < 1
            ? a
            : 1 > 0; // true
    }

    public static void main(String[] a){
        System.out.println(c(new int[0][0]));
        System.out.println(c(new int[][]{ new int[]{1}, new int[]{1}, new int[]{2} }));
        System.out.println(c(new int[][]{ new int[]{1} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4, }, new int[]{2}, new int[]{12,314123} }));
        System.out.println(c(new int[][]{ new int[]{1,2,3,4}, new int[]{1} }));
    }
}

Output:

[[I@15db9742
true
true
true
false
false
Replaced Boolean with Object to save 1 byte
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading
Now that `p = a[x].length;` is removed, the for-loop can be changed into a foreach to save more bytes
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading
Removed completely unnecessary code..
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading