Skip to content

Commit

Permalink
Switch to resturn object instead of array
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre committed Feb 23, 2024
1 parent 268a243 commit d950d12
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 44 deletions.
23 changes: 12 additions & 11 deletions exercises/practice/two-bucket/.meta/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class TwoBucket
{
private int $goal;
private array $buckets;
public int $moves;
public string $goalBucket;
public int $otherBucket;

public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket)
{
Expand All @@ -19,7 +22,7 @@ public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, s
$this->validate();
}

public function solve(): array
public function solve(): self
{
$this->first()->empty();
$this->second()->empty();
Expand All @@ -35,19 +38,17 @@ public function solve(): array

while (true) {
if ($this->first()->getAmount() === $this->goal) {
return [
'moves' => $moves,
'goalBucket' => $this->first()->getName(),
'otherBucket' => $this->second()->getAmount(),
];
$this->moves = $moves;
$this->goalBucket = $this->first()->getName();
$this->otherBucket = $this->second()->getAmount();
return $this;
}

if ($this->second()->getAmount() === $this->goal) {
return [
'moves' => $moves,
'goalBucket' => $this->second()->getName(),
'otherBucket' => $this->first()->getAmount(),
];
$this->moves = $moves;
$this->goalBucket = $this->second()->getName();
$this->otherBucket = $this->first()->getAmount();
return $this;
}

if ($this->first()->isEmpty()) {
Expand Down
10 changes: 1 addition & 9 deletions exercises/practice/two-bucket/TwoBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,7 @@ public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, s
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
}

public function solve(): array
{
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
}
}

class Bucket
{
public function __construct(string $name, int $size)
public function solve(): self
{
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
}
Expand Down
48 changes: 24 additions & 24 deletions exercises/practice/two-bucket/TwoBucketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck
$twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck);
$result = $twoBucket->solve();

$this->assertEquals(4, $result['moves']);
$this->assertEquals('one', $result['goalBucket']);
$this->assertEquals(5, $result['otherBucket']);
$this->assertEquals(4, $result->moves);
$this->assertEquals('one', $result->goalBucket);
$this->assertEquals(5, $result->otherBucket);
}

/**
Expand All @@ -38,9 +38,9 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck
$twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck);
$result = $twoBucket->solve();

$this->assertEquals(8, $result['moves']);
$this->assertEquals('two', $result['goalBucket']);
$this->assertEquals(3, $result['otherBucket']);
$this->assertEquals(8, $result->moves);
$this->assertEquals('two', $result->goalBucket);
$this->assertEquals(3, $result->otherBucket);
}

/**
Expand All @@ -55,9 +55,9 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc
$twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck);
$result = $twoBucket->solve();

$this->assertEquals(14, $result['moves']);
$this->assertEquals('one', $result['goalBucket']);
$this->assertEquals(11, $result['otherBucket']);
$this->assertEquals(14, $result->moves);
$this->assertEquals('one', $result->goalBucket);
$this->assertEquals(11, $result->otherBucket);
}

/**
Expand All @@ -72,9 +72,9 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc
$twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck);
$result = $twoBucket->solve();

$this->assertEquals(18, $result['moves']);
$this->assertEquals('two', $result['goalBucket']);
$this->assertEquals(7, $result['otherBucket']);
$this->assertEquals(18, $result->moves);
$this->assertEquals('two', $result->goalBucket);
$this->assertEquals(7, $result->otherBucket);
}

/**
Expand All @@ -85,9 +85,9 @@ public function testMeasureOneStepUsingBucketOneOfSize1AndBucketTwoOfSize3StartW
$twoBucket = new TwoBucket(1, 3, 3, 'two');
$result = $twoBucket->solve();

$this->assertEquals(1, $result['moves']);
$this->assertEquals('two', $result['goalBucket']);
$this->assertEquals(0, $result['otherBucket']);
$this->assertEquals(1, $result->moves);
$this->assertEquals('two', $result->goalBucket);
$this->assertEquals(0, $result->otherBucket);
}

/**
Expand All @@ -98,9 +98,9 @@ public function testMeasureUsingBucketOneOfSize2AndBucketTwoOfSize3StartWithBuck
$twoBucket = new TwoBucket(2, 3, 3, 'one');
$result = $twoBucket->solve();

$this->assertEquals(2, $result['moves']);
$this->assertEquals('two', $result['goalBucket']);
$this->assertEquals(2, $result['otherBucket']);
$this->assertEquals(2, $result->moves);
$this->assertEquals('two', $result->goalBucket);
$this->assertEquals(2, $result->otherBucket);
}

/**
Expand All @@ -123,9 +123,9 @@ public function testReachabilityNotPossibleToReachGoalStartWithBucketOneAndEndWi
$twoBucket = new TwoBucket(6, 15, 9, 'one');
$result = $twoBucket->solve();

$this->assertEquals(10, $result['moves']);
$this->assertEquals('two', $result['goalBucket']);
$this->assertEquals(0, $result['otherBucket']);
$this->assertEquals(10, $result->moves);
$this->assertEquals('two', $result->goalBucket);
$this->assertEquals(0, $result->otherBucket);
}

/**
Expand All @@ -140,9 +140,9 @@ public function testWithSameBucketsButDifferentGoalItIsPossible(): void
$twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck);
$result = $twoBucket->solve();

$this->assertEquals(10, $result['moves']);
$this->assertEquals('two', $result['goalBucket']);
$this->assertEquals(0, $result['otherBucket']);
$this->assertEquals(10, $result->moves);
$this->assertEquals('two', $result->goalBucket);
$this->assertEquals(0, $result->otherBucket);
}

/**
Expand Down

0 comments on commit d950d12

Please sign in to comment.