Code Monkey home page Code Monkey logo

ctci-6th-edition-php's Issues

Wrong test case implementations for chapter16/question16.03

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 bug in chapter02/question2.4/LinkedListPartitioner

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 $xstill equal to 5, then run the test and find out.

Quick solution to fix it:

change the code at line 17 to:

$node = $node->getNext();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.