Skip to content

Instantly share code, notes, and snippets.

@talbergs
Created January 9, 2020 19:13
Show Gist options
  • Save talbergs/6ea1e0fdf8516d7029e18d714f76e637 to your computer and use it in GitHub Desktop.
Save talbergs/6ea1e0fdf8516d7029e18d714f76e637 to your computer and use it in GitHub Desktop.
100 balls, 10 colors and 10 buckets each of whom can hold 10 balls of one or two color kind problem
<?php
// 10 buckets and 10 types of color and 100 balls
// Each color will have at least one ball.
// Each bucket can hold up to ten balls (in result).
// Place all balls in buckets so that each bucket at most containst two variants of color.
// This function should return list of buckets.
// Each bucket is a list of color ids (integers).
$scale = 10;
function distribute($balls, $scale) {
$buckets = [];
// YOUR CODE GOES HERE
// Feel free to declare any helper methods if needed.
return $buckets;
}
// HINT: use `#~ echo thisfile.php | entr -c php thisfile.php` to execute on file write.
// The stuff below is not relevant.
$data_sets = [];
$color_kinds = range(0, $scale - 1);
function color_ball(int $id): string {
$codes = [];
for ($i = 31; $i < 37; $i ++) $codes = array_merge($codes, ["\033[0;${i}m", "\033[1;${i}m"]);
if ($id > count($codes) - 1) throw new Exception("too big color id given: $id");
return $codes[$id] . "*" . "\033[0m";
}
function format_balls(array $balls) {
$str = '';
foreach ($balls as $ball) {
$str .= color_ball($ball);
}
return $str;
}
for ($i = 0; $i < 100; $i ++) {
$data_set = $color_kinds;
$data_set_size = count($color_kinds);
$max_size = $data_set_size * $scale;
while ($data_set_size < $max_size) {
shuffle($color_kinds);
$data_set[] = $color_kinds[rand(0, $scale - 1)];
$data_set_size ++;
}
shuffle($data_set);
$data_sets[] = $data_set;
}
function test($original_balls, $buckets, $scale, &$bucket_nr) {
$bucket_ctn = count($buckets);
if ($bucket_ctn != $scale) {
return "Expected number of buckets: $scale, got: $bucket_ctn";
}
$seen_balls = [];
foreach ($buckets as $bucket_nr => $bucket) {
$balls_in_bucket = count($bucket);
if ($balls_in_bucket != $scale) {
return "Expected $scale balls in bucket $bucket_nr, instead got: $balls_in_bucket";
}
$sorts_of_ball = count(array_unique($bucket));
if ($sorts_of_ball > 2) {
return "Expected number of kinds are 2 (at most) got: $sorts_of_ball in bucket $bucket_nr at scale $scale";
}
$seen_balls = array_merge($seen_balls, $bucket);
}
sort($seen_balls);
sort($original_balls);
if ($seen_balls !== $original_balls) {
return "Tinkering with data set is not allowed, obviously.";
}
return null;
}
foreach ($data_sets as $nr => $balls) {
echo "#$nr Given ".count($balls) . " balls:\n" . format_balls($balls) . "\n";
$buckets = distribute($balls, $scale);
echo "distributed as:\n";
foreach ($buckets as $n => $bucket) {
echo " $n: [".format_balls($bucket)."]\n";
}
if ($error = test($balls, $buckets, $scale, $current)) {
echo "Error: $error\n";
if ($current !== null) {
echo format_balls($buckets[$current]) . " in bucket $current is not correct\n";
}
break;
}
else {
echo "Correct!\n\n";
}
}
echo "\n\nfinish.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment