input: Write a function to calculate the Gini coefficient of a given dataset. If the dataset is empty the function should return 0. The input is a list of numbers, calculate the Gini coefficient using the provided values, and return the resulting value.
  def gini(x):
      """Calculate Gini Coefficient of a given dataset."""
      if len(x) == 0: 
          return 0
      x_value_counts = x.value_counts().sort_index() 
      x_cum_sum = x_value_counts.cumsum()
      n = len(x)
      lorenz = x_cum_sum / n
      lorenz = np.insert(lorenz, 0, 0)
      width = lorenz[:-1] - lorenz[1:]
      gini_coef = (1 / n) * sum(width * (lorenz[1:] + lorenz[:-1]))
      return gini_coef
  
  assert gini([2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,21])==0.605
  assert gini([1, 1, 1])==0
  assert gini([1, 1, 99, 167])==0.2154
  assert gini([])==0
  assert gini([1, 1, 99, 99])==0.205
  assert gini([1, 1, 1])==0
output: 
  def gini(x):
      if len(x) == 0:
          return 0
      x = sorted(x)
      n = len(x)
      cum_sum = [0] * n
      for i in range(n):
          cum_sum[i] = cum_sum[i - 1] + x[i]
      lorenz = [cum_sum[i] / cum_sum[-1] for i in range(n)]
      lorenz.insert(0, 0)
      width = [lorenz[i] - lorenz[i + 1] for i in range(n)]
      gini_coef = (1 / n) * sum(width[i] * (lorenz[i + 1] + lorenz[i]) for i in range(n))
      return gini_coef
  