repente

Untitled

Jun 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Mos\Dice;
  4.  
  5. /**
  6. * A trait implementing histogram for integers.
  7. */
  8. trait HistogramTrait
  9. {
  10. /**
  11. * @var array $serie The numbers stored in sequence.
  12. */
  13. private $serie = [];
  14.  
  15.  
  16.  
  17. /**
  18. * Get the serie.
  19. *
  20. * @return array with the serie.
  21. */
  22. public function getHistogramSerie()
  23. {
  24. return $this->serie;
  25. }
  26.  
  27.  
  28.  
  29. /**
  30. * Print out the histogram, default is to print out only the numbers
  31. * in the serie, but when $min and $max is set then print also empty
  32. * values in the serie, within the range $min, $max.
  33. *
  34. * @param int $min The lowest possible integer number.
  35. * @param int $max The highest possible integer number.
  36. *
  37. * @return string representing the histogram.
  38. */
  39. public function printHistogram(int $min = null, int $max = null)
  40. {
  41. $arr = $this->getHistogramSerie();
  42. $histogramValue = [];
  43. $dataHistogram = array_count_values($arr);
  44.  
  45. if ($min && $max) {
  46. for ($i = $min; $i <= $max; $i++) {
  47. if (array_key_exists($i, $dataHistogram)) {
  48. echo ' <br>' . "$i. " . str_repeat("*", $dataHistogram[$i]);
  49. } else {
  50. echo ' <br>' . "$i. ";
  51. }
  52. }
  53. } else {
  54. foreach ($dataHistogram as $key => $value) {
  55. echo ' <br>' . "$key. " . str_repeat("*", $value);
  56. }
  57. }
  58.  
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment