-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from jrdnull/master
Add clock problem
- Loading branch information
Showing
3 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
require_once realpath(__DIR__ . '/clock.php'); | ||
|
||
class ClockTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testOnTheHour() | ||
{ | ||
$clock = new Clock(8); | ||
|
||
$this->assertEquals('08:00', $clock->__toString()); | ||
} | ||
|
||
public function testPastTheHour() | ||
{ | ||
$this->markTestSkipped(); | ||
|
||
$clock = new Clock(11, 9); | ||
|
||
$this->assertEquals('11:09', $clock->__toString()); | ||
} | ||
|
||
public function testAddingAFewMinutes() | ||
{ | ||
$this->markTestSkipped(); | ||
|
||
$clock = new Clock(10); | ||
|
||
$clock = $clock->add(3); | ||
|
||
$this->assertEquals('10:03', $clock->__toString()); | ||
} | ||
|
||
public function testAddingOverAnHour() | ||
{ | ||
$this->markTestSkipped(); | ||
|
||
$clock = new Clock(10); | ||
|
||
$clock = $clock->add(61); | ||
|
||
$this->assertEquals('11:01', $clock->__toString()); | ||
} | ||
|
||
public function testWrapAroundAtMidnight() | ||
{ | ||
$this->markTestSkipped(); | ||
|
||
$clock = new Clock(23, 30); | ||
|
||
$clock = $clock->add(60); | ||
|
||
$this->assertEquals('00:30', $clock->__toString()); | ||
} | ||
|
||
public function testSubtractMinutes() | ||
{ | ||
$this->markTestSkipped(); | ||
|
||
$clock = new Clock(10); | ||
|
||
$clock = $clock->sub(90); | ||
|
||
$this->assertEquals('08:30', $clock->__toString()); | ||
} | ||
|
||
public function testWrapAroundBackwards() | ||
{ | ||
$this->markTestSkipped(); | ||
|
||
$clock = new Clock(0, 30); | ||
|
||
$clock = $clock->sub(60); | ||
|
||
$this->assertEquals('23:30', $clock->__toString()); | ||
} | ||
|
||
public function testWrapAroundDay() | ||
{ | ||
$this->markTestSkipped(); | ||
|
||
$clock = new Clock(5, 32); | ||
|
||
$clock = $clock->add(25 * 60); | ||
|
||
$this->assertEquals('06:32', $clock->__toString()); | ||
} | ||
|
||
public function testWrapAroundDayBackwards() | ||
{ | ||
$this->markTestSkipped(); | ||
|
||
$clock = new Clock(5, 32); | ||
|
||
$clock = $clock->sub(25 * 60); | ||
|
||
$this->assertEquals('04:32', $clock->__toString()); | ||
} | ||
|
||
public function testEquivalentClocks() | ||
{ | ||
$this->markTestSkipped(); | ||
|
||
$this->assertEquals(new Clock(15, 37), new Clock(15, 37)); | ||
} | ||
|
||
public function testInequivalentClocks() | ||
{ | ||
$this->markTestSkipped(); | ||
|
||
$this->assertNotEquals(new Clock(01, 01), new Clock(18, 32)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
class Clock | ||
{ | ||
private $minutes; | ||
|
||
/** | ||
* @param int $hour | ||
* @param int $minutes | ||
*/ | ||
public function __construct($hour, $minutes = 0) | ||
{ | ||
$this->minutes = $hour * 60 + $minutes; | ||
} | ||
|
||
/** | ||
* Returns a new Clock incremented by $minutes | ||
* | ||
* @param int $minutes | ||
* @return Clock | ||
*/ | ||
public function add($minutes) | ||
{ | ||
return $this->build($this->minutes + $minutes); | ||
} | ||
|
||
/** | ||
* Returns a new Clock deincremented by $minutes | ||
* | ||
* @param int $minutes | ||
* @return Clock | ||
*/ | ||
public function sub($minutes) | ||
{ | ||
return $this->build($this->minutes - $minutes); | ||
} | ||
|
||
/** | ||
* Returns the string representation of the receiver | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return sprintf('%02d:%02d', $this->minutes / 60, $this->minutes % 60); | ||
} | ||
|
||
/** | ||
* @param $minutes | ||
* @return Clock | ||
*/ | ||
private function build($minutes) | ||
{ | ||
if ($minutes < 60) { | ||
$minutes += 24 * 60; | ||
} | ||
|
||
$hour = floor($minutes / 60); | ||
return new Clock($hour < 24 ? $hour : $hour - 24, $minutes % 60); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ | |
"active": false, | ||
"problems": [ | ||
"trinary", | ||
"wordy" | ||
"wordy", | ||
"clock" | ||
], | ||
"deprecated": [ | ||
|
||
|