input: Write a function to calculate the Gini coefficient of a given dataset.
  def gini(x):
      """Calculate Gini Coefficient of a given dataset."""
      # calculate the frequency of each item in x
      x_value_counts = x.value_counts().sort_index() 
  
      # calculate the cumulative sum of x_value_counts
      x_cum_sum = x_value_counts.cumsum()
  
      # calculate the Lorenz values
      n = len(x)
      lorenz = x_cum_sum / n
      lorenz = np.insert(lorenz, 0, 0) # add 0 to the beginning of array
  
      # calculate the Gini Coefficient
      width = lorenz[:-1] - lorenz[1:]
      gini_coef = (1 / n) * sum(width * (lorenz[1:] + lorenz[:-1]))
  
      return gini_coef
  
  assert gini(np.array([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(np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])) == 0.0
  assert gini(np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) == 0.3
  assert gini(np.array([])) == 0.0
output: def gini(x):
      """Calculate Gini Coefficient of a given dataset."""
      if len(x) == 0:
          return 0.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