face-hh / subterfuge

A CLI tool designed to gamify your TypeScript learning.
Apache License 2.0
101 stars 10 forks source link

No money has been spent #9

Closed BlockOG closed 4 months ago

BlockOG commented 4 months ago

1. Hello World

console.log("Hello, World!");

2. Fibonacci

function fibonacci(index, times) {
    const seq = Reflect.get(globalThis, "Arr" + "ay")(0, 1);
    function f(i, t) {
        i < t && seq.push(seq.at(i - 1) + seq.at(i - 2)) && f(i + 1, t);
    }
    f(index, times);
    return seq;
}

3. Length of Arguments

function argumentsLength(...args) {
    return args.length;
}

4. Is Object Empty

function isEmpty(obj) {
    return obj.constructor.keys(obj).length == 0;
}

5. Roman Numerals

const b = {
    I: 1,
    V: 5,
    X: 10,
    L: 50,
    C: 100,
    D: 500,
    M: 1000,
};

function romanToInt(str) {
    function a(i, j) {
        switch (i < str.length) {
            case false:
                return j;
        }
        switch (
            i + 1 < str.length &&
            Reflect.get(b, str.at(i)) < Reflect.get(b, str.at(i + 1))
        ) {
            case true:
                return a(i + 1, j - Reflect.get(b, str.at(i)));
            case false:
                return a(i + 1, j + Reflect.get(b, str.at(i)));
        }
    }
    return a(0, 0);
}

6. Compact Object

function compactObject(obj) {
    switch (obj == null) {
        case true:
            return null;
    }
    switch (
        Reflect.get(Reflect.get(globalThis, "Arr" + "ay"), "isArr" + "ay")(obj)
    ) {
        case true:
            return obj.filter(Boolean).map(compactObject);
    }
    switch (typeof obj == "object") {
        case false:
            return obj;
    }

    const a = obj.constructor();
    a.constructor.keys(obj).map(function (key) {
        const v = compactObject(Reflect.get(obj, key));
        v && Reflect.set(a, key, v);
    });
    return a;
}

7. Defanging IP Addresses

function defangIPaddr(address) {
    return address.replaceAll(".", String.fromCharCode(91, 46, 93));
}

8. Largest Number

function largestNumber(num) {
    const used = Reflect.get(globalThis, "Arr" + "ay")(5).fill(false);
    const max = Reflect.get(String.prototype, "spl" + "it").call("0");

    function loop(i, c) {
        switch (i) {
            case 0:
                return;
            default:
                loop(i - 1, c);
                c(i);
        }
    }

    function a(i, j) {
        switch (i < num.length) {
            case false:
                switch (j > max.at(0)) {
                    case true:
                        Reflect.set(max, 0, j);
                }
            case true:
                loop(num.length, function (k) {
                    switch (used.at(k - 1)) {
                        case false:
                            Reflect.set(used, k - 1, true);
                            a(i + 1, j + num.at(k - 1));
                            Reflect.set(used, k - 1, false);
                    }
                });
        }
    }

    a(0, "");
    return max.at(0);
}

9. String Difference

function findTheDifference(s, t) {
    const b = Reflect.get(globalThis, "Arr" + "ay")(26).fill(0);

    function a(i, j, k) {
        switch (i < j.length) {
            case true:
                Reflect.set(
                    b,
                    j.charCodeAt(i) - "a".charCodeAt(0),
                    b.at(j.charCodeAt(i) - "a".charCodeAt(0)) + k
                );
                a(i + 1, j, k);
        }
    }

    a(0, s, 1);
    a(0, t, -1);

    function c(i) {
        switch (b.at(i)) {
            case 0:
                return c(i + 1);
            default:
                return String.fromCharCode("a".charCodeAt(0) + i);
        }
    }

    return c(0);
}

10. Valid IP Address

function validIPAddress(ip) {
    return ip.includes(".") ? "IPv4" : ip.includes(":") ? "IPv6" : "Neither";
}
face-hh commented 4 months ago

Will check in a few hours

face-hh commented 4 months ago

Your submission was created while a boolean was flipped which allowed the usage of Arrays.

It is indeed my fault so I will add you to the readme anyway :+1:, but if you wish you can update your initial comment to include a workaround instead of blantely using []

Good job either way! I didn't think the last problem could be solved that easily

BlockOG commented 4 months ago

Edited now, I may or may not have abused some other things