Skip to main content
Commonmark migration
Source Link

#PHP, 88 bytes

PHP, 88 bytes

function($a,$b){return array_map(array_sum,array_chunk($a,~-count($a)/$b+1))+[$b-1=>0];} 

anonymous function, takes array and integer, returns array

The only golfing potential this had was replacing ceil(count($a)/$b)) with (count($a)-1)/$b+1 and abbreviating (count($a)-1) with ~-count($a). The resulting float is implicitly cast to integer in the array_chunk call.

Try it online.

#PHP, 88 bytes

function($a,$b){return array_map(array_sum,array_chunk($a,~-count($a)/$b+1))+[$b-1=>0];} 

anonymous function, takes array and integer, returns array

The only golfing potential this had was replacing ceil(count($a)/$b)) with (count($a)-1)/$b+1 and abbreviating (count($a)-1) with ~-count($a). The resulting float is implicitly cast to integer in the array_chunk call.

Try it online.

PHP, 88 bytes

function($a,$b){return array_map(array_sum,array_chunk($a,~-count($a)/$b+1))+[$b-1=>0];} 

anonymous function, takes array and integer, returns array

The only golfing potential this had was replacing ceil(count($a)/$b)) with (count($a)-1)/$b+1 and abbreviating (count($a)-1) with ~-count($a). The resulting float is implicitly cast to integer in the array_chunk call.

Try it online.

added golfing info
Source Link
Titus
  • 14.9k
  • 1
  • 25
  • 41

#PHP, 88 bytes

function($a,$b){return array_map(array_sum,array_chunk($a,~-count($a)/$b+1))+[$b-1=>0];} 

anonymous function, takes array and integer, returns array

The only golfing potential this had was replacing ceil(count($a)/$b)) with (count($a)-1)/$b+1 and abbreviating (count($a)-1) with ~-count($a). The resulting float is implicitly cast to integer in the array_chunk call.

Try it online.

#PHP, 88 bytes

function($a,$b){return array_map(array_sum,array_chunk($a,~-count($a)/$b+1))+[$b-1=>0];} 

anonymous function, takes array and integer, returns array

Try it online.

#PHP, 88 bytes

function($a,$b){return array_map(array_sum,array_chunk($a,~-count($a)/$b+1))+[$b-1=>0];} 

anonymous function, takes array and integer, returns array

The only golfing potential this had was replacing ceil(count($a)/$b)) with (count($a)-1)/$b+1 and abbreviating (count($a)-1) with ~-count($a). The resulting float is implicitly cast to integer in the array_chunk call.

Try it online.

Source Link
Titus
  • 14.9k
  • 1
  • 25
  • 41

#PHP, 88 bytes

function($a,$b){return array_map(array_sum,array_chunk($a,~-count($a)/$b+1))+[$b-1=>0];} 

anonymous function, takes array and integer, returns array

Try it online.