You will be given an array of non-negative integers and positive integer bin width. 

Your task is to create the Histogram method that will return histogram data corresponding to the input array. The histogram data is an array that stores under index i the count of numbers that belong to bin i. The first bin always starts with zero. 

On empty input you should return empty output.

Examples:

For input data [1, 1, 0, 1, 3, 2, 6] and binWidth=1 the result will be [1, 3, 1, 1, 0, 0, 1] as the data contains single element "0", 3 elements "1" etc.
For the same data and binWidth=2 the result will be [4, 2, 0, 1]
For input data [7] and binWidth=1 the result will be [0, 0, 0, 0, 0, 0, 0, 1]

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Data structures

Starter code: def histogram(values, bin_width):
	

Solutions:
def histogram(lst, w):
	lst = [n // w for n in lst]
	m = max(lst, default=-1) + 1
	return [lst.count(n) for n in range(m)]


def histogram(v, w):
	return [sum((j in range(i, w + i) for j in v)) for i in range(0, max(v) + 1, w)] if v else []


def histogram(values, bin_width):
	return [sum([values.count(i + j) for i in range(bin_width)]) for j in range(max(values) + 1) if j % bin_width == 0] if values else []


def histogram(a, n):
	r = [0] * (max(a, default=-1) // n + 1)
	for x in a:
		r[x // n] += 1
	return r


def histogram(values, bin_width):
	k = []
	end = max(values) if values else -1
	bin = [i for i in range(bin_width, end + bin_width + 1, bin_width)]
	for i in bin:
		count = 0
		for val in values:
			if i - bin_width <= val < i:
				count += 1
		k += [count]
	return k


from collections import Counter

def histogram(values, bin_width):
	count = Counter(values)
	return [sum((count[i + x] for x in range(bin_width))) for i in range(0, max(count) + 1, bin_width)] if count else []


import math

def histogram(values, bin_width):
	if values == []:
		return []
	hist = [0] * math.ceil((max(values) + 1) / bin_width)
	for v in values:
		hist[math.floor(v / bin_width)] += 1
	return hist


from itertools import groupby

def histogram(values, bin_width):
	c = {k: len(list(g)) for (k, g) in groupby(sorted(values), key=lambda x: x // bin_width)}
	return [c.get(i, 0) for i in range(max(c) + 1)] if c else []


def histogram(values, bin_width):
	if len(values) == 0:
		return []
	list = [values.count(digit) for digit in range(0, max(values) + 1)]
	return [sum(list[digit:digit + bin_width]) for digit in range(0, len(list), bin_width)]