Git Product home page Git Product logo

test002's Introduction

1)

1_1 1_2 1_3

// 1)
// A.length >= 5
const requestHandler = (A: number[]): boolean => {
    const len = A.length;
    for (let i = 1; i <= len - 4; i += 1) {
        for (let j = i + 2; j <= len - 2; j += 1) {
            const sub1sum = A.slice(0, i).reduce((a, b) => a + b);
            const sub2sum = A.slice(i + 1, j).reduce((a, b) => a + b);
            const sub3sum = A.slice(j + 1, len).reduce((a, b) => a + b);

            if (sub1sum === sub2sum && sub1sum === sub3sum) return true;
        }
    }

    return false;
};

2)

2_1 2_2 2_3

// 2)
const matrixReverseEngineer = (U: number, L: number, C: number[]): string => {
    const len = C.length;

    let ansSub1: string = "";
    let ansSub2: string = "";

    if (U > len || L > len) return "IMPOSSIBLE";

    for (let i = 0; i < len; i += 1) {
        const nextLen = len - i - 1;

        if (C[i] === 0) {
            if (U <= nextLen && L <= nextLen) {
                ansSub1 += "0";
                ansSub2 += "0";
            } else {
                return "IMPOSSIBLE";
            }
        } else if (C[i] === 2) {
            ansSub1 += "1";
            ansSub2 += "1";

            if (U >= 1 && L >= 1) {
                U -= 1;
                L -= 1;
            } else {
                return "IMPOSSIBLE";
            }
        } else {
            if (U <= nextLen && L >= 1) {
                ansSub1 += "0";
                ansSub2 += "1";
                L -= 1;
            } else if (L <= nextLen && U >= 1) {
                ansSub1 += "1";
                ansSub2 += "0";
                U -= 1;
            } else {
                return "IMPOSSIBLE";
            }
        }
    }

    return ansSub1 + "," + ansSub2;
};

console.log(matrixReverseEngineer(3, 2, [2, 1, 1, 0, 1]));
console.log(matrixReverseEngineer(2, 3, [0, 0, 1, 1, 2]));
console.log(matrixReverseEngineer(2, 2, [2, 0, 2, 0]));

3)

3_1 3_2 3_3

// 3)
const findIntegerPoint = (
    AX: number,
    AY: number,
    BX: number,
    BY: number
): string => {
    if (AX === BX) {
        if (BY > AY) {
            return [BX + 1, BY].join(",");
        } else {
            return [BX - 1, BY].join(",");
        }
    } else if (AY === BY) {
        if (BX > AX) {
            return [BX, BY - 1].join(",");
        } else {
            return [BX, BY + 1].join(",");
        }
    }

    const slope = Math.abs((AX - BX) / (AY - BY)); // after turn right

    let dir: [-1 | 1, -1 | 1] | null = null; // after turn right

    if (BX > AX && BY > AY) {
        dir = [1, -1];
    } else if (BX < AX && BY > AY) {
        dir = [1, 1];
    } else if (BX < AX && BY < AY) {
        dir = [-1, 1];
    } else {
        dir = [-1, -1];
    }

    let xCoord = BX;
    let yCoord = BY;

    let count = 0;
    while (count < 10 ** 4) {
        count += 1;
        xCoord += dir[0];
        yCoord = BY + count * dir[1] * slope;

        if (Number.isInteger(yCoord)) {
            break;
        }
    }

    return [xCoord, yCoord].join(",");
};

console.log(findIntegerPoint(-1, 3, 3, 1)); // "2,-1"
console.log(findIntegerPoint(2, 2, 2, -3)); // "1,-3"
console.log(findIntegerPoint(-2, 1, 0, 2)); // "1,0"
console.log(findIntegerPoint(2, 0, 1, -2)); // "-1,-1"
# დათას კოდი
def solution(AX, AY, BX, BY):
    x_abs = abs(AX - BX)
    y_abs = abs(AY - BY)

    CX = BX - y_abs
    CY = BY - x_abs

    k = (CY - BY)/(CX - BX)
    b = CY - k*CX

    ret_x = 0
    ret_y = 0

    if CY < BY:
        for y in range(BY-1, CY-1, -1):
            x = (y - b)/k
            if x%1 == 0: return int(x), int(y)
    else:
        for x in range(BX-1, CX-1, -1):
            y = k*x + b
            if y%1 == 0: return int(x), int(y)
    return ret_x, ret_y

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.