Skip to main content
Convert from program to code snippet.
Source Link
Dewi Morgan
  • 607
  • 4
  • 12

PHP, 81 7474 67 bytes

<?=array_reducearray_reduce($argv$a,function($c,$x){return[$x,$c[1]+($x<$c[0])];})[1]+1;[1]+1

Try it online! Implementation of algorithm from ovs' answer: count all pairs where right < left, then add 1.

Since array_reduce takes only one "carry" param, but we want each iteration to know both the running total and the previous value, we make 'carry' be an array with both elements in, which is a few bytes shorter than making a global, but makes a warning the first time we access the (uninitialized) array.

Reduced from first submission by moving all calculation into the return.

Reduced further since it apparently doesn't need to be a program, just a code snippet that takes an array and outputs a value, so all the boilerplate can go away into a header/footer.

Try it online!

PHP, 81 74 bytes

<?=array_reduce($argv,function($c,$x){return[$x,$c[1]+($x<$c[0])];})[1]+1;

Try it online!

PHP, 81 74 67 bytes

array_reduce($a,function($c,$x){return[$x,$c[1]+($x<$c[0])];})[1]+1

Implementation of algorithm from ovs' answer: count all pairs where right < left, then add 1.

Since array_reduce takes only one "carry" param, but we want each iteration to know both the running total and the previous value, we make 'carry' be an array with both elements in, which is a few bytes shorter than making a global, but makes a warning the first time we access the (uninitialized) array.

Reduced from first submission by moving all calculation into the return.

Reduced further since it apparently doesn't need to be a program, just a code snippet that takes an array and outputs a value, so all the boilerplate can go away into a header/footer.

Try it online!

rm 7 bytes
Source Link
Dewi Morgan
  • 607
  • 4
  • 12

PHP, 8181 74 bytes

<?=array_reduce($argv,function($c,$x){$c[1]+=$x<$c[0];$c[0]=$x;return $c;return[$x,$c[1]+($x<$c[0])];})[1]+1;

Try it online!Try it online!

PHP, 81 bytes

<?=array_reduce($argv,function($c,$x){$c[1]+=$x<$c[0];$c[0]=$x;return $c;})[1]+1;

Try it online!

PHP, 81 74 bytes

<?=array_reduce($argv,function($c,$x){return[$x,$c[1]+($x<$c[0])];})[1]+1;

Try it online!

Source Link
Dewi Morgan
  • 607
  • 4
  • 12

PHP, 81 bytes

<?=array_reduce($argv,function($c,$x){$c[1]+=$x<$c[0];$c[0]=$x;return $c;})[1]+1;

Try it online!