Sort by

recency

|

372 Discussions

|

  • + 0 comments

    HackerRank

    Explanation: Constructor and Encapsulation:

    The init method (constructor) initializes the object with private attributes __x and __y, which are encapsulated and can't be accessed directly from outside the class.

    Methods like get_coordinates and find_reflection provide controlled access to these private attributes.

    Abstraction:

    The Point class abstracts the concept of a point with coordinates.

    The ReflectivePoint class extends Point and includes a method to find the reflection of a point, abstracting the reflection logic.

    Idiomatic Usage:

    Use of private attributes to enforce encapsulation.

    Descriptive method names and docstrings for clarity.

    Use of inheritance to demonstrate abstraction and reusability.

    This approach leverages the full power of Python's object-oriented features to create a robust, maintainable, and clear implementation. Let me know if this aligns with your needs or if there's something specific you'd like to delve deeper into!

    https://www.hackerrank.com/challenges/find-point/forum

  • + 0 comments

    A way to think about solving the problem

    Information we have: a point P1(x1, y1) and an origin O(xo, yo). What we want to know, P2(x2,y2). Visualization that worked for me(think of singular lines): x-axis |-----------x1—----------xo—------x2—----| Formula for midpoint xo is: xo = (x2 + x1)/ 2 We have xo, so rearrange to solve for x2: New formula: x2 = 2xo - x1

    Why the formula for point-reflection works: When subtracting a coordinate and changing the sign, the coordinate rotates 180 degrees. Example: given x1 = 2, assuming origin is O(0,0), the opposite of x1 would be -2, so x2 = 2(0) - 2
    |--------- -2 —---------------Ox = 0 —--------------- 2 —---|

    Take note of the information that you have and rearrange/find a formula to help solve the problem. Break down the diagram into something simpler.

    For shapes it would be the same concept, just apply to each point.

  • + 0 comments

    hi

  • + 0 comments

    Swift solution

    func findPoint(px: Int, py: Int, qx: Int, qy: Int) -> [Int] { return [qx*2-px, qy*2-py] }

  • + 0 comments

    C++ Solution:

    vector findPoint(int px, int py, int qx, int qy) { return {2*qx - px, 2*qy - py}; }