careercup / ctci-6th-edition-php Goto Github PK
View Code? Open in Web Editor NEWCracking the Coding Interview 6th Ed. PHP Solutions
Cracking the Coding Interview 6th Ed. PHP Solutions
I think there's a bug in LinkedListPartitioner at line 17 ($node = $previousNode;):
<?php
require_once __DIR__ . '/../../lib/Node.php';
class LinkedListPartitioner {
public static function partition(Node $node, $x) {
$head = $node;
$previousNode = null;
while ($node !== null) {
if ($node->getData() < $x && $previousNode !== null) {
// remove the node from this part of the list
$previousNode->setNext($node->getNext());
// put this node at the begining of the list
$node->setNext($head);
// reset head
$head = $node;
// reset node for the next iteration
$node = $previousNode;
} else {
$previousNode = $node;
$node = $node->getNext();
}
}
return $head;
}
}
If there is more than one nodes at the front of the list that are less than $x
, the iteration will be stuck at the second node because the $previousNode
and the $node
point to the same place. You can change the value array in the test to [ 1,2,3,4,5,6 ]
with $x
still equal to 5, then run the test and find out.
change the code at line 17 to:
$node = $node->getNext();
The following two cases are wrong, as instead of checking if the returned point lies on the lines, are matching against a pre-selected point.
public function testGetPointOfIntersectonWith2OverlappingVerticalLines() {
$point = Intersection::getPointOfIntersecton([ 2, 0 ], [ 2, 10 ], [ 2, 8 ], [ 2, 16 ]);
$expected = [ 2, 10 ];
$this->assertEquals($expected, $point);
}
public function testGetPointOfIntersectonWith2OverlappingLines() {
$point = Intersection::getPointOfIntersecton([ 2, 3 ], [ 4, 6 ], [ 4, 6 ], [ 6, 9 ]);
$expected = [ 4, 6 ];
$this->assertEquals($expected, $point);
}
And the below test case is checking for null when the lines represented by the points in the test case do actually intersect as evidenced here:
public function testGetPointOfIntersectonWithNoIntersection() {
$point = Intersection::getPointOfIntersecton([ 0, 0 ], [ 5, 5 ], [ 1, 4 ], [ 2, 3 ]);
$this->assertNull($point);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.